(Linux) Vi & Vim 泛用指令收集 (學習筆記 — 1)

Steven Lu
3 min readJan 16, 2022
Vim photo

簡短介紹vi & vim的背景, vi 是一種使用在Unix & UN*X 作業系統上的文字編輯器,而vim (vi IMproved)相當於vi 的改良版。

剛開啟腳本檔案時,輸入 vi or vim (<filename.sh>) ,即可編輯腳本。

starting syntax of editing shell script

進入腳本編輯介面之後,會呈現下圖的介面。

Default of vim editor page

接下來,就是使用以下常用的指令來進行檔案編輯:

gg : 移至頁面頂端。gg stands for navigating to the top line of the editing page.

:w : 存檔該編輯檔案。w stands for write.

:q : 離開該編輯檔案。q stands for quit.

:q! : 離開該編輯檔案並不儲存。q! stands for quit and not saving it.

:wq : 存檔並離開該編輯檔案,相當於 :w & :q 兩個指令的結合。

:e. : 顯示當前所有清單列表。listing out all the files in the current directory.

Typing :e. to display all files

dd : 刪除單行文字。

u : (算是剛開始使用vim不小心刪除或者是編輯錯誤訊息在腳本時最常使用的指令) 回車,也就是恢復上一次修改訊息。u stands for undo.

/{search for words} : 查找文字並跳轉至文字。For example, in my test.sh file, when typing /New , then the editor will direct it to the line where it begins with New.

Typing /New to search for words

另外,剛輸入vim 編輯文檔時,對於初學者而言也會碰到不知道如何即時編輯的情況,那就是輸入以下的指令來幫助使用者直接進入插入模式:

i : 插入模式。i stands for insert.

a : 附加模式。a stans for append.

o : 開啟模式。o stands for open.

s : 取代字符模式。s stands for substitute.

c : 取代文字模式。c stands for change.

--

--