Easy to use vim configuration and explanation,. vimrc makes office more efficient

Keywords: Linux vim

Many Xiaobai should start using vim just like me. They don't know how to configure it. They have been plagued by problems such as not highlighting SV syntax and boring background color. The following is the summary and explanation of the boss. An efficient vim configuration is of great help to the office!

. vimrc configuration

Powerful vim configuration file makes programming more casual - ma6174 - blog Garden

It took a long time to sort it out. I feel it's very convenient to use. Share it.

My vim configuration has the following advantages:

1. Press F5 to directly compile and execute C, C + +, java code and execute shell script, and press F8 to debug C and C + + code

2. Automatically insert the file header. When creating C and C + + source files, automatically insert the header: including file name, author, contact information, establishment time, etc. readers can change it according to their needs

3. Map "Ctrl + A" to select all and copy the shortcut keys to facilitate code copying

4. Press "F2" to directly eliminate empty lines in the code

5. "F3" can list the current directory file and open the tree file directory

6. Support mouse selection and direction key movement

7. Highlight the code, indent automatically, display the line number and display the status line

8. Press "Ctrl + P" to complete automatically

9. [], {}, (), '', ', etc. are automatically completed

10. Other functions readers can study the following documents

  vim is originally a powerful and convenient editor. With my code, it will certainly be even stronger. Maybe readers use other programming languages and can modify them according to their own needs. Comments have been added in the configuration file.

If the reader is interested, copy the following code directly to the text file, then rename the file ". vimrc" (don't forget the previous "."), and then put the file under the root directory of the user's folder. Reopen vim to see the effect.

The following is a simplified vim configuration file without plug-ins. You can use it by saving it to your own. vimrc file.

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Display correlation  
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"set shortmess=atI   " Don't show the prompt of assisting Ugandan children when starting  
"winpos 5 5          " Set window position  
"set lines=40 columns=155    " Set window size  
"set nu              " set number   
set go=             " Do not use graphic buttons  
"color asmanian2     " Set background theme  
set guifont=Courier_New:h10:cANSI   " Set font  
"syntax on           " Syntax highlighting   
autocmd InsertLeave * se nocul  " Highlight the current row in a light color  
autocmd InsertEnter * se cul    " Highlight the current row in a light color  
"set ruler           " Display ruler  
set showcmd         " The input command is displayed and can be seen clearly  
"set cmdheight=1     " The height of the command line (in the status line), set to 1  
"set whichwrap+=<,>,h,l   " allow backspace And cursor keys across row boundaries(Not recommended)  
"set scrolloff=3     " Move cursor to buffer Maintain a distance of 3 lines at the top and bottom of the  
set novisualbell    " Do not blink(don't get it)  
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}   "What the status line displays  
set laststatus=1    " Start display status line(1),Always show status line(2)  
set foldenable      " Allow folding  
set foldmethod=manual   " Manual folding  
"set background=dark "The background is black 
set nocompatible  "Get rid of annoying about vi Consistency mode to avoid some problems in previous versions bug And limitations  
" Show Chinese help
if version >= 603
    set helplang=cn
    set encoding=utf-8
