22

Rather than ask for your favorite, lets just list them off.

What are the more useful commands inside less?

Personally, I use:

  • / (search forward)
  • ? (search backwards)
  • F (enable tail -f like behvaior, Ctrl-C to break out of it)
  • v (open file in $EDITOR for editing, defaults to vi/vim)

Others?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
geoffc
  • 668
  • 4
  • 13
  • 18

11 Answers11

11

One can also use the mark feature to remember (and jump back to) specific positions in the file.

For example, type ma to mark a position with the label "a". To jump back to the position, simply type 'a. Labels can be any alphabet (case sensitive) thus allowing up to 52 possible marks ([a-zA-Z]) although I seldom use more than 2.

Shawn Chin
  • 1,101
  • 7
  • 9
7
  • u - up half page
  • d - down half page
  • k - scroll single line up
  • j - scroll single line down
  • -i - case insensitive searching unless pattern contains capitals (as clo or in less)
  • / /pattern/ - search (used with n)
  • -p /pattern/ open file at /pattern/ (as clo)
  • & /pattern/ show lines containing /pattern/ (like grep)
gvkv
  • 2,728
  • 24
  • 17
6

-S (either at the command line or typing - and S while running less) chops long lines and is helpful when viewing e.g. log files. Type - and S again to switch back.

Kevin
  • 40,087
  • 16
  • 88
  • 112
kkaempf
  • 51
  • 1
5

Other than the obvious, my two favorites:

  1. export LESS=-FX - if there is under one page, I don't need to press q to quit
  2. Type /^HEADING to search for headings and /^ *-option to search for options
    (especially useful in the bash man page, which in very large)
Mikel
  • 56,387
  • 13
  • 130
  • 149
4
  • ^f - page down
  • ^b - page up
  • G - go to the end of buffer
  • gg - go to the beginning of the buffer

also - most of them are just vim-like functions for navigation.

Eimantas
  • 1,466
  • 3
  • 23
  • 30
3

This isn't a function inside of less, but it is a useful feature.

I like man pages, and prefer less(1) as my pager. However, most GNU software keeps the manual in the info(1) (GNU Texinfo) format, and I'm not a fan of the info(1) interface. Just give me less.

So, I read info(1) pages using less instead. The following command will print out the info(1) pages, using the familiar interface of less!

info gpg |less
Stefan Lasiewski
  • 19,264
  • 24
  • 70
  • 85
2
  • n - continue searching for your pattern in the same direction
  • N - continue searching for your pattern in the opposite direction

If you started searching for 'foo' and want to keep finding instances of it, you can hit / followed by return over and over, or you can just hit n (with no return) to keep on searching in the same direction.

  • -N - show line numbers
  • -I - ignore case in your search pattern (useful when looking for errors that might be spelled as 'ERROR|Error|error')
  • -G - turn off highlighting

You can type those while less is already viewing a file and they'll take affect on whatever file you're viewing. You can undo them by typing them again.

Mark McKinstry
  • 14,943
  • 4
  • 34
  • 27
1

Although it took a question here to remind me how to do it, I always liked the -e or --quit-at-eof option of less.

This nominally causes any page down at the end of the file to quit out of less, but has the useful side effect that if you specified multiple files, it also causes a page down at the end of one file to take you on to the next. That can save loads of :n's if scanning through a bunch of files.

Mark Booth
  • 1,011
  • 3
  • 14
  • 30
1

Other (for me) useful commands are J, K, that works as j, k, but do not stop scrolling at the begin or end of file: it is useful because I often use the terminal border as a visual marker.

As a note, they seem to be undocumented, afaik.

Next, I use less to pipe together zipped and not zipped file (it works thanks to lesspipe), as in less /var/log/dpkg.log* | less

enzotib
  • 50,671
  • 14
  • 120
  • 105
1

Not much of a list, but nonetheless quite useful. Just search through man pages by jumping back and forth to matched (case-insensitive) expressions.

# use n or shift-n to cycle through the matches
man bash | less -Ip 'parameter expansion'  
puja
  • 1
  • 1
0

I like the adjustable horizontal scroll option, i.e. prepend the left/right arrow key with a number and less will scroll that number of columns from then on, works best with chop-lines option -S.

less reads command line switches from the $LESS variable on startup, here are my preferred switches:

$ echo $LESS
-JMQRSi
  • -J add status column on the left, marks columns with search hits.
  • -M more verbose status line.
  • -Q no bells.
  • -R don't convert raw input, lets escape sequences be interpreted.
  • -S disable line wrapping.
  • -i case insensitive searching.
Thor
  • 16,942
  • 3
  • 52
  • 69