Try   HackMD

SE6023 Lab2 快快樂樂學Vim

tags: hadoop

在接下來的課程中會大量在linux上操作,而作業要求之一即在linux上編輯文件。因此,以下我們會引導大家在linux上使用vim編輯器。

Install

sudo apt install vim

編輯文件

Vim有三種模式

  1. 命令模式(Command mode
  2. 插入模式(Insert mode
  3. 底線命令模式(Last line mode)。

當用戶處於不同模式的時候,敲擊鍵盤會產生不同的作用。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

不論在insert mode或是last line mode按下esc都會回到command mode

如何切到insert/last line模式

使用者一開始進入vim便進入到command mode

  • 切到insert mode就是按i,a或是insert
  • 切到last line mode就按:

last line mode

如何存檔

w

如何離開

q

command mode

在vim中有許多加速作業的指令,值得學習。

存檔離開

ZZ

移動游標

  • gg:移到第一行
  • G:移到最後一行

刪除文字

  • dG:刪除到最後一行
  • dgg:刪除到第一行
  • dd:刪除一行
  • [n]dd:刪除n行

word以空白為區隔

  • dw:從游標開始刪除一個word
  • d[n]w:從游標開始刪除n個word
  • x:刪除一個character
  • [x]x:刪除n個character

刪除的文字會被寫進buffer,可以用以下按鍵貼上

  • p:貼在游標後
  • P:貼在游標前

復原

  • u

取消復原Ctrl+R

搜尋

*:搜尋目前游標單字
/:(自行輸入)
gd:搜尋第一個出現的
g*:搜尋所有

n:下一個搜尋結果
N:上一個搜尋結果

以16進位編輯檔案

  • :%!xdd
  • :%!xdd -r

: command-line mode
% ex command-line ranges,將整份文件進行轉換
! filter commands,filter是一個小程式,將標準輸出經過轉換後輸出,這裡使用xxd作為filter command

如何切換行號顯示

last line mode下鍵入
set nu
set nonu

貼上時文字跑板?

先在command mode下鍵入set paste再貼上文字

crlf/lf互轉

In vim, use :set ff=unix to convert to Unix; use :set ff=dos to convert to Windows.


命令列模式常用技巧

  • 瀏覽檔案

    ​​​​vim .
    ​​​​:e .
    
  • 分頁模式

    ​​​​:tabe .
    ​​​​:tabe [filename]
    
    • 使用gt/gT進行分頁切換
    • :close/:q關閉分頁
  • 換行/換頁

    • PageDown/PageDown == ^b/^f**
    • ^u/^d 向上/下半頁
    • ^y/^e 向上/下一行
    • num+方向鍵=跳行,對應如下
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
      H J K L
  • 定位指令

    • zz, zt, zb...

      • z center
      • t top
      • b bottom
    • 水平移動

      • 0/$
      • ^(shift)/g_ 移動到有文字的最前/後
    • 水平移動 II

      • w / e: next 開頭結尾
      • b: prev 單字開頭
      • WEB功能類似,以blank為區隔
    • 水平查找

      • f+[char] find-
      • t+[char] to-
      ​​​​​​​​apple banana apple ...
      ​​​​​​​​:fa
      ​​​​​​​​:ta
      

      大寫反向查找,前方也可加上數值

  • 跳行 :[number] / [number] G

; 重複上次指令

插入模式

  • I 行首
  • A 行尾
  • O 上一行+行首
  • S 取代+行首
  • C 取代(從目前字元)+行首
# 插入雙引號
I" + A"

普通模式

yy: copy 單行模式
p/P: 貼上
<< / >>:縮排
U: 一次性回復單行
^a / ^x: 遞增遞減數字

. 重複上次指令

Reference