endif
" Set Color Scheme 
"colorscheme murphy
"typeface 
"if (has("gui_running")) 
"   set guifont=Bitstream\ Vera\ Sans\ Mono\ 10 
"endif 

 
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936
set fileencoding=utf-8
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""New file title""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"newly build.c,.h,.sh,.java Files, inserting headers automatically 
autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()" 
""Define function SetTitle,Auto insert header 
func SetTitle() 
    "If the file type is.sh file 
    if &filetype == 'sh' 
        call setline(1,"\#########################################################################") 
        call append(line("."), "\# File Name: ".expand("%")) 
        call append(line(".")+1, "\# Author: ma6174") 
        call append(line(".")+2, "\# mail: ma6174@163.com") 
        call append(line(".")+3, "\# Created Time: ".strftime("%c")) 
        call append(line(".")+4, "\#########################################################################") 
        call append(line(".")+5, "\#!/bin/bash") 
        call append(line(".")+6, "") 
    else 
        call setline(1, "/*************************************************************************") 
        call append(line("."), "    > File Name: ".expand("%")) 
        call append(line(".")+1, "    > Author: ma6174") 
        call append(line(".")+2, "    > Mail: ma6174@163.com ") 
        call append(line(".")+3, "    > Created Time: ".strftime("%c")) 
        call append(line(".")+4, " ************************************************************************/") 
        call append(line(".")+5, "")
    endif
    if &filetype == 'cpp'
        call append(line(".")+6, "#include<iostream>")
        call append(line(".")+7, "using namespace std;")
        call append(line(".")+8, "")
    endif
    if &filetype == 'c'
        call append(line(".")+6, "#include<stdio.h>")
        call append(line(".")+7, "")
    endif
    "Automatically navigate to the end of the file after creating a new file
    autocmd BufNewFile * normal G
endfunc 
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Keyboard commands
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

nmap <leader>w :w!<cr>
nmap <leader>f :find<cr>

" Map select all+copy ctrl+a
map <C-A> ggVGY
map! <C-A> <Esc>ggVGY
map <F12> gg=G
" When selected Ctrl+c copy
vmap <C-c> "+y
"Go to the empty line  
nnoremap <F2> :g/^\s*$/d<CR> 
"Comparison file  
nnoremap <C-F2> :vert diffsplit 
"new label  
map <M-F2> :tabnew<CR>  
"Lists the current directory file  
map <F3> :tabnew .<CR>  
"Open tree file directory  
map <C-F3> \be  
"C,C++ Press F5 Compile run
map <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
    exec "w"
    if &filetype == 'c'
        exec "!g++ % -o %<"
        exec "! ./%<"
    elseif &filetype == 'cpp'
        exec "!g++ % -o %<"
        exec "! ./%<"
    elseif &filetype == 'java' 
        exec "!javac %" 
        exec "!java %<"
    elseif &filetype == 'sh'
        :!./%
    endif
endfunc
"C,C++Debugging of
map <F8> :call Rungdb()<CR>
func! Rungdb()
    exec "w"
    exec "!g++ % -g -o %<"
    exec "!gdb ./%<"
