vim

Vim是從vi發展出來的一個文字編輯器。其代碼補完、編譯及錯誤跳轉等方便編程的功能特別豐富,在程式設計師中被廣泛使用。和Emacs並列成為類Unix系統用戶最喜歡的編輯器。 Vim的第一個版本由布萊姆·米勒在1991年發布。

Vim 高級技巧

常用技巧
命令執行(注意大小寫不同)  使用說明  使用技巧
 i 以插入字元方式進入編輯模式 與 a 同為進入編輯模式,兩者差別只有游標的起始位置不同
 a 以附加字元方式進入編輯模式
 h j k l 操控游標的移動,h--向左, j--向下, k--向上, l--向右 當遇到部份作業系統不支援方向鍵使用時,可用此方式代替
 :1 游標移到第一行
 :22 游標移到第22行 作程式除錯找特定行列時會用到
 gg 游標移至檔首 
 G 游標移至檔尾 編輯大檔案時要移到檔尾不用一頁一頁捲
 0 游標移至行首
 $ 游標移至行尾
w
游標移至下一個單字

W
游標移至下一個空格

{
游標移至上一個段落

}
游標移至下一個段落

 :wq 存檔後離開 
 :w new.txt 另存新檔成 new.txt
 :w !sudo tee % 以 sudo 模式存檔 如果存檔時遇到權限不足,且有 sudo 的權限時的方法
 :q! 不存檔離開  如不小心編錯內容,只要沒存檔前就可以用此法離開,保存原始內容
:set nu!
行號顯示切換

:set hlsearch  Highlight 搜尋的關鍵字 有些系統預設已經啟用
:set cuc! 
直欄游標切換
編輯 json, yaml  格式
 : syntax on 啟用 Hihjlight 顯示
 dd 刪除目前這一行
 10dd 刪除目前以下10行(含本列)
 yy 複製目前這一行
 3yy 複製目前以下3行(含本列)
 p 貼上

 :%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> 的內容刪除
 /keyword 搜尋文字
 /\KeyWord 搜尋文字(不分大小寫)
/S[Uu]SE
搜尋特定字元不分大小寫

 /\s\+$ 搜尋行尾的空格字元並 Highlight 這可以配合 :set hlsearch 使用, not support AIX
 ~ 大小寫互換
 :%s/[ctrl+V and ctrl+M]//g 消除斷行符號 ^M
 :%s/\s\+$// 移除所有行尾的空格字元 not support AIX
: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 切換視窗

其他技巧:

Visual mode
快速移動技巧
取代文字技巧
複製整行
# 複製指定的行號,然後移到要貼上的行,輸入 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. Ctrl + v
  2. 移到第一行的中間處
  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

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
"
" 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 <c-j> :m .+1<CR>==
nnoremap <c-k> :m .-2<CR>==
inoremap <c-j> <Esc>:m .+1<CR>==gi
inoremap <c-k> <Esc>:m .-2<CR>==gi
vnoremap <c-j> :m '>+1<CR>gv=gv
vnoremap <c-k> :m '<-2<CR>gv=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 <leader>r <esc>:'<,'>:w !python3<CR>
    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

Cheat Sheets

PDF: vim-commands-cheat-sheet-by-pnap.pdf

vim_cheat_sheet.jpg

vim-cheatsheet.png

vi-help-sheet.jpg