Confident Clean a Hacked Site Workshop Join me March 21st for this 90 Minute Workshop  Claim your spot

Customising Nano – Productivity Friday

DevOps

Welcome to yet another series of posts. You’ve been enjoying the “back to basics security” series and the “how I make timnash.co.uk” series, so I introduce Productivity Fridays. This series will post on a Friday and, at least for the time being, will be every other Friday. In the series, I will be diving into tips and tricks within my workflow. While mainly technical I’m hoping the series is broad enough to be interesting to developers and system administrators. 

Sound Good? Let’s get going.

Diving into my RC Files

So I guess the first question is “what is an RC File?”. These tend to be dot files for unix applications. Ok Tim, what’s a dot file? A dot file is a configuration file for an application normally stored in your home directory or a subfolder. They get their name because they start with . so .bash_profile (that’s an interesting and terrible example but you will have to wait ‘til the bash productivity Friday to find out why). RC files are dot files with the name rc, for example, .vimrc or .nanorc. So what does RC stand for? Good question, but I actually don’t know and the internet is divided as to where it comes from, though most citations suggest it’s a homage to a precursor to UNIX CTSS where a command script feature called “runcom”.

So RC Files, a type of dot file, which is a configuration file that many applications use. With RC files you can customise the look and the appearance of an application, turn features off, and do all the cool tricks. So let’s get started with an old dog, Nano.

Introducing Nano.

Hi VI users, yes VI is totally superior in every way. Hello eMacs users, yes eMacs are superior in every way. Glad we got that over with. 

I have been using Unix based systems since the 90s, and I used to collect my email in an application called Pine. It was an email client in a terminal, because, well, you picked up your mail that way. Pine had a great little editor for writing an email, but it could open any text file and I, like many, many, many others, found it more convenient to open and edit in that editor than in VI. Now I could have printed off the large cheat sheet of VI shortcodes, got one of those keyboard overlays (do you remember those?) and learnt VI. However, I didn’t and back then you didn’t google the command, you looked it up in a book. So I chose the editor that told me the key combinations to push. I can and do use VI (well almost always Vim) when I have to but my personal preference is Nano. Why Nano? Well you remember Pine? Its editor became a spin-off editor called Pico which in turn became Nano (just drifting over the mass of open-source politics, gnu fans just go with it).

Hi huge Pico fans, yes I know JOE editor also exists (we are several hundred words in and we are still doing disclaimers) so let’s just skip over that whole part of the text editor flame wars and yes, technically, Pico as software also still exists.

Cool, so Nano is a text editor, you run it in the command line. Chances are it’s already installed and you just type nano and the file you wish to edit, or nano newfilename, and it will create a new file when you save.

Once inside you can use arrow keys and shortcuts to move around; in a scary revolutionary concept the backspace key works as you would expect. 

Get the latest package

If it’s not installed you can almost certainly install it from a package manager on any UNIX flavour including MacOs.

You can also just grab it as a binary and add it as a path. Finally, you can use a universal manager like Homebrew and install it that way, which is increasingly becoming my prefered method.

If you are installing it, check the version from your package manager, for installs of CentOS/Ubuntu etc they use a very old version of Nano < 3 whereas something like Homebrew uses, as of writing, 4.8 for example.

Customising Nano text editor

By default, you probably don’t have a nano rc file, so go ahead and create one

nano ~/.nanorc

This is creating a file in your home directory; you can now edit to your heart’s content. I suspect the very first thing you are going to want to do is…

set linenumbers

…save and then re-open Nano. Happy days, you have line numbers; by the way if you ever want to go to a specific line in Nano you can just do…

nano +105 myfile

…which will open the file and put the pointer on line 105, or if the file hasn’t got that line the last line in the file.

A few options I also set are:

set autoindent

If the previous line is indented then the new line will be as well

set nonewlines

By default if the file doesn’t have a newline at the end of the file, it adds one, this can cause fun with checksums so I tend to have it off

set tempfile

When you press ctrl-x to quit it automatically saves without prompting.

If you do that then you may want to have…

set backup

…creates a backup of the file you are editing, for quick restores, though it does mean you need to tidy up after yourself.

While included in things I set I have recently stopped setting tempfile within .nanorc. I did experiment with having it set for my user but not sudo but I ended up in some interesting issues, where if you open a file you don’t have write permission for you get stuck, unable to close it.  Instead, I have it aliased so when I type qedit filename it actually opens as nano –tempfile /filename though I rarely use qedit, just because of muscle memory.

set locking

Warns you if the file is already open, though you can still proceed to edit. 

set nohelp

By default, Nano has some helpful shortcuts in the bottom of the screen (chances are this is the reason you used Nano to start with, but muscle memory has now kicked in and they just take up space)

Syntax Highlighting

AKA pretty colours. Enabling syntax highlighting in part depends on what the package manager installed but I suggest you have a quick peek in:

