头图
Abstract :
I will sort out the various usages of less, which can also be used as a reference manual by the way.
<!--MORE-->

Introduction

less is a Linux command line utility for displaying file content. It supports pipeline input, so it can be used to receive the standard output of the previous command for full-screen display.

less is more , which has more comprehensive full-file navigation capabilities and full-text search capabilities. less is well optimized for opening large files, and has outstanding performance in real-time display and fast loading. less also supports opening multiple files at once, and it will display the contents of these files one by one.

It was named less because of a famous saying: less is more . This famous saying has been philosophized.

image-20211225100221467

Basic use

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

less /var/log/messages

The second is as the end of the pipeline operation:

ps aux|less

In the management operation, the real-time refresh mode equivalent to tail -f

dmsg|less +F

Used embedded and implicitly

In many places, less is used quietly through the PAGER environment variable.

A typical example is the man command. when:

man printf

At the time, the man page of printf will be displayed in the reading interface of less, like this:

image-20211225100928604

Reading interface operation

In the reading interface displayed by less, the keys are commands.

You can press h display the help screen q will exit the help screen.

The other most commonly used key commands are:

buttoninstruction
SPACENext page
dBottom half
bprevious page
uFirst half page
vEdit content
j or ↵ EnterNext line
kBefore ㄧ line
HomeTop of file
EndEnd of file
FFollow mode (for log use). Follow Mode (for logs). Interrupt to abort.
g or <first row
G or>Last line
⟨n⟩GSkip to line ⟨n⟩
/⟨text⟩Search forward ⟨text⟩ . The text will be treated as regular expression .
?⟨text⟩Like /, but for backward search.
nThe next matching search result
NLast matching search result
EscuTurn off compliance highlighting (see -g command line option)
-⟨c⟩Toggle option ⟨c⟩ , for example -i will switch whether to ignore case when searching
m⟨c⟩Set flag ⟨c⟩
'⟨c⟩Jump to mark ⟨c⟩
= Or Ctrl+GFile information
:nNext file
:pPrevious file
hinstruction. This will be less , and q will leave.
qLeave

We will fully introduce all available key commands in the keyboard section later.

search

In the reading interface of less, the bottom line is called the status command line. If you press the key in the interface, the status prompt of the line will be :

When you press / enter the search state, the state command line is waiting for you to continue typing pattern.

You can use regular patterns when searching.

Repeat the last search is n .

If you type in the -J , a vertical status bar will be displayed on the left side of the reading interface, and the pattern will be displayed in this status bar:

image-20211225103745594

Mark

The mark is a letter and is case sensitive. Marks are equivalent to our bookmarks in graphical text editors such as Visual Studio Code.

Press the ma key sequence in the reading interface, the m command will create a mark a for your current reading position. You can use 'a to return to this position later.

If you type the -J , a vertical status bar will be displayed on the left side of the reading interface, and the a mark will be displayed in this status bar:

image-20211225102328869

Options

Options can be specified from the command line, or can be directly typed and flipped - or --

