欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Vim】Vim常用配置<二>

程序员文章站 2022-03-10 23:27:08
...

在前面的文章《使用Vim撰写科研论文》和《Vim常用配置<一>》中,我们介绍了一些较为实用的vim插件的安装和使用。这篇文章介绍一下,最基础普适的设置,主要分为:一般性设置,键位设置。

  • 一般性设置

    • 设置显示行号

      " set line number
      set number
      set numberwidth=4
      set relativenumber
      
    • 设置光标

      "Keep the cursor above the bottom by 7 lines
      set scrolloff=7
      set cursorcolumn
      set cursorline
      
    • 光标颜色设置

      " Visual Mode Orange Background, Black Text
      hi Visual          guifg=DarkGreen guibg=#DDDDDD
      
      " Default Colors for CursorLine
      highlight CursorLine guibg=#3E3D32
      highlight Cursor guibg=#A6E22E;
      
      " Change Color when entering Insert Mode
      autocmd InsertEnter * highlight  CursorLine guibg=#323D3E
      autocmd InsertEnter * highlight  Cursor guibg=#00AAFF;
      
      " Revert Color to default when leaving Insert Mode
      autocmd InsertLeave * highlight  CursorLine guibg=#3E3D32
      autocmd InsertLeave * highlight  Cursor guibg=#A6E22E;
      
    • 宽度设置

      "Set the height and weight for vim form
      set lines=35 columns=118
      
      " the width of tab is 4 space
      set tabstop=4
      " the width of autoindent is 4 space
      set sw=4
      set noexpandtab
      set autoindent
      " set smartindent
      " Highlight the search results, use :noh to cancel
      set hlsearch
      
  • 键位设置

    • 组合命令前缀设置

      let mapleader = ","
      

      这个键基本是用的很频繁的,所以我们设置在键盘上方便触及的地方。

    • 行间跳转

      "To move in one long line
      nnoremap k gk
      nnoremap gk k
      nnoremap j gj
      nnoremap gj j
      vnoremap k gk
      vnoremap gk k
      vnoremap j gj
      vnoremap gj j
      

      上述命令就是将行间移动和行内移动进行对调。

    • 自定义快捷键

      inoremap jk <esc>
      "To move to the beginning or end
      nnoremap H ^
      nnoremap L $
      

      原来的键位esc, ^, $ 手指都需要移动较远距离来点击。之所以选择jk来返回到命令模式,主要原因是,一方面在编辑模式下,以jk开头的英文单词很少;另一方面,在命令模式下,j,k 只是上下移动的命令,并不会修改文本内容。

    • 编辑.vimrc快捷键设置

      nnoremap <leader>ev :vsplit $MYVIMRC<cr>
      "-------Choose one for execute the vimrc file when it's saved
      nnoremap <leader>sv :source $MYVIMRC<cr>
      autocmd BufWritePost $MYVIMRC source $MYVIMRC
      

      这样我们可以通过命令<leader>ev打开配置文件,通过命令<leader>sv来使之生效。当然,我们可以直接保存该文件使之生效。


至此,我们完成了.vimrc配置文件的全部讲解,下面给出完整的.vimrc文件:

set nocompatible              " be iMproved, required
filetype on                  " 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 'lervag/vimtex'
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
Bundle 'kien/ctrlp.vim'
Plugin 'altercation/vim-colors-solarized'
 "A Vim Plugin for Lively Previewing LaTeX PDF Output
Plugin 'xuhdev/vim-latex-live-preview'
Plugin 'rking/ag.vim'
Plugin 'scrooloose/nerdtree'
Plugin 'ryanoasis/vim-devicons'
Plugin 'tiagofumo/vim-nerdtree-syntax-highlight'
Plugin 'sirver/ultisnips'
Plugin 'honza/vim-snippets'
Plugin 'tpope/vim-surround'
Plugin 'skywind3000/asyncrun.vim' "for running C
Plugin 'scrooloose/nerdcommenter'
Plugin 'Yggdroot/indentLine'
Plugin 'majutsushi/tagbar'
Plugin 'KeitaNakamura/tex-conceal.vim'
Plugin 'vim-scripts/indentpython.vim'
call vundle#end()            " required
filetype plugin indent on    " required

"----------------General Configurations----------------
set encoding=UTF-8
"To show icons
set guifont=DroidSansMono_Nerd_Font:h16

" set line number
set number
set numberwidth=4
set relativenumber

" set the theme
set t_Co=256
set background=dark
colorscheme solarized

let g:ag_prg="</usr/local/bin/> --vimgrep"

"Set the height and weight for vim form
set lines=35 columns=118

" the width of tab is 4 space
set tabstop=4
" the width of autoindent is 4 space
set sw=4
set noexpandtab
set autoindent
" set smartindent
" Highlight the search results, use :noh to cancel
set hlsearch

syntax enable
syntax on

"Set the transparency of the vim
set transparency=2  


"Keep the cursor above the bottom by 7 lines
set scrolloff=7
set cursorcolumn
set cursorline

" " Visual Mode Orange Background, Black Text
" hi Visual          guifg=DarkGreen guibg=#DDDDDD

