# vim # Vim 高級技巧 ##### 常用技巧
**命令執行(注意大小寫不同)** **使用說明** **使用技巧**
i以插入字元方式進入編輯模式與 a 同為進入編輯模式,兩者差別只有游標的起始位置不同
a以附加字元方式進入編輯模式
o插入一個空白行並編輯
h j k l操控游標的移動,h--向左, j--向下, k--向上, l--向右當遇到部份作業系統不支援方向鍵使用時,可用此方式代替
:1 : 22游標移到第一行 游標移到第22行 程式除錯找特定行號時會用到
gg游標移至檔首
G游標移至檔尾編輯大檔案時要移到檔尾不用一頁一頁捲
0 / $游標移至行首/行尾
w / W 游標移至下一個單字/空格 游標右移
b / B游標移至上一個單字/空格 游標左移
x刪除目前字元
J (Shift + j)移除目前行尾的斷行上下兩行做合併
{ 游標移至上一個段落
} 游標移至下一個段落
:wq存檔後離開
:w new.txt另存新檔成 new.txt
:w !sudo tee %以 sudo 模式存檔如果存檔時遇到權限不足,且有 sudo 的權限時的方法
:q!不存檔離開 如不小心編錯內容,只要沒存檔前就可以用此法離開,保存原始內容
:set nu! 行號顯示切換
:set hlsearch Highlight 搜尋的關鍵字有些系統預設已經啟用
:set cuc! 直欄游標切換 編輯 json, yaml 格式
: syntax on啟用 Hihjlight 顯示
d dd刪除標記的內容 刪除目前這一行
10dd刪除目前以下10行(含本列)
y yy複製標記的內容 複製目前這一行
3yy複製目前以下3行(含本列)
p貼上
/keyword搜尋文字
/\\KeyWord搜尋文字(不分大小寫)
/S\[Uu\]SE 搜尋特定字元不分大小寫
\*快速搜尋目前游標的單字
:%s/old/new/g :%s/old/new/gc 全文內容包含有old字元的都會取代成new字元 gc: 啟用提示
:s#/usr/local/bin#/usr/sbin#g 以 # 作為分隔符號
:10,30 s/old/new/g 第10-30行置換 old 為 new
:%s/old1\\|OLD2/new/gc 全文置換 old1與OLD2 為 new
:/<table>/,/<\\/table>/g/^$/d包含 <table> ... </table> 的內容刪除
:%s/\\t/ /g置換 Tab 成4個空白寫程式時有用
/\\s\\+$搜尋行尾的空格字元並 Highlight這可以配合 :set hlsearch 使用, not support AIX
:%s/\\s\\+$//移除所有行尾的空格字元not support AIX
~大小寫互換
:%s/\[ctrl+V and ctrl+M\]//g消除斷行符號 ^M
ggVG 全選內容 類似 Ctrl a
dG清除游標以下所有內容
dgg刪除游標以上所有內容
:1,$d清空所有內容
:1,22d清除 1-22 行
:23,$d清除 23 行以下所有內容
:! ls -l執行外部 SHELL 指令 :!! 上一個指令
:vsplit my.file左右分割視窗且左邊載入 my.file ctrl + w + w 切換視窗
:split my.file上下分割視窗ctrl + w + w 切換視窗
:sort :sort! :sort u :sort i :sort n 排序 反向排序 排序並移除重複行 忽略大小寫的排序 以數字排序
u Ctrl + r Undo Redo
:g/^$/d :4,100g/^$/d :g/pattern/d :!g/pattern/d 全文移除空白行 第4-100行移除空白行 移除符合 pattern 的行 移除不符合 pattern 的行 \[range\]g/pattern/cmd
其他技巧: - 跳到上次編輯的行號:`'.` - 開啟指令視窗:`:terminal` - `:below terminal` , `:bel term` - `:vertical terminal` , `:vert term` - 切換視窗: Ctrl + `w` + `w` - 編輯模式: Ctrl + `w` + `N` (切換回指令列: `hi`) - 搜尋檔案並開啟:`:find your-script.sh` (可用 tab 自動補全與 `*` 符號) - `:find *.sh` ##### Visual mode - 切換至字元模式: `V` - 切換至行模式: Shift + `V` - 整行文字: Shift + `V` - 文字起始點至結束點: `V` - 切換至區塊模式: Ctrl + `V` - 區塊左邊插入文字: `I` - 區塊右邊插入文字: `A` - 選擇區塊內至行尾的文字: `$` - 選擇與操作 - 游標: 上/下鍵 - 下一個單字: `w` - 下一個單字頭是點: `f .` - 下一個段落: `}` - 取消選擇: `Esc` - 複製選擇區文字: `y` - 貼上文字: `p` - 刪除選擇區的文字: `d` \*(Delete)\* - 移動選擇區的文字: `d` > Move to target line > `p` - 取代選擇區的文字: `r` \*(Replace)\* - 變更選擇區的文字: `c` \*(Change)\* - 復原最近刪除文字: `u` \*(Undo)\* - 快速選擇目前引號("或')內的文字: `i` + `"` 或 `'` ##### 快速移動游標技巧 - 搜尋 MyKeyWord: `/MyKeyWord` - 往下移動至相同文字的位置:`*` - 標示關鍵字:`:set hlsearch` - 輸入即顯示關鍵字:`:set incsearch` - 段落`{},[]()`的結束符號:`%` - 返回目前位置的段落起始符號`{`:`[{` - 移動游標的的顯示位置(行號不變):`zt` (Top), `zz` (Middle), `zb` (Bottom) - 移動游標至畫面上其他行:`H` (Top), `M` (Middle), `L` (Last Line) - 整頁移動:`Ctrl+b` (上一頁), `Ctrl+f` (下一頁) - 整行移動:`Ctrl+y` (向上), `Ctrl+e` (向下) ##### 取代文字技巧 - 逐一取代:搜尋文字 `/old` -> `cw` -> 輸入取代文字 new -> 離開編輯模式 `Esc` -> 下一個搜尋 `n` -> 重複上次變更 `.` - 選擇多行取代: 1. 行模式:shift + `v` 2. 選擇多行 3. 取代關鍵字:`:s/old/new/g` (*反斜線也可以用井字取代*) ##### 複製整行 ``` # 複製指定的行號,然後移到要貼上的行,輸入 p 貼上 :<行號>yank # 複製指定的行號,貼上至目前行的下方. 使用 copy 或 t :<行號>copy. :<行號>t. # 複製目前行下方第 10 行文字,並貼上至目前行 :+10t. # 複製目前行上方第 10 行文字,並貼上至目前行 :-10t. # 複製連續多行文字(33 ~ 44)至目前位置 :33,44t. ``` ##### 在文字區段的每行行首插入多個空白字元 1. Ctrl + `v` 2. 移到第一行的開頭處 3. 往下移最後行 4. Shift + `I` 5. 輸入要插入的空白 6. Esc 跳離後完成 ##### 在文字區段的每行行尾插入`;` 1. 移到第一行的中間處 2. Ctrl + `v` 3. 往下移至最後行 4. Shift + `$` 5. Shift + `A` 6. 輸入要插入的文字 `;` 7. Esc 跳離後完成 ##### 快速註解 1. Shift + `v` 2. 選取多行 3. `:norm i#` (加上註解符號) 4. `:norm x` (移除註解符號) ##### 另存新檔 1. Shift + `v` (Visually Seleting) 2. 選取多行 3. `:w /path/to/newfile` (Save as newfile) 4. `gv` (reselect the last visual selection) 5. `d` (delete that selection) ##### 檔名自動補全 (auto-complete) 1. 進入編輯模式 2. 輸入 /etc/profile(任意路徑) 3. Ctrl + `x` -> Ctrl `f` ##### Tutorials - [Youtube Channel - Vim (TheFrugalComputerGuy)](https://www.youtube.com/watch?v=1lzXr-MztOU&list=PLy7Kah3WzqrEjsuvhT46fr28Q11oa5ZoI) - [VIM Colors Online](https://vimcolors.com/) - [VimTricks](https://vimtricks.com/) # Vim FAQ ##### Err: A line cannot be longer than 2048 characters. 這是因為 VI 的每一行字數不可超過 2048 字元,遇到時可以將超過字數的行刪除,另存一個檔。 ``` cut -c1-2047 before.log > after.log ``` ##### 開啟 3X MB 檔案時出現錯誤 > ex: 0602-101 Out of memory saving lines for undo. ``` # the number must be less than 8 digits. vi -y 99999999 your-file ``` ##### 自動縮排問題 貼上文字後,因為自動縮排(autoindent)因素,導致內容編排錯亂。 Solution: 貼上文字前先執行 `:set paste` ##### 0602-140 The window is too small to display the current line. AIX 開啟有單行過長檔案時,會出現錯誤。 Solution: 1. 將 putty 視窗最大化以及調整字型大小到 9 試試。 2. 安裝 Vim 取代 Vi。 ##### Unable to paste from clipboard on AIX 在 Windows 複製後,無法在 putty 使用滑鼠貼上內容。 Solution: `:set mouse=v` # vimrc ##### vimrc simple ``` " " Fixed for Python virtualenv as the follow error. " ModuleNotFoundError: No module named 'powerline' " python3 import sys;sys.path.append("/home/alang/.local/lib/python3.10/site-packages") " " Powerline with Vim set laststatus=2 python3 from powerline.vim import setup as powerline_setup python3 powerline_setup() python3 del powerline_setup " syntax on " Enable syntax highlighting set number " Show line numbers set relativenumber " Show relative line numbers set tabstop=4 " Set tab width to 4 spaces set shiftwidth=4 " Set indentation width to 4 spaces set expandtab " Use spaces instead of tabs set cursorline " Highlight the current line " Moving Lines " Ctrl-j: Move down, Ctrl-k: Move down nnoremap :m .+1== nnoremap :m .-2== inoremap :m .+1==gi inoremap :m .-2==gi vnoremap :m '>+1gv=gv vnoremap :m '<-2gv=gv " Preserve last editing position silent! source $VIMRUNTIME/defaults.vim " Copy to Clipboard " Prerequisites: apt install vim-gtk3 " Usage: Press v to select the lines, and then press y to copy set clipboard=unnamedplus ``` ##### vimrc advanced ``` " " minimal vimrc with no (extra) plugins " "regular settings "---------------- " ui set number set ruler set wildmenu set showcmd set showmatch " encoding/format set encoding=utf-8 set fileformats=unix,dos,mac " searching set hlsearch set incsearch set ignorecase set smartcase " indent set shiftwidth=4 set tabstop=4 set softtabstop=4 set autoindent " allow syntax and filetype plugins syntax enable filetype plugin indent on runtime macros/matchit.vim " Moving Lines " Ctrl-j: Move down, Ctrl-k: Move up nnoremap :m .+1== nnoremap :m .-2== inoremap :m .+1==gi inoremap :m .-2==gi vnoremap :m '>+1gv=gv vnoremap :m '<-2gv=gv " autocmds "--------- augroup general autocmd! "keep equal proportions when windows resized autocmd VimResized * wincmd = "save cursor position in a file autocmd BufReadPost * if line("'\"") > 1 && line("'\"") \ <= line("$") | exe "normal! g'\"" | endif augroup END augroup languages autocmd! autocmd BufNewFile,BufRead *.bash set syntax=sh autocmd FileType python xnoremap r :'<,'>:w !python3 autocmd FileType go set noexpandtab autocmd FileType html :syntax sync fromstart autocmd FileType html,javascript,css,json,yaml,sh \ setlocal ts=2 sts=2 sw=2 expandtab augroup END ``` ##### Learning - [A Minimal Vimrc](https://jamesdixon.dev/posts/a-minimal-vimrc/) - [Configure Vim with vimrc](https://linuxhint.com/configure_vim_vimrc/) # Cheat Sheets PDF: [vim-commands-cheat-sheet-by-pnap.pdf](https://osslab.tw/attachments/87) [![vim_cheat_sheet.jpg](https://osslab.tw/uploads/images/gallery/2024-02/scaled-1680-/vim-cheat-sheet.jpg)](https://osslab.tw/uploads/images/gallery/2024-02/vim-cheat-sheet.jpg) [![vim-cheatsheet.png](https://osslab.tw/uploads/images/gallery/2024-02/scaled-1680-/vim-cheatsheet.png)](https://osslab.tw/uploads/images/gallery/2024-02/vim-cheatsheet.png) [![vi-help-sheet.jpg](https://osslab.tw/uploads/images/gallery/2024-02/scaled-1680-/vi-help-sheet.jpg)](https://osslab.tw/uploads/images/gallery/2024-02/vi-help-sheet.jpg)