For example, you can type -? to display the help screen (although h is 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, just use the + , so +F represents entering the rolling refresh mode, for example:

less +F /var/log/messages

Note that you can still use less command line-specific options. For example, -F means to exit less immediately when the file content is not enough for one screen.

If you use the ++ , then this command will be automatically applied to every file.

less can open multiple files at once and display them in sequence.

Main command line options

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

  -?  ........  --help
                  Display help (from command line).
  -a  ........  --search-skip-screen
                  Search skips current screen.
  -A  ........  --SEARCH-SKIP-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

Among them, the ones that may be used frequently are:

Optionsability
-X ........ --no-initThe init/deinit function of termcap is not used. The screen is not clear when exiting less.
-x [N[,...]] --tabs=[N[,...]]Set the display width of TAB characters
-n -N .... --line-numbers --LINE-NUMBERSDisplay/not display the line number. The line number is displayed in the status command line
-m -M .... --long-prompt --LONG-PROMPTThe style of the prompt to switch the status command line
-J ........ --status-columnDisplay a vertical status line on the left, the search result line can provide a mark display in this column
-F ........ --quit-if-one-screenIf the content is displayed less than one screen, immediately exit less
-e -E .... --quit-at-eof --QUIT-AT-EOFExit less immediately when it reaches the end of the file for the first time

There are two styles of status command line: short, medium. The former displays a : prompt, and the latter displays a series of document status information such as line number, display position percentage and so on.

-N in the reading interface, the line number will be displayed in the status command line.

-N used in the command line, the line number can be displayed on the left side of each line. for:

less -N /var/log/syslog

Will have:

image-20211225101246073

keyboard

Shortcut key ( less )

In the manual page reading interface, you can use shortcut keys to help reading.

Since the display of the manual page is done using the default PAGER in the operating system Shell environment, the shortcut keys follow the function mapping of the corresponding PAGER.

Most systems now use less as the default PAGER by default, so the less shortcut keys are introduced below, but the description text is corrected for manpage.

Basic operation

buttonability
hShow help screen
qquit
e ^E J ^N CRGo forward one line (or N lines)
y ^Y k ^K ^PGo back one line (or N lines)
f ^F ^V SPACEForward one screen
b ^B ESC-vGo back one screen
zGo forward one screen (set the screen height to N)
wGo back one screen (set the screen height to N)
ESC-SPACEGo forward one screen, scroll up at the end of the article
d ^DHalf screen forward
u ^UBack half screen
ESC-) RightArrowScroll right half screen
ESC-( LeftArrowScroll left half screen
ESC-} ^RightArrowScroll right to the far right
ESC-{ ^LeftArrowScroll left to the first column
FKeep rolling forward, like tail -f
ESC-FKeep scrolling forward, but stop scrolling when looking for a keyword match
r ^R ^LRedraw the screen
RRedraw the screen and discard the input that has been cached

The command with N allows the input method of N cmd

Take the f command as an example. Simply type f the screen will scroll down and the content will move up one line; but you can type 31f make the content scroll up 31 lines at a time. The opposite is the b command, which scrolls up the screen and moves the content down one line.

The F command has the ability to refresh the content of the file instantly, which is equivalent to the enhanced version of tail -f . It can not only refresh and display the latest file content at any time, but also temporarily stop refreshing, navigate and retrieve the existing content, and continue Use F again to enter the instant refresh state.

search

buttonability
/patternSearch forward for the keyword pattern
?patternSearch backward for the keyword pattern
nRepeat the last search (repeat N times)
NRepeat the last search in reverse
ESC-nRepeat the previous search, allowing to span multiple files
ESC-NRepeat the previous search, reverse, allowing to span multiple files
ESC-uAbandon, restore, and flip the search highlight state
&patternShow only matching line text

A pattern can be prefixed by the following keys (ASCII characters):

buttonability
^N or !Search for non-matching rows
^E or *Search multiple files (beyond the end of the file for the current subject)
^F or @Start the search from the first file ( / ) or the last file ( ?
^KHighlight matching text, but do not move (keep the current reading position unchanged)
^RNo regular expression

Jump

buttonability
g < ESC-<The first line of the file (or Nth line)
G > ESC->The last line of the file (or the last Nth line)
p %Go to the beginning of the file (or locate to N by percentage)
tGo to the next mark (repeat N times)
TGo to the previous mark (repeat N times)
[ ( {Find closing bracket})]
} ) ]Find the opening bracket [({
ESC-^F (c1) (c2)Find the closing parenthesis (c2)
ESC-^B (c1) (c2)Find open parenthesis (c1)

g such a key can be directly effective in the reading interface. But you can also press the key continuously, for example, 1 3 g can go to the 13th line.

buttonability
m<letter>Mark the current position as <letter>
'<letter>Jump to the previous marked position (indexed <letter>
''Jump to the previous marked position
^X^XEquivalent to'

A mark is an uppercase or lowercase letter. In addition, the following are the predefined marker positions:

  • ^ file start
  • $ end of file

Switch file

buttonability
:e [file]Load a new file
^X^VEquivalent to: e
:nThe next (last N) file in the list
:pThe previous (upper N) file in the list
:xThe first (Nth) file in the list
:dDelete the current file from the command line list
= ^G :fPrint the current file name

Miscellaneous

buttonability
-<flag>Flip a command line option
--<name>Flip an option
_<flag>Show a command line option
__<name>Show an option
+cmdWhenever a new file is loaded and interpreted, execute the less command cmd
!commandExecute shell command command
\XcommandPipeline operation, from the current position and the marked position X to the shell command command
s fileSave input to file
vEdit the current file (use $VISUAL or $EDITOR )
VPrint less version number

Environment variable

PAGER

less is now the default value of PAGER for most Linux distributions. Therefore, when PAGER is not explicitly set, the system always uses it.

export PAGER=less

LESS

You can specify preset command line options less

LESS="-X"; export LESS

This makes the screen not clear after less exits.

Line edit

In the less reading interface, the key input will be displayed on the bottom line of the screen, and the line editing mode will be entered at the same time. Sometimes you can press : like vi to enter the command line editing mode.

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

 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.

refer to

🔚


hedzr
95 声望19 粉丝