" " Default Colors for CursorLine
" highlight CursorLine guibg=#3E3D32
" highlight Cursor guibg=#A6E22E;

" " Change Color when entering Insert Mode
" autocmd InsertEnter * highlight  CursorLine guibg=#323D3E
" autocmd InsertEnter * highlight  Cursor guibg=#00AAFF;

" " Revert Color to default when leaving Insert Mode
" autocmd InsertLeave * highlight  CursorLine guibg=#3E3D32
" autocmd InsertLeave * highlight  Cursor guibg=#A6E22E;

set spelllang=en_us
set spell

"-----------------Key Mapping------------------
let mapleader = ","


"To move in one long line
nnoremap k gk
nnoremap gk k
nnoremap j gj
nnoremap gj j
vnoremap k gk
vnoremap gk k
vnoremap j gj
vnoremap gj j

inoremap jk <esc>
"To move to the beginning or end
nnoremap H ^
nnoremap L $



nnoremap <leader>ev :vsplit $MYVIMRC<cr>
"-------Choose one for execute the vimrc file when it's saved
nnoremap <leader>sv :source $MYVIMRC<cr>
autocmd BufWritePost $MYVIMRC source $MYVIMRC


"Set the airline's theme
let g:airline_theme="solarized"

"----------------Configuration for LaTeX---------------"
"Type help vimtex for detailed information
let g:tex_flavor='latex'
let g:vimtex_view_method='skim' "Use Skim as the pdf viewer
let g:vimtex_view_automatic=1
let g:vimtex_quickfix_mode=2
let g:vimtex_quickfix_autoclose_after_keystrokes=1
set conceallevel=2
let g:tex_conceal='abdmg'
let g:vimtex_quickfix_autojump=1
let g:vimtex_compiler_latexmk = {
    \ 'options' : [
    \   '-xelatex',
    \   '-verbose',
    \   '-file-line-error',
    \   '-synctex=1',
    \   '-interaction=nonstopmode',
    \ ],
    \}

let g:vimtex_complete_ignore_case=1
let g:vimtex_complete_close_braces=1

"In insert mode show the details, show the math equations othewise
autocmd FileType tex inoremap jk <esc>:set conceallevel=0<cr>
autocmd FileType tex nnoremap i :set conceallevel=0<cr>i
autocmd FileType tex nnoremap s :set conceallevel=0<cr>s
autocmd FileType tex nnoremap a :set conceallevel=0<cr>a
autocmd Filetype tex setlocal nofoldenable

"-------set the shortcuts for compiling, viewing, and cleaning .tex files
autocmd FileType tex nmap <leader>b  \ll
autocmd FileType tex nmap <leader>v \lv
autocmd FileType tex  nmap <leader>c \lc
autocmd FileType tex nmap <leader>t \lt

"Trigger the autocompletion for \cite and \ref in latex
imap <leader><tab> <C-X><C-O>


"----------Settings for xuhdev/vim-latex-live-preview
autocmd Filetype tex setl updatetime=15 "To set the update time for live view
let g:livepreview_previewer='open -a skim'


"-------Shortcuts for the plugin tpope/vim-surround
nmap <leader>) ysiw)
nmap <leader>} ysiw}
nmap <leader>" ysiw"

"-------Shortcuts for the plugin ctrlp
let g:ctrlp_map = '<c-p>'
let g:ctrlp_cmd = 'CtrlP'
let g:ctrlp_by_filename=1
"set the types of files that can be ignored
let g:ctrlp_custom_ignore = {
    \ 'dir':  '\v[\/]\.(git|hg|svn|rvm)$',
    \ 'file': '\v\.(exe|so|dll|zip|tar|tar.gz|pyc)$',
    \ }

"-------Settings for NERDTree
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
autocmd vimenter * NERDTree
"Close vim when the NERDTree is the only window
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

"To update the NERDTree when change the Tab by gt
nnoremap gt gt:NERDTreeFind<CR><C-w>l 
" Open or close the NERDTree 
nmap <C-e> :NERDTreeToggle<CR>
let NERDTreeIgnore=['\.pyc','\~$','\.swp']

let g:NERDTreeShowLineNumbers=1

" NERDTress File highlighting
"Set the vim-devicons
"vim-nerdtree-syntax-highlight settings
""""""""""""""""""""""""""""""
let g:WebDevIconsDisableDefaultFolderSymbolColorFromNERDTreeDir = 1
let g:WebDevIconsDisableDefaultFileSymbolColorFromNERDTreeFile = 1
" not to show brackets around flags
let g:webdevicons_conceal_nerdtree_brackets = 1
" enable folder/directory glyph flag (disabled by default with 0)
let g:WebDevIconsUnicodeDecorateFolderNodes = 1
" enable open and close folder/directory glyph flags (disabled by default with 0)
let g:DevIconsEnableFoldersOpenClose = 1
" use double-width(1) or single-width(0) glyphs
" only manipulates padding, has no effect on terminal or set(guifont) font
let g:WebDevIconsUnicodeGlyphDoubleWidth = 0
" Force extra padding in NERDTree so that the filetype icons line up vertically
let g:WebDevIconsNerdTreeGitPluginForceVAlign = 1

