当前位置:网站首页>VIM configuration tutorial + source code

VIM configuration tutorial + source code

2020-11-08 15:49:00 Li Zhuming


Concept

Preface

  • Discard mouse , Increase of efficiency , This is just vim The standard target of
  • Customize your own editor , Increase of efficiency + The goal of this article is to make a fussy introduction
  • IDE Relative to the editor
    • The biggest advantage is that it integrates compilation 、 debugging 、 Intelligent completion 、 Syntax highlighting 、 Project management software development tools and environment
    • And these Vim Can be done through its powerful and rich plug-in extensions
  • It is not recommended to copy the source code directly , Unless you can't help it

vim advantage

  1. Pattern editor
  2. Highly scalable
  3. Highly customizable

vim To configure

  • Just to mention here neovim If you are interested, you can learn about .
  • principle : In the end, it's based on .vimrc To configure the vim

vim Configuration method 1

  • Reference resources Github:vimrc
    • Brief steps :(linux)
      1. stay home directories creating .vim_runtime Folder
      2. Run the command git clone --depth=1 https://github.com/amix/vimrc.git ~/.vim_runtime
      3. Run the command sh ~/.vim_runtime/install_awesome_vimrc.sh
      4. stay .vimrc Document and .vim_runtime Directory configuration vim.

vim Configuration method 2

  • Direct configuration .vimrc
Auto add header
  • Let's identify .sh File and add the source code of the corresponding file header information
  • Other documents ,.c .h .md And so on can refer to the following source code
"==============================================================================
" filetype  File identification , Add header 
"==============================================================================
filetype on
autocmd BufNewFile *.sh   exec ":call SetSHTitle()" 
"" Defined function SetTitle, Auto insert header  
func SetSHTitle() 
    call setline(1,         "/** @file         ".expand("%"))
    call append(line(".")+0, " *  @brief         Brief description ")
    call append(line(".")+1, " *  @details       Detailed instructions ") 
    call append(line(".")+2, " *  @author       lzm")
    call append(line(".")+3, " *  @date         ".strftime("%Y-%m-%d %H:%M:%S"))  " Creation time 
    call append(line(".")+6, " *")
    call append(line(".")+7," */")
    call append(line(".")+8,"")
    call append(line(".")+9,"#!/bin/sh")
    call append(line(".")+10, "")
endfunc 
  • design sketch
General Settings
  • Set as you like + shielding
"==============================================================================
" set  General Settings 
"==============================================================================
set nocompatible                " Are not compatible vi
syntax enable                   " Grammar highlighting enables 
syntax on                       " Syntax highlight on 
set hlsearch                    " Turn on highlight search 
set incsearch                   " Output string and search at the same time 
set ignorecase                  " Ignore case when searching 
set ruler                       " The coordinates of the cursor are displayed under the status bar in the lower right corner 
set showcmd                     " Show normal Command in mode 
set number                      " According to the line Numbers 
set relativenumber              " Relative line number 
set cmdheight=1                 " The command line height is 1 That's ok "
set noshowmode                  " No display '-- insert --'  etc. 
set autoindent                  " Automatic indentation 
set smartindent                 " Smart indent 
set cindent                     "c Language indentation 
set pastetoggle=<F4>            " System paste
set mouse=i                     " Only in insert mode  You can use mouse

