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**)。 當用戶處於不同模式的時候,敲擊鍵盤會產生不同的作用。  :::info 不論在**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行 :::success word以空白為區隔 ::: - `dw`:從游標開始刪除一個word - `d[n]w`:從游標開始刪除n個word - `x`:刪除一個character - `[x]x`:刪除n個character :::info 刪除的文字會被寫進buffer,可以用以下按鍵貼上 - `p`:貼在游標後 - `P`:貼在游標前 ::: ### 復原 - `u` :::info 取消復原`Ctrl+R` ::: ### 搜尋 `*`:搜尋目前游標單字 `/`:(自行輸入) `gd`:搜尋第一個出現的 `g*`:搜尋所有 :::info `n`:下一個搜尋結果 `N`:上一個搜尋結果 ::: ### 以16進位編輯檔案 - `:%!xdd` - `:%!xdd -r` :::info `:` command-line mode `%` [ex command-line ranges](http://vimdoc.sourceforge.net/htmldoc/cmdline.html#cmdline-ranges),將整份文件進行轉換 `!` [filter commands](http://vimdoc.sourceforge.net/htmldoc/change.html#filter),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+方向鍵=跳行,對應如下 :arrow_left:|:arrow_down:|:top:|:arrow_right: -|-|-|- 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` :::info `;` 重複上次指令 ::: ## 插入模式 - `I` 行首 - `A` 行尾 - ==`O` 上一行+行首== - ==`S` 取代+行首== - ==`C` 取代(從目前字元)+行首== ``` # 插入雙引號 I" + A" ``` ## 普通模式 `yy`: copy 單行模式 `p/P`: 貼上 `<< / >>`:縮排 ==`U`: 一次性回復單行== ==`^a / ^x`: 遞增遞減數字== :::info `.` 重複上次指令 ::: ## Reference - [Vim 編輯器 入門指南 (上)](https://www.youtube.com/watch?v=Yk4s-WLjxug)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up