Introduction to VIM Usage and configuration

Source: Internet
Author: User



Vim related knowledge accumulation, continuous update.


Common Operation General Mode
    • n1,n2s/word1/word2/gN1 row and N2 between the lines to find Word1, replaced by Word2
    • 1,$s/word1/word2/gFirst line to last line find Word1, replace with Word2
    • N[Enter]Move n rows down
    • 0: Move to the last character of the line
    • $: Move to the first character of the line
    • G: Last line
    • GG: First line
    • x&x:x equivalent to [del],x equivalent to [Backspace]
    • DD: Delete the row
    • YY: Copy the row
    • Nyy: Copy n rows down
    • P&p:p Paste to the next line, P to the previous line
    • U: Restore previous action
    • /: Find, can use "n" key (next), jump between results
Switch to the command line
    • :!command temporarily leaves the execution command for display results
    • Set NU Display line number
Block selection


Press V or V or [ctrl]-v, this time the cursor is going to start the anti-white, the Y key can be copied, the D key can be deleted


Replace specified characters in text


in command mode


:% s / character to be replaced / new character / g


% is a regular expression that indicates the end of the text, S is the substitute first character, and G represents the global


Environment settings and logging


Vim has a lot of environment settings parameters, if you want to know the current setting value, can be in general mode when the inputset allto consult, but there are too many settings options, here are some of the more commonly used in some simple set of values



You can also use configuration files to visually specify the VIM operating environment that we are accustomed to. The entire VIM setting value is generally placed in the/ETC/VIMRC file, however, it is not recommended to modify it directly. It is recommended to modify~/.vimrcthis file (default does not exist, please manually create it yourself), the desired setting values are included.


set hlsearch "Highlight highlight
set backspace = 2 "Delete with backspace at any time
set autoindent "autoindent
"set ruler" class displays the status of the last line
set showmode "the status of the line in the lower left corner
set nu "line number
set bg = dark "Display different background shades
syntax on "syntax highlighting


A good VIM environment configuration file


 
"basic
imap yy <Esc>
set nu
syntax on
set tabstop=4
set softtabstop=4
set shiftwidth=4
set autoindent
set cindent
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s
map <C-a> "+yG
map <C-x> "+p

"tab shortcut
map <A-1> 1gt
map <A-1> 2gt
map <A-1> 3gt
map <A-1> 4gt
map <A-1> 5gt
map <A-1> 6gt
map <A-1> 7gt
map <A-1> 8gt
map <A-1> 9gt "window shortcut
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l

"color scheme set t_Co=256 colorscheme desertEx "encoding
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,gbk,euc-jp,euc-kr,latin1
if has("win32")
    set fileencoding=chinese
    " fix menu gibberish source $VIMRUNTIME/delmenu.vim source $VIMRUNTIME/menu.vim " fix console gibberish
    language messages zh_CN.utf-8
else
    set termencoding=utf-8
    set fileencoding=utf-8
endif

"tags
map <F5> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR> "/usr/include/tags
if has("win32")
    set tags+=E:\workspace\linux\tags  " tags for /usr/include/ else set tags+=/usr/include/tags " tags for /usr/include/
endif
set tags+=tags                         " tags for current project "omnioppcoplete
set nocp
filetype plugin on
set completeopt=longest,menu      " I really HATE the preview window!!! let OmniCpp_NameSpaceSearch=1 " 0: namespaces disabled
                                  " 1: search namespaces in the current buffer [default] " 2: search namespaces in the current buffer and in included files

let OmniCpp_GlobalScopeSearch=1   " 0: disabled 1:enabled let OmniCpp_ShowAccess=1 " 1: show access
let OmniCpp_ShowPrototypeInAbbr=1 " 1: display prototype in abbreviation let OmniCpp_MayCompleteArrow=1 " autocomplete after ->
let OmniCpp_MayCompleteDot=1      " autocomplete after . let OmniCpp_MayCompleteScope=1 " autocomplete after ::

autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete

