Table of Contents
vim号称编辑器之神,最近也一直使用vim写代码,最大的印象就是快,无论是临时修改文件,还是专门写代码.现在记录vim主要操作快捷键和我的vim配置
vim操作
一、移动光标
-
左移h、右移l、下移j、上移k
-
向下翻页ctrl
f,向上翻页ctrlb -
向下翻半页ctrl
d,向上翻半页ctrlu -
移动到行尾$,移动到行首0(数字),移动到行首第一个字符处^
-
移动光标到下一个句子 ),移动光标到上一个句子(
-
移动到段首{,移动到段尾}
-
移动到下一个词w,移动到上一个词b
-
移动到文档开始gg,移动到文档结束G
-
移动到匹配的{}.().[]处%
-
跳到第n行 ngg 或 nG 或 :n
-
移动光标到屏幕顶端H,移动到屏幕中间M,移动到底部L
-
读取当前字符,并移动到本屏幕内下一次出现的地方 *
-
读取当前字符,并移动到本屏幕内上一次出现的地方 #
二、查找替换
-
光标向后查找关键字 #或者g#
-
光标向前查找关键字 或者g
-
当前行查找字符 fx, Fx, tx, Tx
-
基本替换 :s/s1/s2 (将下一个s1替换为s2)
-
全部替换 :%s/s1/s2
-
只替换当前行 :s/s1/s2/g
-
替换某些行 :n1,n2 s/s1/s2/g
-
搜索模式为 /string,搜索下一处为n,搜索上一处为N
-
制定书签 mx, 但是看不到书签标记,而且只能用小写字母
-
移动到某标签处 `x,1旁边的键
-
移动到上次编辑文件的位置 `.
.代表一个任意字符 *代表一个或多个字符的重复
三、编辑操作
-
光标后插入a, 行尾插入A
-
后插一行插入o,前插一行插入O
-
删除字符插入s, 删除正行插入S
-
光标前插入i,行首插入I
-
删除一行dd,删除后进入插入模式cc或者S
-
删除一个单词dw,删除一个单词进入插入模式cw
-
删除一个字符x或者dl,删除一个字符进入插入模式s或者cl
-
粘贴p,交换两个字符xp,交换两行ddp
-
复制y,复制一行yy
-
撤销u,重做ctrl + r,重复.
-
智能提示 ctrl
n 或者 ctrlp -
删除motion跨过的字符,删除并进入插入模式 c{motion}
-
删除到下一个字符跨过的字符,删除并进入插入模式,不包括x字符 ctx
-
删除当前字符到下一个字符处的所有字符,并进入插入模式,包括x字符,cfx
-
删除motion跨过的字符,删除但不进入插入模式 d{motion}
-
删除motion跨过的字符,删除但不进入插入模式,不包括x字符 dtx
-
删除当前字符到下一个字符处的所有字符,包括x字符 dfx
-
如果只是复制的情况时,将12-17条中的c或d改为y
-
删除到行尾可以使用D或C
-
拷贝当前行 yy或者Y
-
删除当前字符 x
-
粘贴 p
-
可以使用多重剪切板,查看状态使用:reg,使用剪切板使用”,例如复制到w寄存器,”wyy,或者使用可视模式v”wy
-
重复执行上一个作用使用.
-
使用数字可以跨过n个区域,如y3x,会拷贝光标到第三个x之间的区域,3j向下移动3行
-
在编写代码的时候可以使用]p粘贴,这样可以自动进行代码缩进
-
>> 缩进所有选择的代码, << 反缩进所有选择的代码
-
gd 移动到光标所处的函数或变量的定义处
-
K 在man里搜索光标所在的词
-
合并两行 J
-
若不想保存文件,而重新打开 :e!
-
若想打开新文件 :e filename,然后使用ctrl + ^进行文件切换
四.窗口操作
-
分隔一个窗口:split或者:vsplit
-
创建一个窗口:new或者:vnew
-
在新窗口打开文件:sf {filename}
-
关闭当前窗口:close
-
仅保留当前窗口:only
-
到左边窗口 ctrl + w, h
-
到右边窗口 ctrl + w, l
-
到上边窗口 ctrl + w, k
-
到下边窗口 ctrl + w, j
-
到顶部窗口 ctrl + w, t
-
到底部窗口 ctrl + w, b
五.宏操作
-
开始记录宏操作q[a-z],按q结束,保存操作到寄存器[a-z]中
-
@[a-z]执行寄存器[a-z]中的操作
-
@@执行最近一次记录的宏操作
六、可视操作
-
进入块可视模式 ctrl + v
-
进入字符可视模式 v
-
进入行可视模式 V
-
删除选定的块 d
-
删除选定的块然后进入插入模式 c
-
在选中的块同是插入相同的字符 I<String>ESC
七.跳到声明
-
[[ 向前跳到顶格第一个{
-
[] 向前跳到顶格第一个}
-
]] 向后跳到顶格的第一个{
-
]] 向后跳到顶格的第一个}
-
[{ 跳到本代码块的开头
-
]} 跳到本代码块的结尾
八.挂起操作
-
挂起Vim ctrl + z 或者 :suspend
-
查看任务 在shell中输入 jobs
-
恢复任务 fg [job number](将后台程序放到前台)或者 bg [job number](将前台程序放到后台)
-
执行shell命令 :!command
-
开启shell命令 :shell,退出该shell exit
-
保存vim状态 :mksession name.vim
-
恢复vim状态 :source name.vim
-
启动vim时恢复状态 vim -S name.vim
vim配置
http://github.com/honmaple/vim
初始化文件
~/vim/vimrc
set nocompatible filetype off let g:is_mac = has('macunix') let g:is_linux = has('unix') && !has('macunix') && !has('win32unix') let g:is_windows = has('win32') || has('win64') if has("gui_running") let g:is_gui = 1 else let g:is_gui = 0 endif set rtp+=$HOME/.vim/bundle/Vundle.vim function s:require(file) let l:file = $HOME . '/.vim/plugins/' . a:file . '.vim' if filereadable(expand(l:file)) execute 'source ' . fnameescape(l:file) else echom l:file . ' does not exist.' endif endfunction call vundle#begin() Plugin 'VundleVim/Vundle.vim' call vundle#end() " required filetype plugin indent on " required " call plug#begin('~/.vim/plugged') " call plug#end() let require_list = [ \ 'init-gui', \ 'init-ui', \ 'init-editor', \ 'init-buffer', \ 'init-file', \ 'init-auto-insert', \ 'init-git', \ 'init-develop', \ 'init-python', \ 'init-html', \ 'init-text', \ 'init-keybind'] for file in require_list call s:require(file) endfor
全局设置
~/git/.vim/plugins/init-gui.vim
" 修改leader键 let mapleader = ',' let g:mapleader = ',' filetype on "启用文件类型侦测 filetype plugin on "针对不同的文件类型加载对应的插件 filetype plugin indent on "启用缩进 syntax on "代码高亮 syntax enable "代码高亮 set t_Co=256 set mouse=a "任何模式下启用鼠标 set mousehide "Hide the mouse cursor while typing scriptencoding utf-8 " 设置 退出vim后,内容显示在终端屏幕, 可以用于查看和复制, 不需要可以去掉 " set t_ti= t_te= if has('clipboard') if has('unnamedplus') " When possible use + register for copy-paste set clipboard=unnamed,unnamedplus else " On mac and Windows, use * register for copy-paste set clipboard=unnamed endif endif set shortmess+=filmnrxoOtT "去掉欢迎界面 set guifont=Monospace\ 12 set viewoptions=folds,options,cursor,unix,slash " Better Unix / Windows compatibility set virtualedit=onemore " Allow for cursor beyond last character set history=1000 " Store a ton of history (default is 20) "set spell "启用拼写检查 set hidden " Allow buffer switching without saving set iskeyword-=. " '.' is an end of word designator set iskeyword-=# " '#' is an end of word designator set iskeyword-=- " '-' is an end of word designator "set backup "设置备份文件 "if has('persistent_undo') " set undofile " So is persistent undo ... " set undolevels=1000 " Maximum number of changes that can be undone " set undoreload=10000 " Maximum number lines to save for undo on a buffer reload "endif set tabpagemax=15 " Only show 15 tabs set showmode " Display the current mode set cursorline "高亮光标所在行 set cuc "高亮光标所在列 set colorcolumn=80 highlight clear SignColumn " SignColumn should match background highlight clear LineNr " Current line number row will have same background color in relative mode "highlight clear CursorLineNr " Remove highlight color from current line number if has('cmdline_info') set ruler " Show the ruler set rulerformat=%30(%=\:b%n%y%m%r%w\ %l,%c%V\ %P%) " A ruler on steroids set showcmd " Show partial commands in status line and " Selected characters/lines in visual mode endif if has('statusline') set laststatus=2 "启用状态栏信息 set statusline=%<%f\ " Filename set statusline+=%w%h%m%r " Options " if !exists('g:override_bundles') "set statusline+=%{fugitive#statusline()} " Git Hotness "endif set statusline+=\ [%{&ff}/%Y] " Filetype set statusline+=\ [%{getcwd()}] " Current dir set statusline+=%=%-14.(%l,%c%V%)\ %p%% " Right aligned file nav info endif set backspace=indent,eol,start " Backspace for dummies set linespace=0 " No extra spaces between rows set number "显示行号 set relativenumber number "设置相对行号 au FocusLost * :set norelativenumber number au FocusGained * :set relativenumber " 插入模式下用绝对行号, 普通模式下用相对 autocmd InsertEnter * :set norelativenumber number autocmd InsertLeave * :set relativenumber function! NumberToggle() if(&relativenumber == 1) set norelativenumber number else set relativenumber endif endfunction set scrolloff=10 "在上下移动光标时,光标的上方或下方至少会保留显示的行数 set showmatch "高亮显示匹配的括号 set incsearch "在输入要搜索的文字时,实时匹配 set hlsearch "高亮搜索 "set winminheight=0 " Windows can be 0 line high set ignorecase "搜索模式里忽略大小写 set smartcase "如果搜索模式包含大写字符,不使用'ignorecase' 选项,只有在输入搜索模式并且打开 'ignorecase' 选项时才会使用 set wildmenu " 增强模式中的命令行自动完成操作 set wildmode=list:longest,full " Command <Tab> completion, list matches, then longest common part, then all. set whichwrap=b,s,h,l,<,>,[,] " Backspace and cursor keys wrap too "让Vim的补全菜单行为与一般IDE一致(参考VimTip1228) set completeopt=longest,menu set wildignore=*.o,*~,*.pyc,*.class "set scrolljump=5 " Lines to scroll when cursor leaves screen "set scrolloff=3 " Minimum lines to keep above and below cursor set list set listchars=tab:›\ ,trail:•,extends:#,nbsp:. " Highlight problematic whitespace set showcmd "在状态栏显示正在输入的命令 set nowrap "设置不自动换行 "set autoindent "打开自动缩进 set shiftwidth=4 "换行时自动缩进宽度,可更改(宽度同tabstop) set expandtab "将Tab键转换为空格 set tabstop=4 "设置Tab键的宽度,可以更改,如:宽度为2 "autocmd FileType haskell,rust setlocal nospell let g:FoldMethod = 0 fun! ToggleFold() if g:FoldMethod == 0 exe "normal! zM" let g:FoldMethod = 1 else exe "normal! zR" let g:FoldMethod = 0 endif endfun function! ToggleBG() let s:tbg = &background " Inversion if s:tbg == "dark" set background=light else set background=dark endif endfunction "set smartindent "启用智能对齐方式 "set shiftround "缩进时,取整 "set showtabline=1 "显示标签 set smarttab "指定按一次backspace就删除shiftwidth宽度 "set foldmethod=indent "indent 折叠方式 "set foldmethod=syntax "set foldenable "启用折叠 set foldmethod=marker " 启动 vim 时关闭折叠代码 set nofoldenable "set matchtime=5 "匹配括号高亮的时间(单位是十分之一秒) "set autoread "当文件在外部被修改,自动更新该文件 "set autowrite "自动保存 " set vb t_vb= "关闭提示音 " 显示/隐藏菜单栏、工具栏、滚动条,可用 Ctrl + F11 切换 if g:is_gui winpos 100 10 "指定窗口出现的位置,坐标原点在屏幕左上角 set guiheadroom=0 set lines=38 columns=120 set guioptions-=m set guioptions-=T set guioptions-=r set guioptions-=L nmap <silent> <c-F11> :if &guioptions =~# 'm' <Bar> \set guioptions-=m <Bar> \set guioptions-=T <Bar> \set guioptions-=r <Bar> \set guioptions-=L <Bar> \else <Bar> \set guioptions+=m <Bar> \set guioptions+=T <Bar> \set guioptions+=r <Bar> \set guioptions+=L <Bar> \endif<CR> endif
UI设置
~/.vim/plugins/init-ui.vim
Plugin 'altercation/vim-colors-solarized' Plugin 'tomasr/molokai' " Plugin 'spf13/vim-colors' Plugin 'bling/vim-airline' Plugin 'powerline/fonts' Plugin 'luochen1990/rainbow' Plugin 'Yggdroot/indentLine' Plugin 'liuchengxu/space-vim-dark' Plugin 'mhinz/vim-startify' if $TERM=~'cons25' colorscheme default elseif g:is_gui set background=dark " let g:solarized_termcolors=256 " let g:solarized_termtrans = 0 " colorscheme solarized let g:molokai_original = 1 colorscheme molokai else set background=dark let g:solarized_termcolors=256 let g:solarized_termtrans = 0 " colorscheme molokai colorscheme solarized endif " indentLine { " 用于显示对齐线 let g:indentLine_char = "┊" let g:indentLine_first_char = "┊" " 色块宽度 "let g:indent_guides_guide_size=1 " 设置终端对齐线颜色,如果不喜欢可以将其注释掉采用默认颜色 let g:indentLine_color_term = 256 " } " vim-airline { " let g:airline#extensions#tabline#enabled = 1 let g:airline_powerline_fonts=1 if !exists('g:airline_theme') let g:airline_theme = 'dark' endif " } " rainbow { let g:rainbow_active = 1 " }
编辑器设置
~/.vim/plugins/init-editor.vim
Plugin 'tpope/vim-surround' Plugin 'jiangmiao/auto-pairs' Plugin 'vim-scripts/matchit.zip' Plugin 'gcmt/wildfire.vim' Plugin 'godlygeek/tabular' Plugin 'majutsushi/tagbar' Plugin 'kristijanhusak/vim-multiple-cursors' Plugin 'tpope/vim-commentary' Plugin 'Chiel92/vim-autoformat' Plugin 'thinca/vim-quickrun' " tagbar { "nmap tb :TlistClose<CR>:TagbarToggle<CR> let g:tagbar_width=20 "设置窗口宽度 let g:tagbar_left=0 "在左侧窗口中显示 let g:tagbar_compact=1 "不显示帮助信息 "tagbar_map_togglefold 打开tagbar变量 "o" " set tags=./tags; " 自动切换目录为当前编辑文件所在目录 " au BufRead,BufNewFile,BufEnter * cd %:p:h " } " wildfire { let g:wildfire_objects = { \ "*" : ["i'", 'i"', "i\)", "i\]", "i\}", "ip","it"], \ "html,xml" : ["at","i>",'i"',"i\)"]} " } " vim-multiple-cursors { " default mapping let g:multi_cursor_next_key='<c-n>' let g:multi_cursor_prev_key='<c-p>' let g:multi_cursor_skip_key='<c-x>' let g:multi_cursor_quit_key='<esc>' " } " Tabularize { " 对齐 " nmap <Leader>a= :Tabularize /^[^=]*\zs=<CR> " vmap <Leader>a= :Tabularize /^[^=]*\zs=<CR> " } " quickrun { let g:quickrun_config = { \ "_" : { \ "outputter" : "message", \ }, \} let g:quickrun_no_default_key_mappings = 1 " }
Buffer设置
~/.vim/plugins/init-buffer.vim
Plugin 'bling/vim-bufferline' Plugin 'sjl/gundo.vim' " gundo { set undodir=~/.vim/.cache/undo set undofile set undolevels=10 " Maximum number of changes that can be undone set undoreload=10 " Maximum number lines to save for undo on a buffer reload " }
文件设置
~/.vim/plugins/init-file.vim
Plugin 'ctrlpvim/ctrlp.vim' Plugin 'tacahiroy/ctrlp-funky' Plugin 'dyng/ctrlsf.vim' Plugin 'scrooloose/nerdtree' Plugin 'jistr/vim-nerdtree-tabs' " ctrlp { let g:ctrlp_working_path_mode = 'ra' let g:ctrlp_map = '<c-p>' let g:ctrlp_cmd = 'CtrlP' let g:ctrlp_custom_ignore = { \ 'dir': '\v[\/]\.(git|hg|svn|rvm)$', \ 'file': '\v\.(exe|so|dll|zip|tar|tar.gz|pyc)$', \ } if executable('ag') let s:ctrlp_fallback = 'ag %s --nocolor -l -g ""' elseif executable('ack-grep') let s:ctrlp_fallback = 'ack-grep %s --nocolor -f' elseif executable('ack') let s:ctrlp_fallback = 'ack %s --nocolor -f' else let s:ctrlp_fallback = 'find %s -type f' endif if exists("g:ctrlp_user_command") unlet g:ctrlp_user_command endif let g:ctrlp_user_command = { \ 'types': { \ 1: ['.git', 'cd %s && git ls-files . --cached --exclude-standard --others'], \ 2: ['.hg', 'hg --cwd %s locate -I .'], \ }, \ 'fallback': s:ctrlp_fallback \ } " } " ctrlp-funky { " CtrlP extensions let g:ctrlp_extensions = ['funky'] let g:ctrlp_funky_syntax_highlight = 1 " } " ctrlsf { " 工程内查找文件内容,先安装ag /the_silver_searcher let g:ctrlsf_ackprg = 'ag' nmap <C-F>f <Plug>CtrlSFPrompt vmap <C-F>f <Plug>CtrlSFVwordPath vmap <C-F>F <Plug>CtrlSFVwordExec nmap <C-F>n <Plug>CtrlSFCwordPath nmap <C-F>p <Plug>CtrlSFPwordPath " } " NerdTree { " 自动开启nerdtree let g:nerdtree_tabs_open_on_console_startup = 0 let g:nerdtree_tabs_open_on_gui_startup = 0 "当打开vim且没有文件时自动打开NERDTree " if !g:is_gui " autocmd StdinReadPre * let s:std_in=1 " autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif " endif "显示文件 let NERDTreeShowFiles=1 let NERDTreeIgnore=['\.py[cd]$', '\~$', '\.swo$', '\.swp$', '^\.git$', '^\.hg$', '^\.svn$', '\.bzr$'] "显示隐藏文件 let NERDTreeShowHidden=0 let NERDTreeChDirMode=0 let NERDTreeQuitOnOpen=1 let NERDTreeMouseMode=2 let NERDTreeKeepTreeInNewTab=1 let NERDTreeWinSize=22 let NERDTreeWinPos="left" "高亮显示当前文件或目录 let NERDTreeHightCursorline=1 "不显示'Bookmarks' label 'Press ? for help' let NERDTreeMinimalUI=1 " s/v 分屏打开文件 let g:NERDTreeMapOpenSplit = 's' let g:NERDTreeMapOpenVSplit = 'v' " }