iT邦幫忙

2022 iThome 鐵人賽

DAY 11
1
Software Development

林姓商人與指令操作的秘密—談 Linux command line interface系列 第 11

Day 11:另一種閱讀文字檔案的方式:more、less

  • 分享至 

  • xImage
  •  

除了一次看完全部文字,或者用 head tail 看前面幾行或者後面幾行,有沒有更加類似一般文字編輯器的方式,讓我們可以一頁一頁看文字檔案的呢?

有的!我們還可以用 more 這個指令

NAME
       more - file perusal filter for crt viewing

我們試著用 more 這個指令來看 .bash_history

more .bash_history

剛打開時,可能會覺得一時之間很難以適應,按下上下左右都沒有反應,不知道該怎麼上下移動。

沒關係,這時候可以按 h 進行求救:

Most commands optionally preceded by integer argument k.  Defaults in brackets.
Star (*) indicates argument becomes new default.
-------------------------------------------------------
<space>                 Display next k lines of text [current screen size]
z                       Display next k lines of text [current screen size]*
<return>                Display next k lines of text [1]*
d or ctrl-D             Scroll k lines [current scroll size, initially 11]*
q or Q or <interrupt>   Exit from more
s                       Skip forward k lines of text [1]
f                       Skip forward k screenfuls of text [1]
b or ctrl-B             Skip backwards k screenfuls of text [1]
'                       Go to place where previous search started
=                       Display current line number
/<regular expression>   Search for kth occurrence of regular expression [1]
n                       Search for kth occurrence of last r.e [1]
!<cmd> or :!<cmd>       Execute <cmd> in a subshell
v                       Start up /usr/bin/vi at current line
ctrl-L                  Redraw screen
:n                      Go to kth next file [1]
:p                      Go to kth previous file [1]
:f                      Display current file name and line number
.                       Repeat previous command
-------------------------------------------------------

各位可以發現,指令工具很喜歡用非上下左右的按鍵進行移動。這需要一點適應的時間,不過習慣之後,會讓你的手持續保留在鍵盤的字母位置上,減少需要移動到方向鍵上的機會。

相對應的指令,還有一個:less

NAME
       less - opposite of more
DESCRIPTION
       Less  is  a  program  similar to more (1), 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 (1).  Less uses termcap (or terminfo on some systems), so it can run on a variety of termi‐
       nals.  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.)

使用的方式跟 more 不能說是很像,只能說是一模一樣

less .bash_history

不過,按下 h 的說明就很不同了


                   SUMMARY OF LESS COMMANDS

      Commands marked with * 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.

  h  H                 Display this help.
  q  :q  Q  :Q  ZZ     Exit.
-------------------------------------------------------

                           MOVING

  e  ^E  j  ^N  CR  *  Forward  one line   (or N lines).
  y  ^Y  k  ^K  ^P  *  Backward one line   (or N lines).
  f  ^F  ^V  SPACE  *  Forward  one window (or N lines).
  b  ^B  ESC-v      *  Backward one window (or N lines).
  z                 *  Forward  one window (and set window to N).
  w                 *  Backward one window (and set window to N).
  ESC-SPACE         *  Forward  one window, but don't stop at end-of-file.
  d  ^D             *  Forward  one half-window (and set half-window to N).
  u  ^U             *  Backward one half-window (and set half-window to N).
  ESC-)  RightArrow *  Left  one half screen width (or N positions).
  ESC-(  LeftArrow  *  Right one half screen width (or N positions).
  F                    Forward forever; like "tail -f".
  r  ^R  ^L            Repaint screen.

可以看到,less 的用法比起 more 操作方式多上很多。大家可以慢慢研究一下。

今天的說明有點多,就簡單介紹這兩個指令到這。希望大家都能試著玩看看不同的文字閱讀介面。


上一篇
Day 10:讀取檔案的能力!cat、head、tail
下一篇
Day 12:撰寫檔案,談 nano、vim
系列文
林姓商人與指令操作的秘密—談 Linux command line interface30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言