22
votes

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?

2
  • For the record v opens $EDITOR with local file - not necessary vi/vim. Aug 30, 2010 at 18:06
  • Edited in that change! Thanks Maciej! I did not know that one!
    – geoffc
    Aug 30, 2010 at 20:04

11 Answers 11

11
votes

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.

2
  • Awesome! I needed that feature and did not know it existed!
    – geoffc
    Aug 30, 2010 at 10:13
  • 1
    Also useful (from the man page) is that if you jump somewhere (eg, with 'g'), you can jump back with '' (double apostrophe). May 14, 2012 at 13:38
7
votes
  • 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)
3
  • ? /pattern/ - search backwards (also used with n) F - Follow like tail -f
    – Bauna
    Aug 10, 2010 at 22:16
  • As i recall it, &pattern shows ONLY lines that contain /pattern/ ?
    – Shawn Chin
    Aug 11, 2010 at 13:36
  • @lsc: Yes, that's right. I've updated.
    – gvkv
    Aug 11, 2010 at 16:07
6
votes

-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.

5
votes

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)
1
  • +1 for LESS=-FX, just what i wanted, thanks!
    – ipd
    Jun 29, 2011 at 18:13
4
votes
  • ^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.

2
  • I use space for page down, b for back a page (page up). g for beginning, G for the end as well.
    – geoffc
    Aug 10, 2010 at 20:31
  • Don't forget: 42g - go to line number 42. Jul 14, 2011 at 12:50
3
votes

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
2
votes
  • 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.

1
vote

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.

1
vote

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

1
vote

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'  
0
votes

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .