0%

【Vim】Vim常用配置<二>

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

  • 一般性设置

    • 设置显示行号

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

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

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      " 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;
    • 宽度设置

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      "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
  • 键位设置

    • 组合命令前缀设置

      1
      let mapleader = ","

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

    • 行间跳转

      1
      2
      3
      4
      5
      6
      7
      8
      9
      "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

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

    • 自定义快捷键

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

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

    • 编辑.vimrc快捷键设置

      1
      2
      3
      4
      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文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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>
坚持原创技术分享,您的支持将鼓励我继续创作!