vi常用命令

来源:互联网 发布:淘宝ipad pro鉴别 编辑:程序博客网 时间:2024/06/08 18:59

常用命令

1.删除或剪切当前行
dd
2.张贴
p
3.合并到当前行的下一行
o
4.返回上个编辑处
"
5.转到定义
gd
6.退出
:q
7.保存
:w
8.强制退出
:q!
9.删除全部
:1,$d 
10.到达全文最后
:G
11.显示行号
:set nu
12.使用utf8的编码
:set encoding=utf-8
13.文件编码
:set fenc=utf-8
14.全部替换
:%s/abc/cba/g
15.替换
:s/abc/cba/g
16.编辑一个文件
:e dir/file
17.打开目录
:E dir
18.打开quickfix
:copen
19.列出所有buffer
:buffers
20.buffer切换
Ctrl+Tab或者:bn
21.窗口操作
新建窗口Ctrl+w n
关闭窗口Ctrl+w c
窗口切换Ctrl+w w
22.查找目录

:vimgrep str dir/*.cpp
:vimgrep str dir/*/*.cpp(包括子目录)

23.切换工作目录

:cd workspacedir

可以直接在_vimrc中设置环境变量,例如:

let $workspace1 = "D:/workspace"

let $workspace2 = "E:/workspace"

cd $workspace1 "启动时切换到工作目录

配置文件_vimrc: 

 

