vim操作命令學習筆記
最近因爲買了一個阿里雲的服務器學linux系統,之前用vim寫代碼只會(:q!,:wq:i…)這幾個命令,然後在B站看到了大神TheCW用的Vim以後強烈懷疑自己用的是假Vim,然後下定決心去學習vim常用的操作,說不上操作起來有多順手,只求寫代碼的時候效率可以高一些,可以節省不少時間。所以去github找了一個比較完整的vim操作教程,邊學習邊寫下來,方便以後需要時直接查詢。
操作命令 | 說明 |
---|---|
i | 在光標所在位置開始插入字符 |
shift+i | 在行首插入 |
a | 在光標所在位置的後面開始寫入 |
shift+a | 在光標所在行的行尾寫入 |
o | 在本行的下一行開啓一個新行並進入寫入模式 |
shift+o | 在本行的上一行開啓一個新行並進入寫入模式 |
x | 刪除光標所在的字符 |
b | 跳到上一個單詞,即中間沒有空格的一個字符串爲一個單詞 |
w | 跳到下一個單詞,即中間沒有空格的一個字符串爲一個單詞 |
0 | 會把光標移動到本行的行首,但是依然會處在普通模式 |
zz | 把當前行移動到屏幕的中心位置 |
:split | 水平分屏 |
:vsplit | 垂直分屏 |
// |表示光標所在位置,d 3 向左箭頭--表示從光標所在位置向左刪除三個字符,刪除之後即--asdasd
asdfgh|asd
// |表示光標所在位置,c(change) 6 → 表示向右改變六個字符並進入寫入模式,最後的效果是|tion> <operation>
|<operation> <motion>
// |表示光標所在位置,c w(change word) 更改這個詞,即<|> <motion>,刪除這個單詞並進入寫入模式
<|operation> <motion>
// |表示光標所在位置,c i w(change in word),當光標處與一個單詞的中間時可以刪除整個單詞並且進入寫入模式,即<|> <motion>
<oper|ation> <motion>
// |表示光標所在位置 c i "(change in "),會刪除""中的內容,並進入寫入模式
"this |is a quotation"
// |表示光標所在位置 c i <(change in <或>),會刪除<>中的內容,並進入寫入模式
<h1 this is title />
// |表示光標所在位置 y i "(change in "),會複製""中的內容,光標移動到需要黏貼的位置按下p可以進行黏貼
"this |is a quotation"
// |表示光標所在位置 d i "(change in "),會複製""中的內容,光標移動到需要黏貼的位置按下p可以進行黏貼
"this |is a quotation"
// |表示光標所在位置,f v(find v),從光標所在位置向後查找v,即會把光標移動到v的前面
"|this is vim: the best editor out there"
// |表示光標所在位置,d f :(delete find :)一直剪切到冒號位置,包括冒號
"|this is vim: the best editor out there"
// |表示光標所在位置,y f :(delete find :)一直複製到冒號位置,包括冒號
"|this is vim: the best editor out there"
// |表示光標所在位置,c f :(delete find :)一直刪除到冒號位置,包括冒號,並且進入寫入模式
"|this is vim: the best editor out there"
複製粘貼
- yy–複製當前行
- dd–剪切
- p–黏貼
- yl–複製當前的字符
- yh–複製當前字符的前一個字符
- ggyG–全選複製
- nyy – 從當前行開始複製n行, 例如5yy就是複製5行
- ddp – 交換上下兩行的位置
- xp–交換前後兩個字符的位置
- :1,10 copy 16–把從第一行到第十行的內容複製到從16行到26行
- :1,10 co 16–把從第一行到第十行的內容複製到從16行到26行
- :1,10 move 16–把從第一行到第十行的內容移動到從16行到26行
- :1,10 m 16–把從第一行到第十行的內容移動到從16行到26行
刪除
- dd – 刪除當前行
- ndd – 從當前行開始刪除n行
- n,md – 刪除從第n行到第m行,即:1,10d,刪除一到十行
- d0 – 刪除從光標所在位置至行首的字符
- d$ – 刪除從光標所在位置至行尾的字符
- dw – 刪除光標之後單詞的剩餘部分
- diw – 刪除光標上的單詞(不包括空白字符)
- daw – 刪除光標上的單詞(包括空白字符)
- dG – 刪除光標所在位置至文件結尾的所有字符
- x(小寫) – 刪除光標所在的字符
- X(大寫) – 刪除光標所在的前一個字符
選擇
- :10 --跳轉到第十行
- V20G(在一般模式下) – 選擇從當前行到第20行的內容
移動
以下命令都是在一般模式下
- 0 – 跳轉到光標所在行的行首
- gg – 跳轉到文件的開頭
- $ – 跳轉到光標所在行的行尾
- ^ – 跳轉到本行的第一個不是空白字符的位置
- G – 跳轉到文件的結尾
- nG(或ngg) – 跳轉到第n行
塊編輯
// 修改字符
- 按
r
(小寫), 進入修改模式 - 輸入字符(只能輸入一個, 選擇的內容都會改爲這個字符)
// 在塊前面插入內容
- 按
I
(大寫), 進入行首插入模式 - 輸完內容按ESC
// 在塊後面插入內容
- 按
A
(大寫), 進入行尾插入模式 - 輸完內容按ESC
// 刪除選擇內容
- d
替換
前面有冒號的命令都是在命令行模式下
- :s/abc/efg – 把當前行中的ab替換成efg–只替換每一行中第一次出現的位置
- :s/abc/efg/g – 把當前行中的ab替換成efg–替換該行中所有滿足條件的字符
- :%s/abc/efg – 把該文件中的所有abc都替換成efg
- :n,$s/abc/efg – 從第n行開始向下替換
- : .,$s/abc/efg – 從當前行開始向下替換,點表示當前行
- :%s/abc\c/efg/g – \c忽略abc的大小寫, \C不忽略大小寫
- :%s/abc/efg/gc – * 後面的c表示每次替換之前都需要確認
- :%s/<word>/newword/g – 只匹配整個單詞word,類似於aword不匹配,即在單詞前後加上 < 和 >
上面命令中,最後沒有g表示只替換一行中第一次出現的字符串abc爲efg。而後面帶g的表示當前行的所有abc替換efg。
分屏
打開文件時分屏
// 指定幾個文件就分幾個屏幕,小寫的o表示水平分割
vim -on fileName1,fileName2...
// 指定幾個文件就分幾個屏幕,大寫的O表示垂直分割
vim -On fimeName1,fileName2...
其中大寫O表示垂直分割(vertical), 小寫o表示水平分割(horizontal, 默認值), n表示分屏數量, 如果不加文件名, 則打開n個空白分屏, 如果不加n, 則根據打開的文件數量來決定分屏的數量
在Vim中分屏
// 創建空白分屏, 垂直分割
:vsp
// 打開新文件, 垂直分割
:vsp filename
// 創建空白分屏, 水平分割
:sp 或者
:new
// 打開新文件, 水平分割
:sp filename 或者
:new filename
// 快捷鍵,
Ctrl + w + s 水平分割
Ctrl + w + v 垂直分割
關閉分屏
// 關閉其他分屏, 只保留當前分屏
Ctrl + w + o 或者
:only
// 關閉當前分屏, 不能關閉最後一個窗口
Ctrl + w + c
// 關閉當前分屏, 只剩最後一個分屏時退出
Ctrl + w + q
分屏之間移動
// 分屏之間移動光標
Ctrl + w + w
// 在分屏之間移動光標
上 - Ctrl + w + k 或者 Ctrl + 上箭頭
下 - Ctrl + w + j 或者 Ctrl + 下箭頭
左 - Ctrl + w + h 或者 Ctrl + 左箭頭
右 - Ctrl + w + l 或者 Ctrl + 右箭頭
// 移動分屏(不是移動光標), 先按Ctrl + w, 再按shift + hjkl
上 - Ctrl + w + K
下 - Ctrl + w + J
左 - Ctrl + w + H
右 - Ctrl + w + L
改變尺寸
// 先按Ctrl + w, 再按後面的字符
左 - Ctrl + w + <
右 - Ctrl + w + >
上 - Ctrl + w + + // 最後一個加號是按鍵
下 - Ctrl + w + -
均等 - Ctrl + w + =
vimrc配置
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'
"自動補全
Bundle 'Raimondi/delimitMate'
filetype plugin indent on
" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
"Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
"Plugin 'L9'
" Git plugin not hosted on GitHub
"Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
"Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
"Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Avoid a name conflict with L9
"Plugin 'user/L9', {'name': 'newL9'}
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
"markdown
Plugin 'godlygeek/tabular'
Plugin 'plasticboy/vim-markdown'
Plugin 'terryma/vim-multiple-cursors'
set number " 顯示行號
set cursorline " 突出顯示當前行
"set guicursor=i:block-iCursor-blinkon0,v:block-vCursor
"set cursorcolumn " 高亮顯示當前行/列
set shiftwidth=4 " 設定 << 和 >> 命令移動時的寬度爲 4
set softtabstop=4 " 使得按退格鍵時可以一次刪掉 4 個空格
set tabstop=4 " 設定 tab 長度爲 4
set expandtab " 加上這句防止代碼複製到別的地時tab變爲8個空格
set autochdir " 自動切換當前目錄爲當前文件所在的目錄
"color solarized
set ruler " 顯示光標當前位置
set nowrap " 禁止折行
syntax enable " 開啓語法高亮功能
syntax on " 允許用指定語法高亮配色方案替換默認方案
"colorscheme anderson " 設定配色方案
"colorscheme spring " 設定配色方案
"color spring
set smartindent " 開啓新行時使用智能自動縮進
set laststatus=2 " 顯示狀態欄 (默認值爲 1, 無法顯示狀態欄)
"set foldenable " 開始摺疊
"set foldmethod=syntax " 設置語法摺疊
set mouse=a
"設置補全文件名
set wildmode=list:longest
"hi preproc guifg=blue ctermfg=blue
" MiniBufExplorer 多個文件切換 可使用鼠標雙擊相應文件名進行切換
let g:miniBufExplMapWindowNavVim=1
let g:miniBufExplMapWindowNavArrows=1
let g:miniBufExplMapCTabSwitchBufs=1
let g:miniBufExplModSelTarget=1
map <F7> 20zl " Scroll 20 characters to the right
map <F8> 20zh " Scroll 20 characters to the left
"au VimEnter * if line('$') > &lines | set go+=r | else | set go-=r | endif
"au VimResized * if line('$') > &lines | set go+=r | else | set go-=r | endif
" 配置多語言環境
if has("multi_byte")
" UTF-8 編碼
set encoding=utf-8
set termencoding=utf-8
set formatoptions+=mM
set fencs=utf-8,gbk
if v:lang =~? '^\(zh\)\|\(ja\)\|\(ko\)'
set ambiwidth=double
endif
if has("win32")
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
language messages zh_CN.utf-8
endif
else
echoerr "Sorry, this version of (g)vim was not compiled with +multi_byte"
endif
"配置taglist
let Tlist_Show_One_File=1 "只顯示一個文件中的tag
let Tlist_Exit_OnlyWindow=1
let Tlist_Sort_Type="name" "按tag名稱排序
let Tlist_Use_SingleClick=1 "單擊
let Tlist_Use_Right_Window=1 "把taglist窗口放在屏幕的右側,缺省在左側
let Tlist_Show_Menu=1 "顯示taglist菜單
map <silent> <F9> :TlistToggle<cr>
"配置ctags快捷鍵
map <C-F12> :!ctags --c-kinds=+lpx -R .<CR>
"配置WinManager
let g:winManagerWindowLayout='FileExplorer'
nmap wm :WMToggle<cr>
"設置cscope
:set cscopequickfix=s-,c-,d-,i-,t-,e-
"設置MiniBufExplorer
let g:miniBufExplMapWindowNavArrows = 1
"頭文件互換
nnoremap <silent> <F12> :A<CR>
let g:ycm_global_ycm_extra_conf = '/home/zhangjikai/.vim/bundle/third_party/ycmd/cpp/ycm/ycm.c.py'
let g:ycm_semantic_triggers = {}
let g:ycm_semantic_triggers.c = ['->', '.', '(', '[', '&']
let g:ycm_collect_identifiers_from_tags_files=1
let g:ycm_min_num_of_chars_for_completion=3
let g:ycm_seed_identifiers_with_syntax=1
" YouCompleteMe 功能
" 補全功能在註釋中同樣有效
let g:ycm_complete_in_comments=0
" 允許 vim 加載 .ycm_extra_conf.py 文件,不再提示
let g:ycm_confirm_extra_conf=0
" 開啓 YCM 基於標籤引擎
let g:ycm_collect_identifiers_from_tags_files=1
" 引入 C++ 標準庫tags,這個沒有也沒關係,只要.ycm_extra_conf.py文件中指定了正確的標準庫路徑
set tags+=/data/misc/software/misc./vim/stdcpp.tags
" YCM 集成 OmniCppComplete 補全引擎,設置其快捷鍵
inoremap <leader>; <C-x><C-o>
" 補全內容不以分割子窗口形式出現,只顯示補全列表
set completeopt-=preview
" 從第一個鍵入字符就開始羅列匹配項
let g:ycm_min_num_of_chars_for_completion=1
" 禁止緩存匹配項,每次都重新生成匹配項
let g:ycm_cache_omnifunc=0
" 語法關鍵字補全
let g:ycm_seed_identifiers_with_syntax=1
" 修改對C函數的補全快捷鍵,默認是CTRL + space,修改爲ALT + ;
let g:ycm_key_invoke_completion = '<M-;>'
" 設置轉到定義處的快捷鍵爲ALT + G,這個功能非常贊
nmap <M-g> :YcmCompleter GoToDefinitionElseDeclaration <C-R>=expand("<cword>")<CR><CR>
" 設置按哪個鍵上屏
let g:ycm_key_list_select_completion = ['<TAB>', '<Down>', '<Enter>']
"括號補全
"取消自動檢查錯誤
let g:syntastic_c_checkers = []
let g:syntastic_disabled_filetypes = ['c']
let g:syntastic_ignore_files = ['*.h','*.c']
"自動保存
let g:auto_save = 1
let g:auto_save_no_updatetime = 0
let g:auto_save_in_insert_mode = 0
"配置gvim
if has("gui_running")
set guifont=YaHei\ Consolas\ Hybrid\ 12
endif
" Go to last file(s) if invoked without arguments.
"autocmd VimLeave * nested if (!isdirectory($HOME . "/.vim")) |
" \ call mkdir($HOME . "/.vim") |
" \ endif |
" \ execute "mksession! " . $HOME . "/.vim/Session.vim"
"autocmd VimEnter * nested if argc() == 0 && filereadable($HOME . "/.vim/Session.vim") |
" \ execute "source " . $HOME . "/.vim/Session.vim"
"設置terminal 使用gui顏色
" IMPORTANT: Uncomment one of the following lines to force
" using 256 colors (or 88 colors) if your terminal supports it,
" but does not automatically use 256 colors by default.
set t_Co=256
"set t_Co=88
let g:CSApprox_attr_map = { 'bold' : 'bold', 'italic' : '', 'sp' : '' }
colorscheme spring
hi CursorLine cterm=NONE ctermbg=lightgray ctermfg=black guifg=white
au BufRead,BufNewFile *.{md,mdown,mkd,mkdn,markdown,mdwn} set filetype=markdown nofoldenable
"設置粘貼模式切換,在粘貼模式下,粘貼時不會自動縮進
set pastetoggle=<F3>
set clipboard=unnamed
"正常模式下插入空行 to/tO
nmap <silent> to :call append('.', '')<CR>j
nmap <silent> tO :call append(line('.')-1, '')<CR>k
set foldmethod=syntax
set nofoldenable
插件
- Vundle
- YouCompleteMe
- Markdown
- MiniBufExplorer
- Taglist
- Ctags
- NERDTree
- WimManager
- vim-multiple-cursors
- 其他插件
參考地址