set autoread                    " When the file is changed externally , vim Automatically loaded 
set showmatch                   " Show matching brackets 
set matchtime=0                 " Show the time of the matching bracket , Company : One tenth of a second 
set t_Co=256                    "vim The color is 256 color 
set cursorline                  " Highlight the current cursor line 
set cursorcolumn                " Highlight the current cursor column 
autocmd InsertEnter * se cul
set shortmess=atI
set scrolloff=3                 " Cursor moves to buffer Keep the top and bottom of 3 Row distance 
set laststatus=2                " Specifies when the status bar is displayed for the last window , 2 There will always be 
set cinoptions=g0,:0,N-s,(0     " Set up C/C++ How to indent a language , unkown
set tabstop=4                   " Read to the file \t when , The blank length of the explanation 
set softtabstop=4               " In editing actions ( Press tab or backspace key ) when , The length of space entered or deleted 
set shiftwidth=4                " The length of white space when indenting automatically 
set expandtab                   " take tab convert to space, Will be stored in the file space
set smarttab
" set list                      " Show tab The key is ^I,  Behavior shows $
set listchars=tab:>-,trail:-    "tab Is shown as ">---"  The extra white space at the end of the line is shown as "-"
set history=1000                " Command history number 
set langmenu=en_US.UTF-8        " Menu languages 
set helplang=en                 " Use Chinese documents cn, In English, it means en
set autowrite                   " Save automatically ,  Otherwise switch buffer, After the file, you will be prompted that it has not been saved 
set magic                       " When matching regular expressions (\m): except $.*^ Besides, the metacharacters of other regular expressions should be backslashed \
set confirm                     " The confirmation option always appears , Such as implementation :qa When ordered 
set nobackup                    "vim When you overlay a file, do not keep a backup 
set noswapfile                  " No swap space 
set wildmenu                    " Complete the relevant ,unkown
set completeopt=longest,menu    " Show completion list 
set backspace=indent,eol,start  "indent take 4 Space indent when indent is removed ,eol Delete to the previous line 
set report=0                    " When using :commands On command , The line that always reports the file has been changed 
set fillchars=vert:\ ,stl:\ ,stlnc:\  " Set the split line between windows when multiple windows are used 
" set iskeyword+=.                " Additional . To judge a word
set termencoding=utf-8          " The character encoding used by the terminal 
set encoding=utf8               "vim Inside buffer The character encoding used 
set fileencodings=utf8,ucs-bom,gbk,cp936,gb2312,gb18030 " Judge the file character code in turn 
" set foldmethod=indent         " Indentation based folding 
set foldmethod=syntax           " Grammar based folding 
set nofoldenable                " start-up  vim  When the collapse code is closed 
" set nowrap                      " No folding 
set viminfo+=n~/.cache/vim/.viminfo

let $BASH_ENV="~/.bash_aliases" " Add external commands to alias
let mapleader = ","             " modify <leader> The key is ','
vnoremap <leader>y "+y

filetype plugin on              " Read the plug-in according to the file type 
filetype indent on              " Using indented files 

"  Tag jump shortcut key , stay gnome-terminal Invalid in 
" normal no recursive map
" insert no recursive map
nnoremap <C-c> :tabclose<cr>
nnoremap <C-x> :tabonly<cr>
nnoremap <C-k> :tabs<cr>
nnoremap <C-n> :tabnew<cr>
nnoremap <C-h> :tabprevious<cr>
nnoremap <C-l> :tabnext<cr>

"  The cursor blinks , In insert mode, it is ‘|’, stay norrmal  In mode, it is square 
"  If it doesn't flash , It will be changed to the following number 
let &t_SI = "\<Esc>[5 q"                     " 6
let &t_SR = "\<Esc>[3 q"                     " 4
let &t_EI = "\<Esc>[1 q"                     " 2
plug-in unit **
  • stay linux Operate under the platform

  • Be careful When using plug-ins , You need to know what plug-ins you use , prevent General Settings And Plug in settings Conflict

    • Such as imap <C-h> <LEFT> and Plugin 'jiangmiao/auto-pairs'
      • imap <C-h> <LEFT>
        • It's in edit mode ,ctrl+h For the left arrow key
      • Plugin 'jiangmiao/auto-pairs'
        • In edit mode , Will be able to ctrl+h Map to delete key , So that the end result will be Delete
        • resolvent , Just comment it out
  • use Vundle Management plug-in

    • install Vundle
      • Run the command git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim that will do
    • Plug in type and Application
      1. Github Plugins on
        • Use format :Plugin 'vim-scripts/ Plugin warehouse name '
        • Example :Plugin 'tpope/vim-fugitive'
      2. Github On vim-scripts Plugins in the warehouse
        • Use format :Plugin ' The plug-in name '
          • It's actually Plugin 'vim-scripts/ Plugin warehouse name ' Just the user name here can be omitted
        • Example :Plugin 'L9'
      3. from Git Support but no longer github The plug-in repository on
        • Use format :Plugin 'git clone Back address ''
        • Example :`Plugin 'git://git.wincent.com/command-t.git'
      4. The local Git Warehouse ( For example, your own plug-in )
        • Use format :Plugin 'file:///+ Absolute path of local plug-in warehouse ''
        • Example :Plugin 'file:///home/gmarik/path/to/plugin'
      5. The plug-ins are in the subdirectory of the repository .
        • Use format : Specify the path correctly to set runtimepath.
        • Example : Plug in sparkup/vim Under the table of contents : Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
      6. If you have already installed this plug-in , The following format can be used to avoid naming conflicts
        • Example :L9:Plugin 'ascenator/L9', {'name': 'newL9'}
  • Plug in management source code

"==============================================================================
" Vundle start   Plug-in management 
"==============================================================================
"filetype off                    "  necessary 

"  Settings include vundle Related to initialization runtime path

"  Example + Format begins ==================================================
"  The plug-in commands are placed in vundle#begin and vundle#end Between .
" 1.Github Plugins on 
"  The format is  Plugin ' user name / Plugin warehouse name '
"Plugin 'tpope/vim-fugitive'
" 2. come from  http://vim-scripts.org/vim/scripts.html  Plug in for 
" Plugin ' The plug-in name '  It's actually  Plugin 'vim-scripts/ Plugin warehouse name '  Just the user name here can be omitted 
"Plugin 'L9'
" 3. from Git Support but no longer github The plug-in repository on  Plugin 'git clone  Back address '
"Plugin 'git://git.wincent.com/command-t.git'
" 4. The local Git Warehouse ( For example, your own plug-in ) Plugin 'file:///+ Absolute path of local plug-in warehouse '
"Plugin 'file:///home/gmarik/path/to/plugin'
" 5. The plug-ins are in the subdirectory of the repository .
"  Specify the path correctly to set runtimepath.  The following example plug-ins are in sparkup/vim Under the table of contents 
"Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" 6. install L9, If you have already installed this plug-in , The following format can be used to avoid naming conflicts 
"Plugin 'ascenator/L9', {'name': 'newL9'}
"  Example + Format END ==================================================


set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()                             "  must   Plug in starts ==================================================

Plugin 'VundleVim/Vundle.vim'                   "  must   Give Way vundle Manage plug-in versions 
Plugin 'Yggdroot/indentLine'                    " Indent indicator line 
"Plugin 'suan/vim-instant-markdown'             " markdown Live browsing 
Plugin 'vim-airline/vim-airline'                " Beautiful interface ,powerline
"Plugin 'fatih/vim-go'
"  from Git Support but no longer github The plug-in repository on  Plugin 'git clone  Back address '
Plugin 'L9'
Plugin 'terryma/vim-multiple-cursors'   "  Multi cursor input 
Plugin 'jiangmiao/auto-pairs'                   " Auto complete bracket pairs . Input `[`,  It will automatically input ']' "<c-h>  Will be mapped to  <DELETE>, If you need to , You can comment out 
"Plugin 'tpope/vim-commentary'                   " Annotation plug-ins :gcc
Plugin 'scrooloose/nerdtree'                    " Directory tree 
Plugin 'Valloric/YouCompleteMe'
"Plugin 'rdnetto/YCM-Generator'                  "YouCompleteMe  Configuration Generator 
"Plugin 'vim-scripts/SQLComplete.vim'
"Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}      "html  Shortcut key 
Plugin 'Valloric/MatchTagAlways'                "html, xml Show matching tags :<leader>%
Plugin 'docunext/closetag.vim'                  "html, xml Closed label ,<C-_>  It doesn't work 
Plugin 'godlygeek/tabular'                      " Text alignment ::Tab /=  、 :Tab /|
"Plugin 'tpope/vim-fugitive'                     " stay vim It can be implemented easily git command 
Plugin 'tpope/vim-surround'                     " It is convenient to modify and add brackets to 
Plugin 'tpope/vim-repeat'
Plugin 'tpope/vim-endwise'                      " stay c To complete #if Medium #endif etc. 
Plugin 'ctrlpvim/ctrlp.vim'                     " Search for files <C-f>
Plugin 'majutsushi/tagbar'                      " Show ctags
Plugin 'octol/vim-cpp-enhanced-highlight'       "C++ Enhance highlight 
"Plugin 'vim-airline/vim-airline-themes'         " Beautiful interface ,powerline
"Plugin 'ryanoasis/vim-devicons'                 " Beautification interface 
"Plugin 'gorodinskiy/vim-coloresque'             " Color , Such as  blue #00f
"Plugin 'will133/vim-dirdiff'                    " You can compare the catalog 
Plugin 'haya14busa/incsearch.vim'               " Highlight and match all characters at the same time 
Plugin 'mhinz/vim-startify'                     " The terminal runs directly vim When the menu interface is displayed 
Plugin 'mileszs/ack.vim'                        "ag command 

Plugin 'davidhalter/jedi-vim'                   "python
"Plugin 'Shougo/echodoc.vim'                     " Display command parameters 

filetype plugin indent on                       "  must   load vim Built in scripts related to the syntax and file type of the plug-in 
call vundle#end()                               "  must   plug-in unit END ==================================================


"filetype plugin on                     "  Ignore plugin change indent 
mapping
  • Hotkey mapping
"==============================================================================
" nerdtree
"==============================================================================
map <F2> :NERDTreeToggle<CR>
imap <F2> <ESC> :NERDTreeToggle<CR>

"autocmd vimenter * if !argc() | NERDTree | endif
"autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
"ctrl+h  For the left arrow key 
imap <C-h> <LEFT>
"ctrl+l  Right arrow key 
imap <C-l> <RIGHT>
"ctrl+k  For the up arrow key 
imap <C-k> <UP>
"ctrl+j For the down arrow key 
imap <C-j> <DOWN>
"ctrl+d For delete key 
imap <C-d> <DELETE>
YouCompleteMe plug-in unit
  • YouCompleteMe The plug-in configuration
"==============================================================================
" YouCompleteMe
"==============================================================================
let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py' " Global profile 
let g:ycm_confirm_extra_conf = 0                        " There is no need to confirm when loading the configuration file manually 
let g:ycm_error_symbol = '*'                            "error  Symbol 
let g:ycm_warning_symbol = ''                          "warning Symbol 
let g:ycm_seed_identifiers_with_syntax = 1              " Keyword completion 
let g:ycm_complete_in_comments = 1                      " In the comment input, you can complete 
let g:ycm_complete_in_strings = 1                       " In string input, you can complete 
let g:ycm_key_invoke_completion = '<C-n>'               " Change manual trigger completion button , Default <C-space>
let g:ycm_collect_identifiers_from_tags_files = 1       " Based on the open tag The completion of 
let g:ycm_key_list_select_completion = ['<Down>']       " Cancel '<Tab>'
" let g:ycm_auto_trigger = 0                              " It doesn't automatically complete ,  Need to press <C-n>
" let g:ycm_semantic_triggers = {}
" let g:ycm_semantic_triggers.c = ['->', '.', ' ', '(', '[', '&',']']

nnoremap <leader>u :YcmCompleter GoToDeclaration<CR>    " Jump to the statement 
nnoremap <leader>o :YcmCompleter GoToDefinition<CR>     " Jump to the definition 
nnoremap <leader>i :YcmCompleter GoToInclude<CR>        " Jump to header file 
"nmap <F5> :YcmDiags<CR>                                 " Custom shortcut <F5> Displays warning and error messages 

"let g:ycm_add_preview_to_completeopt = 1                " A preview window opens and closes 
"let g:ycm_autoclose_preview_window_after_completion = 0 " That is, automatically close the preview window 
"let g:ycm_autoclose_preview_window_after_insertion = 1  " Automatically close preview window in return to normal After the model 
"let g:ycm_cache_omnifunc = 0                            " Every time you regenerate a match , Disable caching matches 

" tagbar
let g:tagbar_ctags_bin = '/usr/bin/ctags'
let g:tagbar_width = 30
map <F3> :TagbarToggle<CR>
imap <F3> <ESC> :TagbarToggle<CR>

" cpp_class_scope_highlight
let g:cpp_class_scope_highlight = 1
let g:cpp_experimental_template_highlight = 1
let c_no_curly_error = 1

" colorscheme
"colorscheme default
" colorscheme monokai
"let g:solarized_termcolors=256
"set background=light                " Set a dark background color ,  The command needs to be in systax enable After the call 
"colorscheme solarized               " Set the color scheme to solarized
Other configuration
  • Other configuration
"==============================================================================
" Other
"==============================================================================
let g:airline#extensions#tabline#enabled = 1
if !exists('g:airline_symbols')
    let g:airline_symbols = {}
endif

版权声明
本文为[Li Zhuming]所创,转载请带上原文链接,感谢