endfunc
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""Utility settings
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Set to load automatically when the file is changed
set autoread
" quickfix pattern
autocmd FileType c,cpp map <buffer> <leader><space> :w<cr>:make<cr>
"Code completion  
set completeopt=preview,menu 
"Allow plug-ins  
filetype plugin on
"Share clipboard  
set clipboard+=unnamed 
"Never back up  
set nobackup
"make function
:set makeprg=g++\ -Wall\ \ %
"Auto save
set autowrite
set ruler                   " Open status bar ruler
set cursorline              " Highlight current line
set magic                   " Set magic
set guioptions-=T           " hide the toolbar
set guioptions-=m           " Hide menu bar
"set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)\
" Sets the information displayed on the status line
set foldcolumn=0
set foldmethod=indent 
set foldlevel=3 
set foldenable              " Start folding
" Do not use vi Keyboard mode, but vim own
set nocompatible
" Syntax highlighting 
set syntax=on
" Remove the prompt sound of input error
set noeb
" When processing unsaved or read-only files, a confirmation pops up
set confirm
" Auto indent
set autoindent
set cindent
" Tab Key width
set tabstop=4
" Uniform indent to 4
set softtabstop=4
set shiftwidth=4
" Do not replace tabs with spaces
set noexpandtab
" Use tabs at the beginning of lines and segments
set smarttab
" set number 
set number
" Number of history records
set history=1000
"Disable generation of temporary files
set nobackup
set noswapfile
"Search ignores case
set ignorecase
"Search highlighting character by character
set hlsearch
set incsearch
"Intra row substitution
set gdefault
"Coding settings
set enc=utf-8
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
"Language settings
set langmenu=zh_CN.UTF-8
set helplang=cn
" What is displayed on my status line (including file type and decoding)
"set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
"set statusline=[%F]%y%r%m%*%=[Line:%l/%L,Column:%c][%p%%]
" Always show status line
set laststatus=2
" The height of the command line (in the status line). The default is 1. Here is 2
set cmdheight=2
" filetype on 
filetype on
" Load file type plug-in
filetype plugin on
" Load relevant indented files for a specific file type
filetype indent on
" Save global variables
set viminfo+=!
" Words with the following symbols shall not be separated by newline
set iskeyword+=_,$,@,%,#,-
" Number of pixel lines inserted between characters
set linespace=0
" Command line autocompletion in enhanced mode
set wildmenu
" Return key( backspace)Normal processing indent, eol, start etc.
set backspace=2
" allow backspace And cursor keys across row boundaries
set whichwrap+=<,>,h,l
" Can be in buffer Use the mouse anywhere (similar) office (double click in the workspace to locate)
set mouse=a
set selection=exclusive
set selectmode=mouse,key
" through the use of: commands Command to tell us which line of the file has been changed
set report=0
" Display blank space between divided windows for easy reading
set fillchars=vert:\ ,stl:\ ,stlnc:\
" Highlight matching parentheses
set showmatch
" The time (in tenths of a second) that matches the highlight of parentheses
set matchtime=1
" Move cursor to buffer Maintain a distance of 3 lines at the top and bottom of the
set scrolloff=3
" by C The program provides automatic indentation
set smartindent
" Highlight normal txt File (required) txt.vim (script)
au BufRead,BufNewFile *  setfiletype txt
"Automatic completion
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { {<CR>}<ESC>O
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
:inoremap " ""<ESC>i
:inoremap ' ''<ESC>i
function! ClosePair(char)
    if getline('.')[col('.') - 1] == a:char
        return "\<Right>"
    else
        return a:char
    endif
endfunction
filetype plugin indent on 
"Open file type detection, Add this sentence to complete it with intelligence
set completeopt=longest,menu
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CTags Setting of  
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let Tlist_Sort_Type = "name"    " Sort by name  
let Tlist_Use_Right_Window = 1  " Show window on right  
let Tlist_Compart_Format = 1    " Compression mode  
let Tlist_Exist_OnlyWindow = 1  " If there's only one buffer,kill Window also kill fall buffer  
let Tlist_File_Fold_Auto_Close = 0  " Do not close other files tags  
let Tlist_Enable_Fold_Column = 0    " Do not display collapsed trees  
autocmd FileType java set tags+=D:\tools\java\tags  
"autocmd FileType h,cpp,cc,c set tags+=D:\tools\cpp\tags  
"let Tlist_Show_One_File=1            "Not displaying multiple files at the same time tag,Show only the current file
"set up tags  
set tags=tags  
"set autochdir 

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Other things
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Open by default Taglist 
let Tlist_Auto_Open=1 
"""""""""""""""""""""""""""""" 
" Tag list (ctags) 
"""""""""""""""""""""""""""""""" 
let Tlist_Ctags_Cmd = '/usr/bin/ctags' 
let Tlist_Show_One_File = 1 "Not displaying multiple files at the same time tag,Show only the current file 
let Tlist_Exit_OnlyWindow = 1 "If taglist If the window is the last window, exit vim 
let Tlist_Use_Right_Window = 1 "Show in right window taglist window
" minibufexpl General settings for plug-ins
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1

let g:miniBufExplModSelTarget = 1   

In order to facilitate management, the source code is hosted in github, and many new functions are added in the later stage,

For details, see: https://github.com/ma6174/vim

Reprinted from ma6174 - blog Park

Posted by Zippyaus on Mon, 08 Nov 2021 22:35:16 -0800