"compile if(has("win32") || has("win95") || has("win64") || has("win16")) let g:iswindows=1 else let g:iswindows=0 endif "单个文件编译
map <F9> :call Do_OneFileMake()<CR>
function Do_OneFileMake()
    if expand("%:p:h")!=getcwd()
        echohl WarningMsg | echo "Fail to make! This file is not in the current dir! Press <F7> to redirect to the dir of this file." | echohl None
        return
    endif
    let sourcefileename=expand("%:t")
    if (sourcefileename=="" || (&filetype!="cpp" && &filetype!="c"))
        echohl WarningMsg | echo "Fail to make! Please select the right file!" | echohl None
        return
    endif
    let deletedspacefilename=substitute(sourcefileename,‘ ‘,‘‘,‘g‘)
    if strlen(deletedspacefilename)!=strlen(sourcefileename)
        echohl WarningMsg | echo "Fail to make! Please delete the spaces in the filename!" | echohl None
        return
    endif
    if &filetype=="c"
        if g:iswindows==1
            set makeprg=gcc\ -o\ %<.exe\ %
        else
            set makeprg=gcc\ -o\ %<\ %
        endif
    elseif &filetype=="cpp"
        if g:iswindows==1
            set makeprg=g++\ -o\ %<.exe\ %
        else
            set makeprg=g++\ -o\ %<\ %
        endif
        "elseif &filetype=="cs" "set makeprg=csc\ \/nologo\ \/out:%<.exe\ %
    endif
    if(g:iswindows==1)
        let outfilename=substitute(sourcefileename,‘\(\.[^.]*\)‘ ,‘.exe‘,‘g‘)
        let toexename=outfilename
    else
        let outfilename=substitute(sourcefileename,‘\(\.[^.]*\)‘ ,‘‘,‘g‘)
        let toexename=outfilename
    endif
    if filereadable(outfilename)
        if(g:iswindows==1)
            let outdeletedsuccess=delete(getcwd()."\\".outfilename)
        else
            let outdeletedsuccess=delete("./".outfilename)
        endif
        if(outdeletedsuccess!=0)
            set makeprg=make
            echohl WarningMsg | echo "Fail to make! I cannot delete the ".outfilename | echohl None
            return
        endif
    endif
    execute "silent make"
    set makeprg=make
    execute "normal :"
    if filereadable(outfilename)
        if(g:iswindows==1)
            execute "!".toexename
        else
            execute "!./".toexename
        endif
    endif
    execute "copen"
endfunction
"进行make的设置
map <F6> :call Do_make()<CR>
map <c-F6> :silent make clean<CR>
function Do_make() set makeprg=make
    execute "silent make" execute "copen" endfunction "debug
map <F8> :call Do_OneFileDebug()<CR>
function Do_OneFileDebug()
    if expand("%:p:h")!=getcwd()
        echohl WarningMsg | echo "Fail to make! This file is not in the current dir! Press <F7> to redirect to the dir of this file." | echohl None
        return
    endif
    let sourcefileename=expand("%:t")
    if (sourcefileename=="" || (&filetype!="cpp" && &filetype!="c"))
        echohl WarningMsg | echo "Fail to make! Please select the right file!" | echohl None
        return
    endif
    let deletedspacefilename=substitute(sourcefileename,‘ ‘,‘‘,‘g‘)
    if strlen(deletedspacefilename)!=strlen(sourcefileename)
        echohl WarningMsg | echo "Fail to make! Please delete the spaces in the filename!" | echohl None
        return
    endif
    if &filetype=="c"
        if g:iswindows==1
            set makeprg=gcc\ -g\ -o\ %<.exe\ %
        else
            set makeprg=gcc\ -g\ -o\ %<\ %
        endif
    elseif &filetype=="cpp"
        if g:iswindows==1
            set makeprg=g++\ -g\ -o\ %<.exe\ %
        else
            set makeprg=g++\ -g\ -o\ %<\ %
        endif
        "elseif &filetype=="cs" "set makeprg=csc\ \/nologo\ \/out:%<.exe\ %
    endif
    if(g:iswindows==1)
        let outfilename=substitute(sourcefileename,‘\(\.[^.]*\)‘ ,‘.exe‘,‘g‘)
        let toexename=outfilename
    else
        let outfilename=substitute(sourcefileename,‘\(\.[^.]*\)‘ ,‘‘,‘g‘)
        let toexename=outfilename
    endif
    if filereadable(outfilename)
        if(g:iswindows==1)
            let outdeletedsuccess=delete(getcwd()."\\".outfilename)
        else
            let outdeletedsuccess=delete("./".outfilename)
        endif
        if(outdeletedsuccess!=0)
            set makeprg=make
            echohl WarningMsg | echo "Fail to make! I cannot delete the ".outfilename | echohl None
            return
        endif
    endif
    execute "silent make"
    set makeprg=make
    execute "normal :"
    if filereadable(outfilename)
        if(g:iswindows==1)
            execute "!gdb\ ".toexename
        else
            execute "!gdb\ ./".toexename
        endif
    endif
    execute "copen"
endfunction


Introduction to VIM Usage and configuration


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.