less命令功能详解

less -?
less --help
less -V
less --version
less [-[+]aBcCdeEfFgGiIJKLmMnNqQrRsSuUVwWX~] [-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]... 

  Description: Less is a program similar to more, but which allows backward movement in the file as well as forward movement. Also, less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi. Less uses termcap (or terminfo on some systems), so it can run on a variety of terminals. There is even limited support for hardcopy terminals. (On a hardcopy terminal, lines which should be printed at the top of the screen are prefixed with a caret.) 简单点说就是less其实和more差不多,但是多了向前翻页的功能。另外还和其他命令做了简单的比较。

      less命令的Option什么的太多了,一口老血喷出来啊……但其实我都没有用到过这些,用得比较多的还是打开文件后的一些功能键,如翻页啊之类的。这里只放翻译吧,英语原文放出来太乱了。我觉得重点在打开文件后的command上,(点击直接跳至command)

  -? or --help,  这个大家都知道,显示帮助文件~
  -a or --search-skip-screen, 因为less命令是边搜索边显示的,所以速度会比较快,默认情况下是从第二行开始搜索,而用了-a以后会从当前显示的最后一行开始搜索,从而跳过当前显示的所有行。
  -bn or --buffers=n,  控制缓存区的大小,若n为-1则为无限大,这时候可以同时将整个文件读入内存,n的单位为k。
  -B or --auto-buffers,  默认情况下,从管道读取时,数据会一次性输入,这就可能导致占用内存过大的情况,这里的-B表示默认占用64k的内存,而这也可能导致输出问题,比如最近的一次输出会保存在内存中,但是更久一点的就可能丢失了。
  -c or --clear-screen, 移动最上一行时就重绘屏幕。
  -C or --CLEAR-SCREEN, 和-c一样,兼容老版本.
  -d or --dumb, 如果终端机是dumb的(话说这里的什么样的终端是dumb的?),-d选项会禁止错误信息的正常显示。所以会丢失很多重要的功能,如清屏或者向回滚动。
  -Dxcolor or --color=xcolor, [MS-DOS only]  这是设置文字显示颜色及样式的,x可以为n,s,d,u,k,而color是二组数字,用'.'分隔,前者表前景色,后者为背景色。(我没测试出来这个)
  -e or --quit-at-eof,  第二次到达文件结尾时自动退出less。
  -E or --QUIT-AT-EOF,第一次到达文件结尾的时候退出less。
  -f or --force, 强制打开如目录或设备文件等的非ASCII文件。
  -F or --quit-if-one-screen, 若文件只有一屏,自动退出less。
  -g or --hilite-search,  默认情况下,在less中做字段搜索时,所有符合条件的字段都会被高亮显示,使用-g的话,只会显示被搜索到的当前字段。
  -G or --HILITE-SEARCH, 搜索字段时不高亮显示任何字段。
  --old-bot, 有的终端在一行命令过长的情况下,不会自动换行输入,而是从头覆盖。之前输入的就被后面输入的覆盖率。为了避免这种情况才有了--old-bot选项的。
  -hn or --max-back-scroll=n, 设置回退的最大行数。如果要回退超过n行时,屏幕会重绘。
  -i or --ignore-case, 当搜索的字段均为小写时,搜索结果不区分大小写。
  -I or --IGNORE-CASE, 搜索字段不区分大小写。
  -jn or --jump-target=n, 设置target行,target行是搜索时搜索结果所在的行,如在第一行搜到结果,但是jump-target是9,那么会将第一行显示在第九行的位置,前面均为空白行。(这是我自己跑出来的结果,欢迎纠正)。n可以是正数、负数或者以.开头的小数,最上面一行是1,第二行是2,以此类推,最小面一行是-1,倒数第二行是-2……小数则表示十分之几。
  -J or --status-column, 显示状态列,状态列将显示匹配搜索结果的行。
  -kfilename or --lesskey-file=filename, Causes less to open and interpret the named file as a lesskey (1) file. Multiple -k options may be specified. If the LESSKEY or LESSKEY_SYSTEM environment variable is set, or if a lesskey file is found in a standard place (see KEY BINDINGS), it is also used as a lesskey file. 
  -K or --quit-on-intr, 通常情况下Ctrl-c会暂停less界面中当前执行的命令,并返回到命令提示,使用-K以后,Ctrl-c将导致直接退出less命令。
  -L or --no-lessopen,  忽视LESSOPEN环境变量。-L也可在less界面中使用,但是只对底下要打开的文件起作用,对当前打开的文件无效。
  -m or --long-prompt, 显示和more一样的提示。
  -M or --LONG-PROMPT, 显示比more更长的提示。
  -n or --line-numbers, 禁用line number。默认我们可以从提示或者通过=命令来显示行号,并且通过v命令将当前行号传给编辑器,禁用line number后对于大文件的查看速度会有所提高。
  -N or --LINE-NUMBERS, 每行开头显示行号。
  -ofilename or --log-file=filename, -o会将将查看内容复制到指定文件filename中,只对管道有效。如果指定文件已存在,less会询问是否进行覆盖。
  -Ofilename or --LOG-FILE=filename, 与-o一样,不过覆盖已存在文件时不会进行询问。
  -ppattern or --pattern=pattern, 告诉tell从第一个pattern开始显示。
  -Pprompt or --prompt=prompt, 根据个人喜好选择prompt。这个option会将值设置进LESS这个环境变量中,不需要每次使用less命令时都进行设置。
