Wednesday, February 12, 2014

Nano – Shortcuts, Syntax Highlight And Nanorc Config File

I have been using nano as my favorite console editor (i use emacs/kate for the rest) for some time now. It’s simplicity, the speed and usefulness of it made it what it is. But recently, i’ve been trying to find out more features that i might been missing. One of those were the shortcuts. The other was syntax highlighting! Meanwhile, the things i discovered!

Before we start, here’s some useful links:

So let’s start with the basic shortcuts!
“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) key.”
The help system in nano is available by pressing ^G.
Where is
^W : allows to search for an expression

^W expr Meta+w : find the next match
^W ^T numline or Meta+g numline, numcol : will jump to that line number
^W Meta+R expr1 RET expr2 : will replace expr1 with expr2. It asks for the confirmation
Replace this instance?
 Y Yes           A All
 N No           ^C Cancel 
^K : cut text (if nothing is selected, it cuts a line
^U : paste the cut text, or undo the justification
^J : Justify the paragraph
Meta+j : justify the whole buffer
Meta+} : indent
Meta+{ : unindent
Meta+a : set/unset the mark ( similar to ^space in emacs)
^space : move forward one word
Meta+space : move backward one word
Page up/down : scroll up/down half(?) buffer
^W ^Y : jump to the first line
^W ^V : jump to the last line
^Y : jump to the Previous Page (up almost “one” buffer”)
^V : jump to the Next Page (down almost “one” buffer”)
^W ^Y : jump to the first line
^W ^V : jump to the last line
M-< or (M-,) Switch to the previous file buffer
M-> or (M-.) Switch to the next file buffer

And now, let’s look at the .nanorc file.
Here, i’ll go through the .nanorc config file, try to explain parts of it and set the syntax highlight on.
See the first part for shortcuts .

You can use the sample nanorc from /usr/share/nano/ , a copy from /etc/nanorc, or even from the sample nanorc in /usr/share/doc/nano/examples/. A blank one is just fine too.
If in doubt, do nano -V to see if it was compiled with the proper flags. here’s mine:
nano -V
GNU nano version 2.0.4 (compiled 12:23:22, Jun 22 2007)
Email: nano@nano-editor.org Web: http://www.nano-editor.org/
Compiled options: –disable-wrapping-as-root –enable-color –enable-multibuffer –enable-nanorc –enable-utf8
auto indent: (Meta-I) to toggle
set autoindent
so the next line starts after the same number of spaces /tabs as the previous line.
nano doesn’t know how to properly indent code lines.

brackets:
set brackets ""')>]}"
to be able to justify paragraphs after one of those characters.
Use ctrl+j to justify the paragraph, meta+j to justify all the file, and ctrl+u to undo the justification

Match Brackets
set matchbrackets "(]}"
use meta+altgr+9 (i.e meta+] ) to switch between the matching brackets

Remove empty first line (Meta-O) to toggle
set morespace
as said in the manual: “Use the blank line below the titlebar as extra editing space.”

Mouse support (Meta-M)
set mouse
Well, i’m still giving a 2nd change, because it’s not that useful. With it enable, nano will allow you to move the cursor, set a mark (see below) and use the helpful shortcut list at the bottom. To use the mark, double click and drag, or double click and move the cursor with the arrow keys. You can them, cut ( ctrl+k ), and to paste it, ctrl+u. Those are the good things. the bad, it won’t let you middle click and thus paste things into the current position inside the editor. Also, copying text with the mouse to use outside the editor is disabled. So basicly, we get a more featured editor, but with less integration with the surrounding environment. Completly useless in text mode.
If only it had the ability to scroll up/down with the mouse wheel…

Allow multibuffer (Meta-F)
set multibuffer
This is actually extremely useful. If this option isn’t active, the crtl+r (read file)
just adds the file into the same buffer. With it, it opens a new buffer! You can cicle through the buffers with Meta+< and Meta+>
set smooth
Text will scroll line-by-line, instead of the usual chunk-by-chunk behavior.

Save automatically on exit (without prompt)
set tempfile
Although this might seen a time-saver, beware to not enable this on a normal user. When enabled, if nano opens a file which the user doesn’t have permition and modifies it, nano won’t ask for the option to not save it, and thus you’ll be stuck inside nano forever! :P
( you can ofc, save it on a proper directory:/home/you/thatevilfile )
Aparently, the bug is said to be fixed on the version2, but i guess not quite. i’ve to check upon that…
Next the include files for the sintax highlighting. just comment the include lines you’ll think wont be useful.
i’m using this ones:
## Nanorc files
include “/usr/share/nano/nanorc.nanorc”
## C/C++
include “/usr/share/nano/c.nanorc”
## HTML
include “/usr/share/nano/html.nanorc”
## Patch files
include “/usr/share/nano/patch.nanorc”
## Manpages
include “/usr/share/nano/man.nanorc”
## Groff
include “/usr/share/nano/groff.nanorc”
## Perl
include “/usr/share/nano/perl.nanorc”
## Python
include “/usr/share/nano/python.nanorc”
## Bourne shell scripts
include “/usr/share/nano/sh.nanorc”
include “/usr/share/nano/gentoo.nanorc”
## Syntax Highlight Pack
## CSS
include “/usr/share/nano/css.nanorc”
## php
include “/usr/share/nano/php.nanorc”
## makefile .am
include “/usr/share/nano/gentoo.nanorc”
More syntax higlight files: syntax Highlight pack
they are a colection taken from the gentoo foruns.
I’ve took the following 2 from more Syntax highlighting(dead link as of 2012)
This is useful for those debian people.

## syntax highlighting for /etc/apt/sources.list
## syntax "apt/sources.list" "sources\.list(\.old|~)?$"
# component
color brightmagenta "^deb(-src)? ((http|file|ftp):/[^ ]+|cdrom:\[[^\]]+\]/|cdrom:\[[a-zA-Z0-9\._-\(\) ]+\]/) [^ ]+ .+$"
# distribution
color brightred "^deb(-src)? ((http|file|ftp):/[^ ]+|cdrom:\[[^\]]+\]/|cdrom:\[[a-zA-Z0-9\._-\(\) ]+\]/) [^ ]+"
# URI
color brightgreen "(http|file|ftp):/[^ ]+"
# cdroms
# [^\]] does not work...
color brightgreen "cdrom:\[[a-zA-Z0-9\._-\(\) ]+\]/"
# deb / deb-src
color cyan "^deb"
color brightblue "^deb-src"
# comments
color brightyellow "#.*"
config files (.ini)

# ini highlighting
syntax "ini" "\.ini(\.old|~)?$"
# values
color brightred "=.*$"
# equal sign
color green "="
# numbers
color brightblue "-?[0-9\.]+\s*($|;)"
# ON/OFF
color brightmagenta "ON|OFF|On|Off|on|off\s*($|;)"
# sections
color brightcyan "^\s*\[.*\]"
# keys
color cyan "^\s*[a-zA-Z0-9_\.]+"
# comments
color brightyellow ";.*$"
That’s all for now.. Hope you’ll find it useful to bring more “shiningness” to your nano.

http://freethegnu.wordpress.com/2007/06/23/nano-shortcuts-syntax-highlight-and-nanorc-config-file-pt1/

No comments:

Post a Comment