Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share

locked by Michael Mrozek Jul 23 '12 at 2:37

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

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

11 Answers 11

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

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

share

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'  
share

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

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

share

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
share

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.

share

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)
share
    
+1 for LESS=-FX, just what i wanted, thanks! –  ipd Jun 29 '11 at 18:13
  • 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)
share
    
? /pattern/ - search backwards (also used with n) F - Follow like tail -f –  Bauna Aug 10 '10 at 22:16
    
As i recall it, &pattern shows ONLY lines that contain /pattern/ ? –  Shawn Chin Aug 11 '10 at 13:36
    
@lsc: Yes, that's right. I've updated. –  gvkv Aug 11 '10 at 16:07

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.

share
    
Awesome! I needed that feature and did not know it existed! –  geoffc Aug 30 '10 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). –  Steve Bennett May 14 '12 at 13:38
  • ^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.

share
    
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 '10 at 20:31
    
Don't forget: 42g - go to line number 42. –  Wes Hardaker Jul 14 '11 at 12:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.