ls /usr/local/share/nano

If you see a bunch of different files like php.nanorc then good news, your package manager has kindly installed some sort of syntax highlighting and you just need to add…

include /usr/local/share/nano/*.nanorc

…to your .nanorc and you will find syntax highlighting is enabled. 

If you find this is not the case then you can find a really good syntax highlighter package:

https://github.com/scopatz/nanorc

Which is what I use. For most servers I manage I clone into 

~/.nano/ 

folder and then just add

Include ~/.nano/*nanorc

into my .nanorc file

If using the Scopatz package you will need Nano 3 or higher, otherwise you will get a few errors, these are easy to fix, just a bit of manual editing, though ideally just update Nano. 

I have tweaked the colours to fit slightly more with the Monoaki theme I use across the board.

Keyboard bindings/shortcuts

You can map keyboard bindings using the bind command.For example, you can do:

bind ^f whereis main

which maps to ctl f to be the search option, bring Nano inline with most programs, y default this is ctl w 

Other shortcuts you might not know about, that are built-in include:

  • Escape u – Undo 
  • Escape r – Redo the undo
  • Escape n – Toggle line numbers on and off (really useful if you are copying directly out of the file)
  • Escape g – Go to line number
  • Ctl g – Brings up help within Nano
  • Escape \ – Go to top of file
  • Escape / – Go to bottom of the file

Now you might be going, why does the helpful cheat sheet (https://www.nano-editor.org/dist/latest/cheatsheet.html) say alt when you use Escape? This is because alt is used by MacOS much like option and so the alt key doesn’t bind within Nano.

F keys

By default, Nano maps f keys to functions so pressing

  • F1 – brings up help (like ctl G)
  • F2 – Closes Nano (like ctl x)
  • F3 – Saves output (like ctl o)

Because these commands replicate existing ones I do remap them so my remap looks like

  • bind ^z undo main – Undo ctl f
  • bind ^y redo main – redo ctl r 
  • bind ^f whereis main – search

Importantly by mapping ctl f and r I’m overwriting existing mapping, but in both cases they are functionality I have never reached for or can achieve in a different way. 

My F key remaps

  • bind F1 exit main – Save and Exit
  • bind F2 savefile main – Save
  • bind F3 findnext main – Find next for use with ctl f

Complete .nanorc file

My complete .nanorc file looks a little dull but massively changes the experience within Nano

set linenumbers
set autoindent
set nohelp
set nonewlines
set locking
bind ^z undo main
bind ^y redo main
bind ^f whereis main
bind F1 exit main
bind F2 savefile main
bind F3 findnext main
#Add colour syntax
include /usr/local/share/nano/*.nanorc

Helpful Bash/ZSH Aliases

Then within my .zshrc & .bashrc depending on servers I have the following setup

alias snano='sudo nano --rcfile=/home/tnash/.nanorc'
alias qedit=’nano --tempfile --backup’
alias sqedit=’sudo nano --rcfile=/home/tnash/.nanorc --tempfile --backup’

The qedit opens Nano but when you ctl x, it will automatically save without prompting, however it does create a backup of the file.

snano runs sudo nano, however, when you run a sudo command it doesn’t run with your user and as such doesn’t load your config file in, so the alias is not just a shorthand but also ensures my conf is loaded.

sqedit like qedit but with sudo and once again passing my nanorc file though. 

Customising Nano

The humble text editor is somewhere I spend a lot of time, and I want it to be fast and comfortable. There are not a huge number of changes I make but they make a huge difference to my productivity, and yes there are more customisable tools, and yes it might not be the “best”, but the humble nano/pico is powerful enough for me some 20+ years and I’m sure it will continue to be for years to come.

Want to learn more?

This post is from a series called Productivity Fridays, here is the series so far:

Help others find this post:

This post was written by Me, Tim Nash I write and talk about WordPress, Security & Performance.
If you enjoyed it, please do share it!

Helping you and your customers stay safe


WordPress Security Consulting Services

Power Hour Consulting

Want to get expert advice on your site's security? Whether you're dealing with a hacked site or looking to future-proof your security, Tim will provide personalised guidance and answer any questions you may have. A power hour call is an ideal starting place for a project or a way to break deadlocks in complex problems.

Learn more

Site Reviews

Want to feel confident about your site's security and performance? A website review from Tim has got you covered. Using a powerful combination of automated and manual testing to analyse your site for any potential vulnerabilities or performance issues. With a comprehensive report and, importantly, recommendations for each action required.

Learn more

Code Reviews

Is your plugin or theme code secure and performing at its best? Tim provides a comprehensive code review, that combine the power of manual and automated testing, as well as a line-by-line analysis of your code base. With actionable insights, to help you optimise your code's security and performance.

Learn more

Or let's chat about your security?

Book a FREE 20 minute call with me to see how you can improve your WordPress Security.

(No Strings Attached, honest!)