augroup custom_nerdtree_options
    autocmd!
    autocmd FileType,WinEnter * :call <SID>SetNerdTreeOptions()
augroup END

function! s:SetNerdTreeOptions() abort
    if &l:filetype ==# 'nerdtree'
        let g:default_opts = {
                    \   'ambiwidth': &ambiwidth,
                    \   'listchars': &listchars,
                    \   'list'     : &l:list,
                    \ }
        setlocal ambiwidth=double listchars=space:. nolist
    else
        if exists('g:default_opts')
            let [&ambiwidth, &listchars, &l:list] = [
                        \   g:default_opts.ambiwidth,
                        \   g:default_opts.listchars,
                        \   g:default_opts.list
                        \ ]
            unlet g:default_opts
        endif
    endif
endfunction

"Highlight full name (not only icons). You need to add this if you don't have vim-devicons and want highlight.
let g:NERDTreeFileExtensionHighlightFullName = 1
let g:NERDTreeExactMatchHighlightFullName = 1
let g:NERDTreePatternMatchHighlightFullName = 1

"Highlight full name (not only icons). You need to add this if you don't have vim-devicons and want highlight.
let g:NERDTreeHighlightFolders = 1

"highlights the folder name
let g:NERDTreeHighlightFoldersFullName = 1

"you can add these colors to your .vimrc to help customizing
let s:brown = "905532"
let s:aqua =  "3AFFDB"
let s:blue = "689FB6"
let s:darkBlue = "44788E"
let s:purple = "834F79"
let s:lightPurple = "834F79"
let s:red = "AE403F"
let s:beige = "F5C06F"
let s:yellow = "F09F17"
let s:orange = "D4843E"
let s:darkOrange = "F16529"
let s:pink = "CB6F6F"
let s:salmon = "EE6E73"
let s:green = "8FAA54"
let s:Turquoise = "40E0D0"
let s:lightGreen = "31B53E"
let s:white = "FFFFFF"
let s:rspec_red = "FE405F"
let s:git_orange = "F54D27"
let s:gray = "808A87"

let g:NERDTreeExtensionHighlightColor = {} " this line is needed to avoid error
let g:NERDTreeExtensionHighlightColor['py'] = s:orange " sets the color of py files to blue
let g:NERDTreeExtensionHighlightColor['tex'] = s:yellow " sets the color of tex files to blue
let g:NERDTreeExtensionHighlightColor['c'] = s:green " sets the color of c files to blue
let g:NERDTreeExtensionHighlightColor['pdf'] = s:beige " sets the color of pdf files to blue
let g:NERDTreeExtensionHighlightColor['c++'] = s:green " sets the color of c++ files to blue

"---------------settings for the nerdcommenter--------------------------------"
" Add a space before comments
let g:NERDSpaceDelims=1


"---------------configurations for the plugin ultisnips--------------------"
let g:UltiSnipsExpandTrigger = '<tab>'
let g:UltiSnipsJumpForwardTrigger = '<tab>'
let g:UltiSnipsJumpBackwardTrigger = '<s-tab>'

"---------------settings for the plugin asyncrun---------------------------"
let g:asyncrun_open =6
let g:asyncrun_bell =1
let g:asyncrun_rootmarks = ['.svn', '.git', '.root', '_darcs', 'build.xml'] 

"call for the quickfix window
autocmd FileType c,cpp nnoremap <leader>c :call asyncrun#quickfix_toggle(6)<cr>
"Shortcuts for compiling and run the c file
autocmd FileType c,cpp nnoremap <silent> <leader>b :AsyncRun -cwd=<root>  make <cr>
autocmd FileType c,cpp nnoremap <silent> <leader>r :AsyncRun  -cwd=<root> -raw make run <cr>

"---------------Configurations for the plugin ctags-----------------------"
let g:tagbar_ctags_bin='/usr/local/bin/ctags'
let g:tagbar_width=30
let g:tagbar_right=1
"AutoOpen tagbar for c/c++ files
autocmd BufReadPost *.cpp,*.c,*.h,*.hpp,*.cc call tagbar#autoopen()
"Open or close tagbar
map <leader>tb :TagbarToggle<CR>



"---------------Settings for the plugin indentline-----------------------"
let g:indentLine_char='|'
let g:indentLine_enabled=1

"---------------Configurations for Python-------------------------------"
set encoding=utf-8
au BufNewFile,BufRead *.py set tabstop=4 |set softtabstop=4|set shiftwidth=4|set textwidth=79|set expandtab|set autoindent|set fileformat=unix
set clipboard=unnamed

let python_highlight_all=1

function CheckPythonSyntax() 
    let mp = &makeprg 
    let ef = &errorformat 
    let exeFile = expand("%:t") 
    setlocal makeprg=python3\ -u  
    set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m 
    silent make %
    copen 
   let &makeprg     = mp  
    let &errorformat = ef  
endfunction
au filetype python map <leader>b  :w <cr> :call CheckPythonSyntax() <cr> :cw<cr>
	
相关标签: Vim