# vim ###### tags: `Linux` > 紀錄vim常用指令 [TOC] ## :memo: Where do I start? > 輸入以下指令進入教學[color=#3b75c6] ```bash vimtutor ``` ## :memo: Common commands. ### Moving the cursor > 移動游標[color=#3b75c6] | H | J | K | L | | ------------ | ------------ | ---------- | ------------- | | :arrow_left: | :arrow_down: | :arrow_up: | :arrow_right: | > 搭配數字 (Number) 來移動Cursor (ex: 2w, 3e)[color=#3b75c6] | Commands | Descriptions | | -------------- | ----------------------- | | Number +`w` | 移動到第N個word的開頭處 | | Number +`e` | 移動到第N個word的結尾處 | > 進階移動Cursor[color=#3b75c6] | Commands | Descriptions | | ---------------- | -------------- | | Press`<0>`(zero) | 移動到當行開頭 | | Press`<$>` | 移動到當行結尾 | | `gg` | 移動到文件開頭 | | Press `<G>` | 移動到文件結尾 | | Press `<H>` | 移動到當前頁面第一行開頭處 | | Press `<M>` | 移動到當前頁面中間行開頭處 | | Press `<L>` | 移動到當前頁面最後一行開頭處 | | Press `<%>` | 在括號上會移動到對應的左右括號 | ### Exiting Vim > Vim 每次完成一個動作記得按下ESC確保回到**一般模式**[color=#3b75c6] | Commands | Descriptions | | ------------- | ------------------------------- | | Press `<ESC>` | 確保在一般模式(Normal mode)下 | | `:q!` | 放棄任何變動並退出 | | `:wq` | 儲存檔案並退出 | ### Text editing | Commands | Descriptions | | ------------ | ---------------------------------- | | Press `<x>` | 刪除當前cursor的字元 | | Press `<a>` | 進入附加模式,在cursor之後新增文字 | | Press `<A>` | 進入附加模式,在那行最後新增文字 | | Press `<i>` | 進入插入模式,在cursor之前新增文字 | | Press `<I>` | 進入插入模式,在那行開頭新增文字 | | `d$` | 刪除從cursor到那行的最後 | | `dd` | 刪除整行 | | `dw` | 刪除到下個word之前 | | `de` | 刪除到當前word結束 | ### Undo and Redo | Commands | Descriptions | | ------------------ | -------------------------------------- | | Press `<u>` | 回到上次做的指令之前 | | Press `<U>` | 回復到那行最初的狀態 | | Press `CTRL + <r>` | 回到上次做的指令之後 (undo the undo's) | ### Copy and Paste | Commands | Descriptions | | ----------- | ------------------------------ | | Press `<y>` | 複製cursor所在範圍 | | Press `<p>` | 在下一行貼上複製或上次刪除內容 | | Press `<P>` | 在上一行貼上複製或上次刪除內容 | ### Replace and Change | Commands | Descriptions | | ----------- | ----------------------------------------------------------- | | Press `<r>` | 進入取代模式,取代一個字元即會回到一般模式 | | Press `<R>` | 進入取代模式,取代到直到按下`<ESC>` | | `ce` | 從當前cursor刪除到那個word結束,即可變更文件直到按下`<ESC>` | | `ce` | 從當前cursor刪除到那行結束,即可變更文件直到按下`<ESC>` | ### Search and Replace | Commands | Descriptions | | ------------------------- | ------------------------------------------ | | `/` + Keyword | 向後搜尋關鍵字(Keyword) | | `?` + Keyword | 向前搜尋關鍵字(Keyword) | | Press `<n>` | 進入搜尋模式後,向後查找 | | Press `<N>` | 進入搜尋模式後,向前查找 | | `:s/search/replace/g` | 在當行進行取代替換 | | `:1,$s/search/replace/g` | 從第一行到檔案最後,搜尋並進行取代 | | `:1,$s/search/replace/gc` | 從第一行到檔案最後,搜尋並詢問是否進行取代 | ### 選取多行 | Commands | Descriptions | | ----------------- | ------------ | | Press `<Shift+v>` | 整行選取 | | Press `<Ctrl+v>` | 垂直選取 | :::info 若需要垂直插入文字先`<Ctrl+v>`後,調整要輸入的行數,按下`<Shift+i>` 輸入想輸入的文字後,按下`<Esc>`即可完成。 ::: ## :memo: Set option. > `Ctrl+w+` 可以選擇切換當前文件(多文件下)[color=#3b75c6] | Commands | Descriptions | | ------------------------- | ------------------------------------------ | | `:set nu` | 顯示行號 | | `:sp` + FileName | 多開文件(垂直) | | `:vsp` + FileName | 多開文件(水平) | ## :memo: How to execute an external command | Commands | Descriptions | | ----------- | ------------ | | `:!command` | 執行外部指令 | ## :memo: Define simple vim config ```script set nu set cursorline set tabstop=4 set shiftwidth=4 " Color configuration set bg=dark color evening " Same as :colorscheme evening hi LineNr cterm=bold ctermfg=DarkGrey ctermbg=NONE hi CursorLineNr cterm=bold ctermfg=Green ctermbg=NONE " 視窗切換時候顯示/隱藏游標底線 autocmd WinEnter * setlocal cursorline autocmd WinLeave * setlocal nocursorline " 存檔時自動把行末多餘的空白刪除 autocmd BufWritePre * :%s/\s\+$//e if executable("ncverilog") autocmd BufRead,BufNewFile *.v noremap <F5> :% w !bash run.sh<Enter> else autocmd BufRead,BufNewFile *.v noremap <F5> :echo "YOU NEAD TO SOURCE CADENCE TOOLS !" endif ``` ```script= set t_Co=256 colorscheme desert set cursorline syntax on hi cursorLine cterm=none ctermbg=238 set nocursorcolumn set nocompatible set ruler set number :hi LineNr cterm=bold ctermfg=DarkGrey ctermbg=none set hlsearch set tabstop=4 set softtabstop=4 set shiftwidth=4 set expandtab set autoindent set backspace=2 set paste set incsearch set autowrite " Event trigger autocmd WinEnter * setlocal cursorcolumn autocmd WinLeave * setlocal nocursorcolumn autocmd WinEnter * setlocal number autocmd WinLeave * setlocal nonumber " Make extra space remove autocmd BUfWritePre * :%s/\s\+$//e " Go back to the last cursor position if has("autocmd") autocmd BufReadPost * \ if line("'\"") > 1 && line ("'\"") <= line("$") | \ exe "normal g'\"" | \ endif endif set laststatus=2 set statusline=%#filepath#[%{expand('%:p')}]%#filetype#[%{strlen(&fenc)?&fenc:&enc},\ %{&ff},\ %{strlen(&filetype)?&filetype:'plain'}]%#filesize#%{FileSize()}%{IsBinary()}%=%#position#%c,%l/%L\ [%3p%%] hi filepath cterm=none ctermbg=238 ctermfg=40 hi filetype cterm=none ctermbg=238 ctermfg=45 function IsBinary() if (&binary == 0) return "" else return "[Binary]" endif endfunction function FileSize() let bytes = getfsize(expand("%:p")) if bytes <= 0 return "[Empty]" endif if bytes < 1024 return "[" . bytes . "B]" elseif bytes < 1048576 return "[" . (bytes / 1024) . "KB]" else return "[" . (bytes / 1048576) . "MB]" endif endfunction ``` ## :memo: Apply my vim completed setup [Link](https://github.com/LeosyHsu/myvim) ```bash=1 git clone https://github.com/LeosyHsu/myvim.git mv myvim ~/.vim git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim ``` ```script Launch vim and run :PluginInstall ``` --- ## Reference - Hackmd(yanjiun) ➜ [Vim](https://hackmd.io/@yanjiun/vim) - 鳥哥的 Linux 私房菜 ➜[第九章、vim 程式編輯器](http://linux.vbird.org/linux_basic/0310vi.php) - [大家來學VIM(一個歷久彌新的編輯器)](http://www.study-area.org/tips/vim/index.html) - [手把手教你把 Vim 改装成一个 IDE 编程环境 ](https://www.csie.ntu.edu.tw/~b97049/pdf/Programming+with+vim+-+Ver.0.7.pdf) - 成大資工 Wiki ➜[vimrc設定教學 ](http://wiki.csie.ncku.edu.tw/vim/vimrc#%E4%BB%8B%E9%9D%A2%E5%84%AA%E5%8C%96) - Noob's Space ➜[Vundle:管理 vim 套件的工具](https://noob.tw/vundle/) - 國王的耳朵是驢耳朵 ➜[給自己剪貼用的vim設定](https://wen00072.github.io/blog/2018/02/15/vim-setup-for-trace-c-code/)