an atonement of nano

apparently this was discussed on hacker news and i only just found out about it, and also on lobste.rs.

for years nano has been nothing but a mockery for those who use emacs or vim. a friendly and pragmatic editor? what a laughable prospect.

well this is where it ends. nano is a friendly and pragmatic editor, but it's no notepad. nano has depth. let's take a look.

indentation

we'll start with something simple. nano can be set to vi-like auto indentation with M-I. Here M refers to the alt key, and I refers to an unshifted i. with this enabled, nano will copy any whitespace from the beginning of the current line onto the next line when you hit return.

you can make this permanent by adding "set autoindent" to you $XDG_CONFIG_HOME/nano/nanorc file.

searching

i just want to clear up searching, as i've seen a few people have trouble with the bindings. ^W initiates a forward search and prompts you for a string. ^Q initiates a search backwards in the same way. M-W and M-Q move forwards and backwards through the search occurences. searching wraps.

backups

some people say backups are unnecessary if you use version control. i say that they have different uses. here's what you need:

set backup
set backupdir "~/.cache/nano/backups"

every time you save, a new backup is added. maybe you won't use them. backups have saved me before.

linting

nano comes with support for linting a few languages. if yours isn't supported, add something like the following to your config file:

extendsyntax sh linter dash -n

"sh" should be the name of the syntax file you want it to apply to. "dash -n" should be the linting command. you can run the linter with M-B. it works pretty nicely.

commenting and uncommenting lines

M-3 comments or uncomments a line. nano does in fact have a mark, which can be activated with M-A. While the mark is active (text is highlighted), M-3 will comment or uncomment all the highlighted lines.

formatting

nano has support for formatters. as with the linter, this can be added to syntax which doesn't already have it with

extendsyntax go formatter gofmt -w

it copies the current file to a temporary file, runs the formatter on it, and if there are no errors copies the new file back. you can run it with M-F.

higher level movement

everyone knows how to move up and down, left and right. nano can also move in blocks with C-Up and C-Down and in brackets with M-].

nano also has anchors. an anchor can be put on any line with M-Ins. If you enable line numbers with M-N, you can see a diamond on lines with anchors. you can navigate between anchors with M-PgUp and M-PgDown. i like to use this with the mark.

regular expressions

add "set regexp" to your nanorc and you will be able to use regular expressions in searches.

macros

nano has macros. M-: starts and ends recording a macro. M-; plays back a macro. it's enough for most things i use macros for. i find it useful to "set stateflags", which puts a "R" in the top right of the screen while a macro is being recorded. it also puts an asterisk next to the filename when it has been changed from disk.

buffers

M-← and M-→ navigate between buffers. i normally use a buffer for what i'm editing, and a scratch buffer for commands and notes.

external commands

^T runs an external command, and puts the output where the cursor is. pressing M-F at the prompt will put the output into a new buffer.

prepending a command with "|" will give the selected region as the command input, and replace it with the command output.

conclusion

those were some of the things that you can do with nano that a lot of people don't seem to realise. it is definitely not up to the level of vim or emacs, but it is a very capable editor. maybe if we dropped the elitism and actually read the manual like we preach so much we would appreciate the diversity in tools we can use.