main

Nano the best cmdline text editor


So theres much debate on what text editor you should use in the sysadmin community.
Alot sware by vi/vim personally I use nano and always will, almost all the commands are like a muscle reaction for me :)
I will share with you how to get the most out of this text editor

Keyboard Shortcuts in editor

Check out http://staffwww.fullcoll.edu/sedwards/Nano/UsefulNanoKeyCommands.html for the full list of keyboard commands while in the editor.
The most important ones are
Ctrl+O for writing the file (saving)
Ctrl+X for exiting the editor
Ctrl+- goto line number (it will prompt you for the line number)
Alt+R Search and Replace (Once or All occurances)
Ctrl+W for searching within the file

nanorc Options

Next we will dive into some of the advanced options you can use and set as default. You can see the list of options you can set as default by typing nano --help this should display something like

$ nano --help
Usage: nano [OPTIONS] [[+LINE,COLUMN] FILE]...

Option          GNU long option         Meaning
 -h, -?         --help                  Show this message
 +LINE,COLUMN                           Start at line LINE, column COLUMN
 -A             --smarthome             Enable smart home key
 -B             --backup                Save backups of existing files
 -C <dir>       --backupdir=<dir>       Directory for saving unique backup files
 -D             --boldtext              Use bold instead of reverse video text
 -E             --tabstospaces          Convert typed tabs to spaces
 -F             --multibuffer           Enable multiple file buffers
 -H             --historylog            Log & read search/replace string history
 -I             --ignorercfiles         Don't look at nanorc files
 -K             --rebindkeypad          Fix numeric keypad key confusion problem
 -L             --nonewlines            Don't add newlines to the ends of files
 -N             --noconvert             Don't convert files from DOS/Mac format
 -O             --morespace             Use one more line for editing
 -Q <str>       --quotestr=<str>        Quoting string
 -R             --restricted            Restricted mode
 -S             --smooth                Scroll by line instead of half-screen
 -T <#cols>     --tabsize=<#cols>       Set width of a tab to #cols columns
 -U             --quickblank            Do quick statusbar blanking
 -V             --version               Print version information and exit
 -W             --wordbounds            Detect word boundaries more accurately
 -Y <str>       --syntax=<str>          Syntax definition to use for coloring
 -c             --const                 Constantly show cursor position
 -d             --rebinddelete          Fix Backspace/Delete confusion problem
 -i             --autoindent            Automatically indent new lines
 -k             --cut                   Cut from cursor to end of line
 -l             --nofollow              Don't follow symbolic links, overwrite
 -m             --mouse                 Enable the use of the mouse
 -o <dir>       --operatingdir=<dir>    Set operating directory
 -p             --preserve              Preserve XON (^Q) and XOFF (^S) keys
 -q             --quiet                 Silently ignore startup issues like rc file errors
 -r <#cols>     --fill=<#cols>          Set wrapping point at column #cols
 -s <prog>      --speller=<prog>        Enable alternate speller
 -t             --tempfile              Auto save on exit, don't prompt
 -u             --undo                  Allow generic undo [EXPERIMENTAL]
 -v             --view                  View mode (read-only)
 -w             --nowrap                Don't wrap long lines
 -x             --nohelp                Don't show the two help lines
 -z             --suspend               Enable suspension
 -$             --softwrap              Enable soft line wrapping
 -a, -b, -e,
 -f, -g, -j                             (ignored, for Pico compatibility)

You can set any of these options as on by adding set OPTIONNAME in your ~/.nanorc I have set these following options

set softwrap
set morespace
set const
set autoindent

set softwrap will remove the word wrapping from files (not showing the entire line and ending it with $) as this can mess up copying from a file

set morespace will get rid of the blank line between the title and the first line of the file... cause who doesnt want morespace?!?

set const will give a constant update of what line number and colum your cursor is in. I find this useful when troubleshooting code errors on multiple lines

set autoident this will obey idents from previous lines again useful when coding

Another option I find useful at work is the -B option.
This will save a backup of the file you editied just encase you nuke it after saving. This will save the unedited version of the file you just saved with a ~ for example I dick up httpd.conf but with the -B it saves the undicked file at httpd.conf~.
If I am working on a complex ticket that I'm not too sure on the outcome of my actions I will use the -B option inconjunction with the -C /root/ command this will save all the backups in one place.
You can specify the directory where to keep the backups. If I was sucessfull in my config I just move all the ~ files to a folder stating the ticket number so if needed the changes can be reverted later on
One last thing use the + option when you want to open a file at a particular line number for example nano +30 error.log to open error.log at line number 30

Syntax highlighting

Cause who doesnt like pretty colors?!
For this you will need to obtain the latest version of nano from http://www.nano-editor.org/download.php remove the one that came with your OS and install the latest stable from there
And get the lang pack from github

git clone https://github.com/scopatz/nanorc.git ~/.nano
cat ~/.nano/nanorc >> ~/.nanorc

For tests open up a file with some sort of extenstion (.php .html) and enjoy some syntax highlighting just like vim :P

NANO FOR LIFE