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
- 其他插件
参考地址