set nocompatiblesource $VIMRUNTIME/vimrc_example.vimsource $VIMRUNTIME/mswin.vimbehave mswinset diffexpr=MyDiff()function MyDiff()  let opt = '-a --binary '  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif  let arg1 = v:fname_in  if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif  let arg2 = v:fname_new  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif  let arg3 = v:fname_out  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif  let eq = ''  if $VIMRUNTIME =~ ' '    if &sh =~ '/<cmd'      let cmd = '""' . $VIMRUNTIME . '/diff"'      let eq = '"'    else      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '/diff"'    endif  else    let cmd = $VIMRUNTIME . '/diff'  endif  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eqendfunction"==================================================================================================="                                  基本配置"===================================================================================================if(has("win32") || has("win95") || has("win64") || has("win16")) "判定当前操作系统类型    let g:iswindows=1else    let g:iswindows=0endif"字体set guifont=Consolas:h11:cANSI"配色方案"colorscheme desert"显示行号set nu"不备份set nobackupset nowritebackup"隐藏工具栏set guioptions-=T"隐藏菜单栏set guioptions-=m"启动gVIM时最大化"au GUIEnter * simalt ~x"搜索时全小写相当于不区分大小写,只要有一个大写字母出现,则区分大小写set ignorecase smartcase"自动判断编码方式"set fileencoding=gb18030"set fileencodings=ucs-bom,gb18030,utf-8,default"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" vim在windows下的编码设置""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""set encoding=utf-8set fileencodings=utf-8,chinese,latin-1if has("win32")set fileencoding=chineseelseset fileencoding=utf-8endif"解决菜单乱码source $VIMRUNTIME/delmenu.vimsource $VIMRUNTIME/menu.vim"解决consle输出乱码language messages zh_CN.utf-8" 突出显示当前行set cursorline"Set to auto read when a file is changed from the outsideif exists("&autoread")    set autoreadendif"智能补全  imap <M-/> <C-X><C-O>  "编译和运行C/C++、Python程序func CompileRun()      exec "w"     if &filetype == 'c'        exec "!gcc -Wl,-enable-auto-import % -g -o %<.exe"        exec "!%<.exe"  elseif &filetype == 'cpp'             exec "!g++ -Wl,-enable-auto-import % -g -o %<.exe"        exec "!%<.exe"    elseif &filetype == 'python'         exec "!python %"      endifendfunc    "定义Debug函数,用来调试程序  func Debug()      exec "w"    if &filetype == 'c'          exec "!gcc % -g -o %<.exe"          exec "!gdb %<.exe"      elseif &filetype == 'cpp'          exec "!g++ % -g -o %<.exe"          exec "!gdb %<.exe"      endif  endfunc map <F5> :call CompileRun()<CR>map <F6> :call Debug()<CR>"==================================================================================================="                                        插件配置"===================================================================================================let Tlist_Show_One_File = 1            "不同时显示多个文件的tag,只显示当前文件的let Tlist_Exit_OnlyWindow = 1          "如果taglist窗口是最后一个窗口,则退出vimlet Tlist_Use_Right_Window = 1         "在右侧窗口中显示taglist窗口"winManager 配置let g:winManagerWindowLayout='FileExplorer'"|TagList'"按c时打开nmap c :WMToggle<cr> :Tlist<cr>"全屏if has('gui_running') && g:iswindows==1    map <F11> :call libcallnr("gvimfullscreen.dll", "ToggleFullScreen", 0)<CR>endif"cscope配置nmap <C-@>s :cs find s <C-R>=expand("<cword>")<CR><CR>:copen<CR>nmap <C-@>g :cs find g <C-R>=expand("<cword>")<CR><CR>nmap <C-@>c :cs find c <C-R>=expand("<cword>")<CR><CR>:copen<CR>nmap <C-@>t :cs find t <C-R>=expand("<cword>")<CR><CR>:copen<CR>nmap <C-@>e :cs find e <C-R>=expand("<cword>")<CR><CR>:copen<CR>nmap <C-@>f :cs find f <C-R>=expand("<cfile>")<CR><CR>:copen<CR>nmap <C-@>i :cs find i ^<C-R>=expand("<cfile>")<CR>{1}lt;CR>:copen<CR>nmap <C-@>d :cs find d <C-R>=expand("<cword>")<CR><CR>:copen<CR>"cscope调用函数function Do_CsTag()    let dir = getcwd()    if filereadable("tags")        if(g:iswindows==1)            let tagsdeleted=delete(dir."//"."tags")        else            let tagsdeleted=delete("./"."tags")        endif        if(tagsdeleted!=0)            echohl WarningMsg | echo "Fail to do tags! I cannot delete the tags" | echohl None            return        endif    endif    if has("cscope")        silent! execute "cs kill -1"    endif    if filereadable("cscope.files")        if(g:iswindows==1)            let csfilesdeleted=delete(dir."//"."cscope.files")        else            let csfilesdeleted=delete("./"."cscope.files")        endif        if(csfilesdeleted!=0)            echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.files" | echohl None            return        endif    endif    if filereadable("cscope.out")        if(g:iswindows==1)            let csoutdeleted=delete(dir."//"."cscope.out")        else            let csoutdeleted=delete("./"."cscope.out")        endif        if(csoutdeleted!=0)            echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.out" | echohl None            return        endif    endif    if(executable('ctags'))        "silent! execute "!ctags -R --c-types=+p --fields=+S *"        silent! execute "!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ."    endif    if(executable('cscope') && has("cscope") )        if(g:iswindows!=1)            silent! execute "!find . -name '*.h' -o -name '*.c' -o -name '*.cpp' -o -name '*.java' -o -name '*.cs' > cscope.files"        else            silent! execute "!dir /s/b *.c,*.cpp,*.h,*.java,*.cs >> cscope.files"        endif        silent! execute "!cscope -b"        execute "normal :"        if filereadable("cscope.out")            execute "cs add cscope.out"        endif    endifendfunction"按F12是打开文件管理和taglist,并自动在当前目录生成cscope的数据库map <F12> :WMToggle<cr> :call Do_CsTag()<CR>"F3转到定义map <F3> :cs find g <C-R>=expand("<cword>")<CR><CR>"F4查找所有引用map <F4> :cs find c <C-R>=expand("<cword>")<CR><CR>:copen<CR>"tags调用函数function Do_Tag()    silent! execute "!ctags -R"endfunctionmap <F9> :WMToggle<cr> :call Do_Tag()<CR>:nmap ,s :source $VIM/_vimrc " 译释:nmap 是绑定一个在normal模式下的快捷键:nmap ,v :e $VIM/_vimrc:nmap ,f :update<CR>:silent !start c://progra~1//intern~1//iexplore.exe file://%:p



原创粉丝点击