Linux command less omniscient plenary session

InfoQ 2022-02-13 08:05:49 阅读数:857

linux command omniscient plenary session
Yes  less  Sort out the various usages of , By the way, it can also be used as a reference manual .


  It's a  Linux  Command line utility , Used to display the contents of a file , It supports pipeline input , So it can be used to receive the standard output of the previous command for full screen display .

  Enhanced version of , It has a more comprehensive full file navigation capability , And full text search capabilities .
  It has good optimization in opening large files , In real time display 、 Outstanding performance in terms of fast loading .
  It also supports opening multiple files at once , It will show the contents of these files one by one .

  Also because of a famous saying :
less is more
. This famous saying has been philosophized .


Basic use

less  There are two basic ways to use , One is to open one or more files :

less /var/log/messages

Second, as the end of pipeline operation :

ps aux|less

When managing operations , May often be used  
tail -f
  Equivalent real-time refresh mode :

dmsg|less +F

Embedded 、 Implicitly use

In many places  less  Passed  PAGER  Use environment variables quietly .

A typical example is  man  command . When :

man printf

when ,printf  The man page will be displayed in  less  In the reading interface , Like this :


Reading interface operation

stay  less  In the displayed reading interface , Press the key to command .

You can press... At any time  
  Display the help screen ,
  Will exit the help screen .

Other key commands most commonly used are :

Later in the keyboard section, we will fully introduce all available key commands .

Search for

stay  less  In the reading interface , The bottom line is called the status command line . If you press the button in the interface , The status prompt of this line is temporarily hidden , with  
  The command line input at the prompt will be displayed .

When you press  
  When entering the search state , The status command line waits for you to continue typing  pattern.

You can use regular expressions when searching  pattern.

Repeat the last search is  

If you type  
  Sequence , Then a vertical status bar will be displayed on the left side of the reading interface , and  
  The matching line will be displayed in the status bar :


Mark (Mark)

The mark is a letter , Case sensitive . Marking is equivalent to us in  Visual Studio Code  Bookmarks in such a graphical text editor .

Press... On the reading interface  ma  Key sequence ,
  The command will create a name for your current reading location  
  The tag . Later you can use  
  Return to this location .

If you type  
  Sequence , Then a vertical status bar will be displayed on the left side of the reading interface , and  
  The tag will be displayed in the status bar :



Options can be specified from the command line , It can also be in  less  Read the character sequence through prefix in the interface  
  Type and flip directly .

For example, you can type  
  To display the help screen ( Even though  
  It's easier ).

less [-[+]aABcCdeEfFgGiIJKLmMnNqQrRsSuUVwWX~]
 [-b space] [-h lines] [-j line] [-k keyfile]
 [-{oO} logfile] [-p pattern] [-P prompt] [-t tag]
 [-T tagsfile] [-x tab,...] [-y lines] [-[z] lines]
 [-# shift] [+[+]cmd] [--] [filename]...

less  Allows you to type commands directly from the command line , Use  
  The prefix is enough , therefore  
  It means entering the scroll refresh mode , for example :

less +F /var/log/messages

Note that you can still use  less  Command line specific options , for example  
  It means that when the file content is less than one screen, exit immediately  less.

If you use  
  Prefix , Then the command will be automatically applied to each file .

  You can open multiple files at once and display them in turn .

Main command line options

The valid command line specific options are roughly as follows ( Because the description text is very simple, keep the original text ):

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

Common options

Which may be often used may be :

The status command line has two styles :short, medium. The former shows a  
  Prompt , The latter displays a series of document status information, such as line number 、 Display position percentage, etc .

If used in the reading interface  
, Then the line number will be displayed in the status command line .

If you use... On the command line  
, Then the line number can be displayed on the left side of each line . about :

less -N /var/log/syslog

There will be :



Shortcut key (


In the man page reading interface , You can use shortcut keys to help you read .

Because the man page is displayed using the operating system  Shell  Default in the environment  PAGER  To complete , So the shortcut keys follow the corresponding  PAGER  Functional mapping of .

Most systems now use... By default  
  As default  PAGER, So here's how  
  Shortcut key , But aim at  manpage  Fix description text .
Basic operation

  The order of , allow  
  Input method of .

  Command as an example , Simply type  
  Then the screen scrolls down , Move the content up one line ; But you can type  
  Make the content scroll up at one time  31  That's ok . On the contrary  
  command , You can roll the screen up , Move the content down one line .

F  The command has the ability to refresh the contents of the file immediately , It's equivalent to an enhanced version of  
tail -f
  function , Not only can you refresh and display the latest file content at any time , You can also temporarily stop refreshing , Navigate and retrieve existing content , And it can be used again  F  Enter the status of instant refresh .
Search for

  You can use the following keys (ASCII character ) Prefix modification :


g  This kind of button , It can take effect directly in the reading interface . But you can also press keys continuously , for example  1 3 g  You can go to page  13  That's ok .

A marked position (mark) Is a capital letter or a lowercase letter . in addition , Here are the predefined marker locations :

  • ^
      File start
  • $
      End of file
Switch files


environment variable


  Now it's most  Linux  Distribution  PAGER  The default value is . So when  PAGER  When there is no explicit setting , The system always uses it .

export PAGER=less


You can have the  
  Specify preset command line options , for example :

LESS="-X"; export LESS

This makes  less  The screen will not clear after exiting .

Line edit

less  In the reading interface , The key input will be displayed in the bottom line of the screen , At the same time, enter the line editing mode . Sometimes you can be like  vi  Same press  
  Enter the command line edit mode .

The following is the keyboard operation method of this line editing ( Because the description text is very simple, keep the original text ).

 RightArrow ..................... ESC-l ... Move cursor right one character.
 LeftArrow ...................... ESC-h ... Move cursor left one character.
 ctrl-RightArrow ESC-RightArrow ESC-w ... Move cursor right one word.
 ctrl-LeftArrow ESC-LeftArrow ESC-b ... Move cursor left one word.
 HOME ........................... ESC-0 ... Move cursor to start of line.
 END ............................ ESC-$ ... Move cursor to end of line.
 BACKSPACE ................................ Delete char to left of cursor.
 DELETE ......................... ESC-x ... Delete char under cursor.
 ctrl-BACKSPACE ESC-BACKSPACE ........... Delete word to left of cursor.
 ctrl-DELETE .... ESC-DELETE .... ESC-X ... Delete word under cursor.
 ctrl-U ......... ESC (MS-DOS only) ....... Delete entire line.
 UpArrow ........................ ESC-k ... Retrieve previous command line.
 DownArrow ...................... ESC-j ... Retrieve next command line.
 TAB ...................................... Complete filename & cycle.
 SHIFT-TAB ...................... ESC-TAB Complete filename & reverse cycle.
 ctrl-L ................................... Complete filename, list all.

Reference resources

  • man less
  • less (Unix) - Wikipedia

copyright:author[InfoQ],Please bring the original link to reprint, thank you.