-Ps 后跟字符串可把默认(short)提示改成当前字符串,-Pm修改的是medium (-m) 提示,-PM 修改的是long (-M) 提示,而-Ph修改的是帮助界面的提示。 -P= 修改的是=命令之后的提示. -Pw修改的是F命令时等待过程中显示的提示.
  
-q or --quiet or --silent, 安静模式。但是只有试图滚动到文件范围以外的时候(如试图从最后一行向下滚动,或从第一行向上滚动)不会响铃,其余错误仍会有警示音。
  -Q or --QUIET or --SILENT, 完全静音,任何错误都不发出警示音。
  -r or --raw-control-chars, 默认情况下,ctrl是用^显示,如control-A会显示成^A,用了这个option就只能乖乖把control显示出来了。
  -R or --RAW-CONTROL-CHARS, 和r相似,但是只有ANSI颜色会用raw形式。
  -s or --squeeze-blank-lines, 将多个空白行压缩为一行。
  -S or --chop-long-lines, 超过屏幕长度的行,超出部分会被舍弃,而不是折行。
  -u or --underline-special, 显示返回键和回车键。
  -U or --UNDERLINE-SPECIAL, 显示backspace,tab,carriage return。
  -V or --version, 显示版本信息。
  -w or --hilite-unread, 高亮显示第一行。这个第一行是表示接在之前最后一行后面的那行。
  -W or --HILITE-UNREAD, 同w,但是这个第一行之每次向后滚动显示出来的第一行。
  -xn,... or --tabs=n,... 设置tab的间距,如果只设置一个n,那tab会停在n的倍数上。如果设置一系列数字,tab会停在相应数字对应的位置,然后继续加上前两个数值之差,如-x9,17,tabs所停留的位置一次会是:9,17,25,33……
  -yn or --max-forw-scroll=n, 设置向前滚动的最大行数,当向前滚动行数超过n行时,重绘屏幕。
  -[z]n or --window=n, 将默认滚动行数改成n行。
  -dqcc or --quotes=cc, 修改括住文件名的引号。通常在文件名中包含引号或空格时使用。如跟一个字符时,引号开头结尾都用该字符代替,如跟两个字符,则引号开头用第一个字符代替,结尾用第二个字符代替。
  -~ or --tilde, 默认情况下最后一行后每行都显示成~,这个option的作用就是不显示~而显示成空白行。
  -# or --shift, 指定水平移动的距离。如果设为0,则默认移动半页。也可设置为小数。
  --no-keypad, 禁用键盘右边的小键盘上的功能键。
  --follow-name, 默认情况下,如果文件在F命令执行的时候被重命名了,less会无视它名字的修改,继续显示原文件内容,但是若使用了--follow-name,less会周期性地按指定的文件名打开文件,若文件被成功打开并且内容发生了改变(说明一个旧文件被改名或者删除了,并且生成了同样文件名的新文件),那么less会显示新文件的内容。
  --, 单独一个--表示option结束了,后面跟的会被默认认为是文件名。这个常用于文件名以-或者+开头的文件的查看。
  +, 这个我觉得很实用,可在+后跟一个你希望进入less后执行的第一个命令,如+G表示从最后一行显示,+/xyz表示从搜索到的第一个xyz开始显示。

  另外,还有几句不懂的,求解:

  -X or --no-init,禁止向终端发送termcap initialization和deinitialization strings
  -ttag or --tag=tag, The -t option, followed immediately by a TAG, will edit the file containing that tag. For this to work, tag information must be available; for example, there may be a file in the current directory called "tags", which was previously built by ctags (1) or an equivalent command. If the environment variable LESSGLOBALTAGS is set, it is taken to be the name of a command compatible with global (1), and that command is executed to find the tag. (See http://www.gnu.org/software/global/global.html). The -t option may also be specified from within less (using the - command) as a way of examining a new file. The command ":t" is equivalent to specifying -t from within less.
  -Ttagsfile or --tag-file=tagsfile, Specifies a tags file to be used instead of "tags".

  下面是显示文件时的一些可执行操作(^v表示Ctrl-v, ESC-SPACE表示先按下ESC键再按下空白键):

h or H

Help: display a summary of these commands. If you forget all the other commands, remember this one. 显示commands的概要
SPACE or ^V or f or ^F Scroll forward N lines. If N is more than the screen size, only the final screenful is displayed. 向下滚动N行
z Like SPACE, but if N is specified, it becomes the new window size. 向下滚动一页,设置n值
ESC-SPACE Like SPACE, but scrolls a full screenful, even if it reaches end-of-file in the process. 向下滚动一屏
RETURN or ^N or e or ^E or j or ^J Scroll forward N lines, default 1. The entire N lines are displayed, even if N is more than the screen size. 向下滚动n行,默认为1,如果n大于屏幕行数,仍然全部显示

d or ^D

Scroll forward N lines, default one half of the screen size. If N is specified, it becomes the new default for subsequent d and u commands. 下前滚动n行,默认半页
b or ^B or ESC-v Scroll backward N lines, default one window. If N is more than the screen size, only the final screenful is displayed. 向前上动n行,默认一屏
w Like ESC-v, but if N is specified, it becomes the new window size. 同ESC-v,但是如果n值设置过,则重设n值
y or ^Y or ^P or k or ^K Scroll backward N lines, default 1. The entire N lines are displayed, even if N is more than the screen size. Warning: some systems use ^Y as a special job control character. 向上滚动n行,默认为1.
u or ^U Scroll backward N lines, default one half of the screen size. If N is specified, it becomes the new default for subsequent d and u commands. 向上滚动n行
ESC-) or RIGHTARROW Scroll horizontally right N characters, default half the screen width (see the -# option). If a number N is specified, it becomes the default for future RIGHTARROW and LEFTARROW commands. While the text is scrolled, it acts as though the -S option (chop lines) were in effect. 水平向右n个字符,默认半屏
ESC-( or LEFTARROW Scroll horizontally left N characters, default half the screen width (see the -# option). If a number N is specified, it becomes the default for future RIGHTARROW and LEFTARROW commands. 水平相左移动n个字符,默认半屏
r or ^R or ^L Repaint the screen. 重绘屏幕
R

Repaint the screen, discarding any buffered input. Useful if the file is changing while it is being viewed. 重绘屏幕

F Scroll forward, and keep trying to read when the end of file is reached. Normally this command would be used when already at the end of the file. It is a way to monitor the tail of a file which is growing while it is being viewed. (The behavior is similar to the "tail -f" command.) 向下滚动,即使已经到了文件结尾(类似于tail -f)
g or < or ESC-< Go to line N in the file, default 1 (beginning of file). (Warning: this may be slow if N is large.) 跳至n行,默认第一行
G or > or ESC-> Go to line N in the file, default the end of the file. (Warning: this may be slow if N is large, or if N is not specified and standard input, rather than a file, is being read.) 跳至n行,默认最后一行
p or % Go to a position N percent into the file. N should be between 0 and 100, and may contain a decimal point. 跳至N%的位置
P Go to the line containing byte offset N in the file. 跳到第n个字符所在的行
{ If a left curly bracket appears in the top line displayed on the screen, the { command will go to the matching right curly bracket. The matching right curly bracket is positioned on the bottom line of the screen. If there is more than one left curly bracket on the top line, a number N may be used to specify the N-th bracket on the line. 找到匹配第一个{的}所在的行
} If a right curly bracket appears in the bottom line displayed on the screen, the } command will go to the matching left curly bracket. The matching left curly bracket is positioned on the top line of the screen. If there is more than one right curly bracket on the top line, a number N may be used to specify the N-th bracket on the line. 找到匹配最后一个}的{的行
( Like {, but applies to parentheses rather than curly brackets. 同{
) Like }, but applies to parentheses rather than curly brackets. 同}
[ Like {, but applies to square brackets rather than curly brackets. 同{
] Like }, but applies to square brackets rather than curly brackets. 同}
ESC-^F Followed by two characters, acts like {, but uses the two characters as open and close brackets, respectively. For example, "ESC ^F < >" could be used to go forward to the > which matches the < in the top displayed line. 同{,但是可以自己设置起始和结尾字符
ESC-^B Followed by two characters, acts like }, but uses the two characters as open and close brackets, respectively. For example, "ESC ^B < >" could be used to go backward to the < which matches the > in the bottom displayed line. 同},但是各自用两个字符作为起始和结尾
m Followed by any lowercase letter, marks the current position with that letter. 用指定字符标记当前位置
' (Single quote.) Followed by any lowercase letter, returns to the position which was previously marked with that letter. Followed by another single quote, returns to the position at which the last "large" movement command was executed. Followed by a ^ or $, jumps to the beginning or end of the file respectively. Marks are preserved when a new file is examined, so the ' command can be used to switch between input files. 跳至指定标记所在位置。
^X^X Same as single quote. 同'
/pattern

Search forward in the file for the N-th line containing the pattern. N defaults to 1. The pattern is a regular expression, as recognized by the regular expression library supplied by your system. The search starts at the second line displayed (but see the -a and -j options, which change this). 向后搜索
Certain characters are special if entered at the beginning of the pattern; they modify the type of search rather than become part of the pattern:

^N or !: Search for lines which do NOT match the pattern. 查找不匹配的行
^E or *: Search multiple files. That is, if the search reaches the END of the current file without finding a match, the search continues in the next file in the command line list. 在多个文件中搜索
^F or @: Begin the search at the first line of the FIRST file in the command line list, regardless of what is currently displayed on the screen or the settings of the -a or -j options. 从第一个文件的第一行开始搜索
^K: Highlight any text which matches the pattern on the current screen, but don't move to the first match (KEEP current position). 高亮显示搜索结果但不移动到第一个匹配结果去
^R: Don't interpret regular expression metacharacters; that is, do a simple textual comparison. 不支持正则表达式

以上的使用方法是输在/和pattern之间

?pattern

Search backward in the file for the N-th line containing the pattern. The search starts at the line immediately before the top line displayed. 向前搜索
Certain characters are special as in the / command:

^N or !: Search for lines which do NOT match the pattern. 查找不匹配的行
^E or *: Search multiple files. That is, if the search reaches the beginning of the current file without finding a match, the search continues in the previous file in the command line list. 在多个文件中搜索
^F or @: Begin the search at the last line of the last file in the command line list, regardless of what is currently displayed on the screen or the settings of the -a or -j options. 从最后一个文件的最后一行开始搜索
^K: As in forward searches. 高亮显示结果但是不移至第一个搜索结果
^R: As in forward searches. 不支持正则表达式

ESC-/pattern Same as "/*". 同/*
ESC-?pattern Same as "?*". 同?*
n Repeat previous search, for N-th line containing the last pattern. If the previous search was modified by ^N, the search is made for the N-th line NOT containing the pattern. If the previous search was modified by ^E, the search continues in the next (or previous) file if not satisfied in the current file. If the previous search was modified by ^R, the search is done without using regular expressions. There is no effect if the previous search was modified by ^F or ^K. 下一个搜索结果
N Repeat previous search, but in the reverse direction. 与n反方向,下一个搜索结果
ESC-n Repeat previous search, but crossing file boundaries. The effect is as if the previous search were modified by *. 下一个搜索结果,支持文件之间的跳跃
ESC-N Repeat previous search, but in the reverse direction and crossing file boundaries. 同ESC-n,不过为反方向
ESC-u Undo search highlighting. Turn off highlighting of strings matching the current search pattern. If highlighting is already off because of a previous ESC-u command, turn highlighting back on. Any search command will also turn highlighting back on. (Highlighting can also be disabled by toggling the -G option; in that case search commands do not turn highlighting back on.) 关闭当前搜索的高亮显示,若高亮之前被ESC-u关闭,则还原之前设置
&pattern

Display only lines which match the pattern; lines which do not match the pattern are not displayed. If pattern is empty (if you type & immediately followed by ENTER), any filtering is turned off, and all lines are displayed. While filtering is in effect, an ampersand is displayed at the beginning of the prompt, as a reminder that some lines in the file may be hidden.只显示匹配pattern的行,若pattern为空,会显示所有行

Certain characters are special as in the / command:

^N or !: Display only lines which do NOT match the pattern. 仅显示没有匹配的行
^R: Don't interpret regular expression metacharacters; that is, do a simple textual comparison.不支持正则表达式

:e [filename] Examine a new file. If the filename is missing, the "current" file (see the :n and :p commands below) from the list of files in the command line is re-examined. A percent sign (%) in the filename is replaced by the name of the current file. A pound sign (#) is replaced by the name of the previously examined file. However, two consecutive percent signs are simply replaced with a single percent sign. This allows you to enter a filename that contains a percent sign in the name. Similarly, two consecutive pound signs are replaced with a single pound sign. The filename is inserted into the command line list of files so that it can be seen by subsequent :n and :p commands. If the filename consists of several files, they are all inserted into the list of files and the first one is examined. If the filename contains one or more spaces, the entire filename should be enclosed in double quotes (also see the -" option). 执行新文件,若文件名缺失,则当前文件重新执行
^X^V or E Same as :e. Warning: some systems use ^V as a special literalization character. On such systems, you may not be able to use ^V. 同e命令
:n Examine the next file (from the list of files given in the command line). If a number N is specified, the N-th next file is examined. 执行后一个文件,如果n是指定的值,那么会执行后第n个文件
:p Examine the previous file in the command line list. If a number N is specified, the N-th previous file is examined. 执行前一个文件,如果n是指定的值,那么会之情第前n个文件
:x Examine the first file in the command line list. If a number N is specified, the N-th file in the list is examined. 执行第一个文件,如果n是指定的值,那么会执行第n个文件
:d Remove the current file from the list of files. 从打开的文件列表中删除当前文件
t Go to the next tag, if there were more than one matches for the current tag. See the -t option for more details about tags. 移至下一个tag
T Go to the previous tag, if there were more than one matches for the current tag. 移至前一个tag
= or ^G or :f Prints some information about the file being viewed, including its name and the line number and byte offset of the bottom line being displayed. If possible, it also prints the length of the file, the number of lines in the file and the percent of the file above the last displayed line. 打印当前文件的相关信息:文件名、行数等
- Followed by one of the command line option letters, this will change the setting of that option and print a message describing the new setting. If a ^P (CONTROL-P) is entered immediately after the dash, the setting of the option is changed but no message is printed. If the option letter has a numeric value (such as -b or -h), or a string value (such as -P or -t), a new value may be entered after the option letter. If no new value is entered, a message describing the current setting is printed and nothing is changed. 改变option的设置并打印消息,option后要追加value,若没有value,不会改变option的值,只会打印出当前值
-- Like the - command, but takes a long option name (see OPTIONS below) rather than a single option letter. You must press RETURN after typing the option name. A ^P immediately after the second dash suppresses printing of a message describing the new setting, as in the - command. 同-命令,但是用长option代替
-+ Followed by one of the command line option letters this will reset the option to its default setting and print a message describing the new setting. (The "-+X" command does the same thing as "-+X" on the command line.) This does not work for string-valued options. 恢复option默认值,并打印信息
--+ Like the -+ command, but takes a long option name rather than a single option letter. 同-+command,但是用长option代替
-! Followed by one of the command line option letters, this will reset the option to the "opposite" of its default setting and print a message describing the new setting. This does not work for numeric or string-valued options. 做相反设置,并打印信息
--! Like the -! command, but takes a long option name rather than a single option letter. 和-!相同,用长option代替短的
_ (Underscore.) Followed by one of the command line option letters, this will print a message describing the current setting of that option. The setting of the option is not changed. 打印出option的说明
__ (Double underscore.) Like the _ (underscore) command, but takes a long option name rather than a single option letter. You must press RETURN after typing the option name. 同_,但是用长option名。输完option名之后按下RETURN键
+cmd Causes the specified cmd to be executed each time a new file is examined. For example, +G causes less to initially display each file starting at the end rather than the beginning. 每次less打开一个新文件时执行的命令
V Prints the version number of less being run. 显示版本号
q or Q or :q or :Q or ZZ Exits less. 退出

   

posted @ 2013-09-08 20:35  geeky_jane  Views(1927)  Comments(0Edit  收藏  举报