Skip to content

Instantly share code, notes, and snippets.

@TheMatjaz
Created April 8, 2019 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheMatjaz/7a97a37f8adcead38d42cca3f7330153 to your computer and use it in GitHub Desktop.
Save TheMatjaz/7a97a37f8adcead38d42cca3f7330153 to your computer and use it in GitHub Desktop.
Less command cheat sheet (--help in Markdown)

Summary of less comands

For less v487, obtained through less --help.

Commands marked with [N] may be preceded by a number, N. Notes in parentheses indicate the behavior if N is given.

A key preceded by a caret indicates the Ctrl key; thus ^K is Ctrl-K.

  • Display this help: h H
  • Exit: q :q Q :Q ZZ

Moving

  • Forward one line (or N lines): [N] e ^E j ^N CR
  • Backward one line (or N lines): [N] y ^Y k ^K ^P
  • Forward one window (or N lines): [N] f ^F ^V SPACE
  • Backward one window (or N lines): [N] b ^B ESC-v
  • Forward one window (and set window to N): [N] z
  • Backward one window (and set window to N): [N] w
  • Forward one window, but don't stop at end-of-file: [N] ESC-SPACE
  • Forward one half-window (and set half-window to N): [N] d ^D
  • Backward one half-window (and set half-window to N): [N] u ^U
  • Left one half screen width (or N positions): [N] ESC-) RightArrow
  • Right one half screen width (or N positions): [N] ESC-( LeftArrow
  • Right to last column displayed: ESC-} ^RightArrow
  • Left to first column: ESC-{ ^LeftArrow
  • Forward forever; like tail -f: F
  • Like F but stop when search pattern is found: ESC-F
  • Repaint screen: r ^R ^L
  • Repaint screen, discarding buffered input: R

Default "window" is the screen height. Default "half-window" is half of the screen height.

Searching

  • Search forward for (N-th) matching line: [N] /pattern
  • Search backward for (N-th) matching line: [N] ?pattern
  • Repeat previous search (for N-th occurrence): [N] n
  • Repeat previous search in reverse direction: [N] N
  • Repeat previous search, spanning files: [N] ESC-n
  • Repeat previous search, reverse dir. & spanning files: [N] ESC-N
  • Undo (toggle) search highlighting: ESC-u
  • Display only matching lines: [N] &pattern

A search pattern may be preceded by one or more of:

  • Search for NON-matching lines: ^N or !
  • Search multiple files (pass thru END OF FILE): ^E or *
  • Start search at FIRST file (for /) or last file (for ?): ^F or @
  • Highlight matches, but don't move (KEEP position): ^K
  • Don't use REGULAR EXPRESSIONS: ^R

Jumping

  • Go to first line in file (or line N): [N] g < ESC-<
  • Go to last line in file (or line N): [N] G > ESC->
  • Go to beginning of file (or N percent into file): [N] p %
  • Go to the (N-th) next tag: [N] t
  • Go to the (N-th) previous tag: [N] T
  • Find close bracket } ) ]: [N] { ( [
  • Find open bracket { ( [: [N] } ) ]
  • Find close bracket : [N] ESC-^F <c1> <c2>
  • Find open bracket : [N] ESC-^B <c1> <c2>

Each "find close bracket" command goes forward to the close bracket matching the (N-th) open bracket in the top line. Each "find open bracket" command goes backward to the open bracket matching the (N-th) close bracket in the bottom line.

  • Mark the current position with : [N] m<letter>
  • Go to a previously marked position: [N] '<letter>
  • Go to the previous position: [N] ''
  • Same as ': [N] ^X^X

A mark is any upper-case or lower-case letter. Certain marks are predefined:

  • ^ means beginning of the file
  • $ means end of the file

Changing files

  • Examine a new file: [N] :e [file]
  • Same as :e: [N] ^X^V
  • Examine the (N-th) next file from the command line: [N] :n
  • Examine the (N-th) previous file from the command line: [N] :p
  • Examine the first (or N-th) file from the command line: [N] :x
  • Delete the current file from the command line list: [N] :d
  • Print current file name: [N] = ^G :f

Miscellaneous commands

  • Toggle a command line option [see Options below]: [N] -<flag>
  • Toggle a command line option, by name: [N] --<name>
  • Display the setting of a command line option: [N] _<flag>
  • Display the setting of an option, by name: [N] __<name>
  • Execute the less cmd each time a new file is examined: [N] +cmd
  • Execute the shell command with $SHELL: [N] !command
  • Pipe file between current pos and mark X to shell command: [N] |Xcommand
  • Save input to a file: s file
  • Edit the current file with $VISUAL or $EDITOR: [N] v
  • Print version number of less: [N] V

Options

Most options may be changed either on the command line, or from within less by using the - or -- command. Options may be given in one of two forms: either a single character preceded by a -, or a name preceded by --.

  • Display help (from command line): -?, --help
  • Search skips current screen: -a, --search-skip-screen
  • Search starts just after target line: -A, --SEARCH-SKIP-SCREEN
  • Number of buffers: -b [N], --buffers=[N]
  • Don't automatically allocate buffers for pipes: -B, --auto-buffers
  • Repaint by clearing rather than scrolling: -c, --clear-screen
  • Dumb terminal: -d, --dumb
  • Set screen colors. (MS-DOS only): -D [xn.n], --color=xn.n
  • Quit at end of file: -e -E, --quit-at-eof --QUIT-AT-EOF
  • Force open non-regular files: -f, --force
  • Quit if entire file fits on first screen: -F, --quit-if-one-screen
  • Highlight only last match for searches: -g, --hilite-search
  • Don't highlight any matches for searches: -G, --HILITE-SEARCH
  • Backward scroll limit: -h [N], --max-back-scroll=[N]
  • Ignore case in searches that do not contain uppercase: -i, --ignore-case
  • Ignore case in all searches: -I, --IGNORE-CASE
  • Screen position of target lines: -j [N],--jump-target=[N]
  • Display a status column at left edge of screen: -J, --status-column
  • Use a lesskey file: -k [file],--lesskey-file=[file]
  • Exit less in response to Ctrl-C: -K,--quit-on-intr
  • Ignore the LESSOPEN environment variable: -L, --no-lessopen
  • Set prompt style: -m -M, --long-prompt --LONG-PROMPT
  • Don't use line numbers: -n -N, --line-numbers --LINE-NUMBERS
  • Copy to log file (standard input only): -o [file], --log-file=[file]
  • Copy to log file (unconditionally overwrite): -O [file], --LOG-FILE=[file]
  • Start at pattern (from command line): -p [pattern], --pattern=[pattern]
  • Define new prompt: -P [prompt], --prompt=[prompt]
  • Quiet the terminal bell: -q -Q, --quiet --QUIET --silent --SILENT
  • Output "raw" control characters: -r -R, --raw-control-chars --RAW-CONTROL-CHARS
  • Squeeze multiple blank lines: -s, --squeeze-blank-lines
  • Chop (truncate) long lines rather than wrapping: -S, --chop-long-lines
  • Find a tag: -t [tag], --tag=[tag]
  • Use an alternate tags file: -T [tagsfile], --tag-file=[tagsfile]
  • Change handling of backspaces: -u -U, --underline-special --UNDERLINE-SPECIAL
  • Display the version number of "less": -V, --version
  • Highlight first new line after forward-screen: -w, --hilite-unread
  • Highlight first new line after any forward movement: -W, --HILITE-UNREAD
  • Set tab stops: -x [N[,...]], --tabs=[N[,...]]
  • Don't use termcap init/deinit strings: -X, --no-init
  • Forward scroll limit: -y [N], --max-forw-scroll=[N]
  • Set size of window: -z [N], --window=[N]
  • Set shell quote characters: -" [c[c]], --quotes=[c[c]]
  • Don't display tildes after end of file: -~, --tilde
  • Horizontal scroll amount (0 = one half screen width): -# [N], --shift=[N]
  • Don't send termcap keypad init/deinit strings: --no-keypad
  • The F command changes files if the input file is renamed: --follow-name
  • Subsequent options use backslash as escape char: --use-backslash

Line editing

These keys can be used to edit text being entered on the "command line" at the bottom of the screen.

  • Move cursor right one character: RightArrow ESC-l
  • Move cursor left one character: LeftArrow ESC-h
  • Move cursor right one word: Ctrl-RightArrow ESC-RightArrow ESC-w
  • Move cursor left one word: Ctrl-LeftArrow ESC-LeftArrow ESC-b
  • Move cursor to start of line: HOME ESC-0
  • Move cursor to end of line: END ESC-$
  • Delete char to left of cursor: BACKSPACE
  • Delete char under cursor: DELETE ESC-x
  • Delete word to left of cursor: Ctrl-BACKSPACE ESC-BACKSPACE
  • Delete word under cursor: Ctrl-DELETE ESC-DELETE ESC-X
  • Delete entire line: Ctrl-U ESC (MS-DOS only)
  • Retrieve previous command line: UpArrow ESC-k
  • Retrieve next command line: DownArrow ESC-j
  • Complete filename & cycle: TAB
  • Complete filename & reverse cycle: SHIFT-TAB ESC-TAB
  • Complete filename, list all: Ctrl-L
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment