vim Vim是從vi發展出來的一個文字編輯器。其代碼補完、編譯及錯誤跳轉等方便編程的功能特別豐富,在程式設計師中被廣泛使用。和Emacs並列成為類Unix系統用戶最喜歡的編輯器。 Vim的第一個版本由布萊姆·米勒在1991年發布。 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>/g/^$/d 包含
...
的內容刪除 :%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 -> 重複上次變更 .   選擇多行取代: 行模式:shift + v 選擇多行 取代關鍵字: :s/old/new/g   ( 反斜線也可以用井字取代 ) 複製整行 # 複製指定的行號,然後移到要貼上的行,輸入 p 貼上 :<行號>yank # 複製指定的行號,貼上至目前行的下方. 使用 copy 或 t :<行號>copy. :<行號>t. # 複製目前行下方第 10 行文字,並貼上至目前行 :+10t. # 複製目前行上方第 10 行文字,並貼上至目前行 :-10t. # 複製連續多行文字(33 ~ 44)至目前位置 :33,44t. 在文字區段的每行行首插入多個空白字元 Ctrl + v 移到第一行的開頭處 往下移最後行 Shift + I 輸入要插入的空白 Esc 跳離後完成 在文字區段的每行行尾插入`;` 移到第一行的中間處 Ctrl + v 往下移至最後行 Shift + $ Shift + A 輸入要插入的文字  ; Esc 跳離後完成 快速註解 Shift + v 選取多行 :norm i# (加上註解符號) :norm x (移除註解符號) 另存新檔 Shift + v (Visually Seleting) 選取多行 :w /path/to/newfile (Save as newfile) gv (reselect the last visual selection) d (delete that selection) 檔名自動補全 (auto-complete) 進入編輯模式 輸入 /etc/profile(任意路徑) Ctrl + x -> Ctrl f Tutorials Youtube Channel - Vim (TheFrugalComputerGuy)   VIM Colors Online VimTricks 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: 將 putty 視窗最大化以及調整字型大小到 9 試試。 安裝 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 Configure Vim with vimrc Cheat Sheets PDF:  vim-commands-cheat-sheet-by-pnap.pdf