Nano Text Editor Tutorial for Programmers

13 02 2009

Now that I’ve been programming a lot of C in Linux, I’ve learned more of the useful nano feature that many people overlook. Despite people going on about how much more capable vi is, I just can’t get the hang of it, nor do I like the style of it enough to want to. My first time using vi, and several times after for that matter, my expierence went something like this.

***********************************************************************
* I think I’ve got the hang of it now:  exit, ^D, ^C, ^\, ^Z, ^Q, ^^, *
* F6, quit, ZZ, :q, :q!, M-Z, ^X^C, logoff, logout, close, bye, /bye, *
* stop, end, F3, ~., ^]c, +++ ATH, disconnect, halt,  abort,  hangup, *
* PF4, F20, ^X^X, :D::D, KJOB, F14-f-e, F8-e,  kill -1 $$,  shutdown, *
* init 0, kill -9 1, Alt-F4, Ctrl-Alt-Del, AltGr-NumLock, Stop-A, … *
* …  “Are you sure?”  …   YES   …   Phew …   I’m out          *
***********************************************************************

Does vi have more features? Sure. Am I going to use them? Probably not. If I need to do a bunch of complicated tasks with my text, I’d rather use a full IDE like eclipse or visual studio instead of a skimpy little command line editor. But this is beyond the point, lets get back to nano. It’s lightweight, easy to use, and I like it.

At first glance nano looks very simple, which is on purpose. You can learn the basics in a matter of seconds, glancing at the handy list of commands on the bottom if you forget. The advanced features do exist though, and should be learned as well if you plan to use nano a lot. First off, just open a file with “nano file”. Then you shall be greeted with your file and will easily be able to traverse it with the arrow keys right away.

Special functions use the Control (Ctrl) key, displayed in the help and shortcut lists as ^; the Meta key, displayed as M; or the Escape (Esc) then the key. The meta key and escape is the same thing. For example, the default meta key is alt. To go to a line, I can go alt+g or press escape, and then g.

Ctrl+X: exit. You will be prompted to save or not if the file was modified. Also a way to get out of prompts usually.

Ctrl+W: where is. Enter words to search for or a regular expression. Ctrl+w and enter to find next result. Note that when you hit Ctrl+W a new list of shortcuts appears at the bottom, to do things like find and replace.

Ctrl+K: Cuts text. Removes current line if no text is selected.

Ctrl+A: Jumps to beginning of current line.

Ctrl+E: Jumps to end of current line.

Escape g: go to entered line, column number.

You’ll need syntax highlighting for any sort of programming. Create a .nanorc file in your home directory and enable the defauly syntax highlighting by adding the following to it,

include “/usr/share/nano/c.nanorc”

include “/usr/share/nano/sh.nanorc”

include “/usr/share/nano/java.nanorc”

include “/usr/share/nano/perl.nanorc”

highlighting

etc… Doing a “ls /usr/share/nano/” will give you the full list of defauly syntax highlighting rules you can enable. If you want, you can also look around the Internet for different highlighting rules. If you really want to customize things, you ccan write your own, but that’s beyond the scope of this.

Another useful thing to add to the file, this makes the next line start with the same number of spaces/tabs as the one before. Note though this sometimes creates bugs when pasting things in that already have indents before them.

set autoindent

Another uber handy one to enable, which allows for opening more than one file at once. After you enable this, pressing Ctrl+r will let you open more than one file, and pressing escape < and escape > or meta+< and meta+> will let you switch between the files open.

set multibuffer

My main complaint so far with nano is that I can’t find a way to make it display line numbers on the left/right hand side. The only thing you can do is launch it with nano -c, which will show what line your curser is on in text at the bottom. Meh. Maybe I should just use vi…


Actions

Information

3 responses

16 02 2009
ricky

hi… thank’s for your info from http://www.infomixe.blogspot.com

13 08 2009
Teo

Nano is the best editor for linux!!!

Nano Rules! ;-)

26 01 2010
Stephen Schilsky

Outstanding posting. I appreciate you for posting it. Keep up the great work.

Leave a comment