This are my notes in the fields of computer science and technology. Everything is written with ABSOLUTE NO WARRANTY of fitness for any purpose. Of course, feel free to comment anything.

Sunday, August 9, 2009

Compact guide to less commands

Movement

Arrows, page up/down, home/end keys all work as expected. In addition the following are sometimes useful:
d/uMove half page forward / backward
20gMove to line 20
20%Move to the 20% point
ma/'aset markpoint "a" / move to markpoint "a"
(also instead of "a" any other lowcase letter)

Search

/patternsearch pattern forward
?patternsearch pattern backward
nnext match
Nprevious match
/!patternsearch non-matching line
Esc-uturn match highlighting on/off

Misc

=show info about the file and current position
! commandexecute command and show its output
s filenamesave to file (if the input was a pipe)

GIT_PAGER and LESS colors

I was wondering why, working on two different machines, I was able to display git-log and git-diff colors on one machine and not on the other, despite the same git configuration file ~/.gitconfig:

[color]
diff = auto
status = auto
branch = auto
log = auto

and no particular settings regarding colors in <repository>/.git/config

Although I was not using a Mac, the solution was in this blog post: that is, setting the GIT_PAGER variable to cat was already solving the problem; GIT_PAGER sets which program should be used by git to display for example the diff and log output (default less).

The actual problem was in less and how it outputs ANSI color ESC codes. A solution, similarly to what suggested in that post, was to set the LESS variable to -R and leave GIT_PAGER unset, as it originally was. Regarding the other options suggested in the blog post for LESS, -X and -e have no influence on color display, and -R is probably better than -r; in particular, this in an extract of the less manual, at the -r/-R section, explaining why:
-r or --raw-control-chars

Causes "raw" control characters to be displayed.
[...] Warning: when the -r option is used, less cannot
keep track of the actual appearance of the screen
(since this depends on how the screen responds to
each type of control character). Thus, various display
problems may result, such as long lines being split
in the wrong place.

-R or --RAW-CONTROL-CHARS

Like -r, but only ANSI "color" escape sequences are
output in "raw" form. Unlike -r, the screen appearance
is maintained correctly in most cases. [...]

About Me

My photo
Hamburg, Hamburg, Germany
Former molecular biologist and web developer (Rails) and currently research scientist in bioinformatics.