Vim笔记

Vim 的使用

vim 基本操作

移动光标

下面操作均需处在一般模式(默认的模式)下:

  • h j k l 分别为“左”“下”“上”“右”
  • 翻半页:Ctrl + d(d for down),Ctrl + u(u for up)。
  • 翻一页:Ctrl + f(f for front),Ctrl + b(b for back)。
  • gg表示移到到首行。
  • G表示移动到尾行。
  • nG(n指的是数字)表示移动到第 n 行;一般用于根据程序错误提示信息进行 bug fix。
  • 0表示移到光标所在行的行首; $表示移动到光标所在行的行尾。

复制剪切粘贴

  • yy复制光标所在行。
  • dd剪切光标所在行,如果光剪切不粘贴,那就相当于删除。
  • p将复制/剪切的内容粘贴至光标后,因为光标是在具体字符的位置上,所以实际是在该字符的后面;整行的复制粘贴在游标的下一行。

v 模式相关

  • v切换到“高亮选择模式”,移动光标进行选择。
  • v模式下,按y(y for yank)复制高亮选择的内容。
  • v模式下,按d剪切高亮选择的内容,如果光剪切不粘贴,那就相当于删除。

撤销、重做

以下仅讨论 vim 下的操作(vi 的操作稍有不同):

  • u进行撤销,可多次撤销。
  • Ctrl + r(r for redo)进行重做,可多次重做。

进入编辑模式

下面所有操作均需在一般模式下执行:

  • i,在当前光标所在字符前插入。
  • o,在当前光标所在行的下一行插入新的一行。
  • O,在当前光标所在行的上一行插入新的一行。

查找和替换字符串

下面所有操作均需在一般模式下执行:

  • /word,向下查找一个字符串 word,查找后按n看下一匹配结果,按N看上一匹配结果。
  • ?word,向上查找一个字符串 word,查找后按n看下一匹配结果,按N看上一匹配结果。
  • :s/word1/word2/g,查找 word1 并替换为 word2,/g表示全文替换,不加则只替换每行的第一个匹配项。
  • :n1,n2s/word1/word2/g,在 n1 和 n2 行之间查找 word1 并替换为 word2,其中 n1、n2 皆可取数字,另外 n2 可取$表示最后一行。

行号相关

  • :set nu表示显示行号。
  • :set nonu表示不显示行号。

保存及退出

  • :w,保存文本。
  • :q,退出 vim。
  • :w!,强制保存,在 root 用户下,即使文本只读也可以完成保存。
  • :q!,强制退出,所有改动不生效。
  • :wq,保存并退出。

vim 插件

下载 Vundle

1
$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

配置插件

1
2
3
4
5
6
7
8
9
10
11
12
13
$ vim ~/.vimrc
set nocompatible " be iMproved, required
filetype off " required

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'tpope/vim-fugitive'
Plugin 'git://git.wincent.com/command-t.git'
Plugin 'file:///home/gmarik/path/to/plugin'
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
call vundle#end() " required
filetype plugin indent on " required

安装插件

1
2
$ vim
:PluginInstall

或者

1
$ vim +PluginInstall +qall

然后会出现如下图一样的界面

然后慢慢等!慢慢等!慢慢等!这里敲黑板,画圈圈记起来,笔者当年就是进入这个界面一脸懵逼发现不会动就Ctrl+C了,然后就以为自己配置失败了 o_O。
仔细看,最左边正在安装的有个>符号,新增的插件有个+符号,左下角的英文也是已经相当明白了!

一键安装?

没错,我知道你懒,所以这里提供了一键安装,移步这里一键安装

这里贴出我的配置

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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
set nocompatible              " be iMproved, required
filetype off " 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'
" NERD-tree
Plugin 'scrooloose/nerdtree'
Plugin 'majutsushi/tagbar'
Plugin 'fholgado/minibufexpl.vim'
Plugin 'godlygeek/tabular'
Plugin 'plasticboy/vim-markdown'
Plugin 'jeroenbourgois/vim-actionscript'
Plugin 'cespare/mxml.vim'
Plugin 'vim-scripts/xmledit'
Plugin 'msanders/cocoa.vim'
Plugin 'Yggdroot/indentLine'
Plugin 'tfnico/vim-gradle'
Plugin 'adragomir/javacomplete'
Plugin 'scrooloose/syntastic'
Plugin 'flazz/vim-colorschemes'
Plugin 'molok/vim-smartusline'
Plugin 'bpdp/vim-java'
Plugin 'mattn/emmet-vim'
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line

"=============================================================================
" General settings
"=============================================================================

highlight OverLength ctermbg=blue ctermfg=white
set colorcolumn=120

set nocp

" Tab related
set ts=4
set sw=4
set smarttab
set et
set ambiwidth=double

" Format related
set tw=0
set lbr
set fo+=mB

" Indent related
set cin
set ai
set cino=:0g0t0(susj1

" Editing related
set backspace=indent,eol,start
set whichwrap=b,s,<,>,[,]
set mouse=
set selectmode=
set mousemodel=popup
set keymodel=
set selection=inclusive

" Misc
set wildmenu
set nospell

" Encoding related
set encoding=utf-8
"set langmenu=zh_CN.UTF-8
"language message zh_CN.UTF-8
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1

" File type related
filetype plugin indent on
au BufRead *.as set filetype=actionscript et
au BufRead *.mxml set filetype=mxml et
au BufRead *.mml set filetype=mathml et

" Display related
set ru
set nu
set sm
set hls
set nowrap
syntax on

" Refer to: https://github.com/bpdp/vim-java
set ofu=syntaxcomplete#Complete
"if (has("gui_running"))
" colo bluegreen
"else
" colo bluegreen
"endif

" Default Colors for CursorLine
highlight CursorLine ctermbg=Blue ctermfg=Yellow

" Change Color when entering Insert Mode
autocmd InsertEnter * highlight CursorLine ctermbg=Darkgray ctermfg=Red

"Revert Color to default when leaving Insert Mode
autocmd InsertLeave * highlight CursorLine ctermbg=Blue ctermfg=Yellow

" autocmd vimenter * NERDTree
autocmd vimenter * if !argc() | NERDTree | endif
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif

let g:NERDTreeDirArrows=0
let g:NERDTreeWinSize=30

au BufRead,BufNewFile *.gradle set filetype=groovy

" set foldmethod=indent
" set foldnestmax=2

let g:indentLine_leadingSpaceEnabled=1
let g:indentLine_leadingSpaceChar='.'
let g:indentLine_color_term=7
let g:indentLine_color_gui='#FFB3B3'
" let g:indentLine_char = '|'

hi Modified guifg=black guibg=#FFA500
set statusline=%2.2n\ %t\ %h%#Modified#%m%r%*%=%l%L\ %2c\ %P
let g:smartusline_string_to_highlight='%2.2n %t %h'

au BufNewFile,BufRead *.groovy setf groovy
au BufNewFile,BufRead *.gradle setf groovy

if (has("autocmd"))
autocmd Filetype java setlocal omnifunc=javacomplete#Complete
endif

" ============================================================================
" Shortcuts
" ============================================================================
" Escape arrow in putty
map <Esc>[A <C-Up>
map <Esc>[B <C-Down>
map <Esc>[C <C-Right>
map <Esc>[D <C-Left>
map! <Esc>[A <C-Up>
map! <Esc>[B <C-Down>
map! <Esc>[D <C-Left>
map! <Esc>[C <C-Right>
" Move lines
nmap <C-Down> :move .+1<CR>
nmap <C-Up> :move .-2<CR>

" Toggle line number
nmap <F12> :set nu!<CR>
imap <F12> :set nu!<CR>

" Toggle NERDTree window
nmap <silent> <F9> :NERDTreeToggle<CR>
nmap <silent> <C-O> :NERDTreeToggle<CR><ESC>

" miniBufExploer config
let g:miniBufExplMapWindowNavVim=1
let g:miniBufExplMapWindowNavArrows=1
let g:miniBufExplMapCTabSwitchBufs=1
let g:miniBufExplModSelTarget=1

" buffer change
nmap <S-Tab> :bn<CR>
nmap <S-Left> :bp<CR>
nmap <S-Right> :bn<CR>

" Toggle Tagbar
nmap <C-t> :TagbarToggle<CR>

"=============================================================================
" Modes
"=============================================================================

function EnglishMode()
"set imactivatekey=
"set noimcmdline
"set iminsert=0
"set imsearch=0
setlocal nospell
endfunction
command -nargs=0 EnglishMode call EnglishMode()

function ChineseMode()
"set imactivatekey=C-space
"set noimcmdline
"set iminsert=2
"set imsearch=2
setlocal nospell
endfunction
command -nargs=0 ChineseMode call ChineseMode()

function TextMode()
setlocal nocin
setlocal nosm
setlocal noai
setlocal tw=0
endfunction
command -nargs=0 TextMode call TextMode()

function CodeMode()
setlocal cin
setlocal sm
setlocal ai
setlocal tw=0
endfunction
command -nargs=0 CodeMode call CodeMode()

function MailMode()
call TextMode()
setlocal ft=mail
setlocal tw=0
endfunction
command -nargs=0 MailMode call MailMode()

function BBSMode()
call MailMode()
call ChineseMode()
setlocal ft=bbs
setlocal fenc=cp936
setlocal tw=0
endfunction
command -nargs=0 BBSMode call BBSMode()

function VikiMode()
call ChineseMode()
setlocal ft=viki
endfunction
command -nargs=0 VikiMode call VikiMode()

"=============================================================================
" Functions
"=============================================================================

function TimeStamp()
let curposn= SaveWinPosn()
%s/\$Date: .*\$/\=strftime("$Date: %Y-%m-%d %H:%M:%S$")/ge
%s/Last Change: .*$/\=strftime("Last Change: %Y-%m-%d %H:%M:%S")/ge
%s/Last Modified: .*$/\=strftime("Last Modified: %Y-%m-%d %H:%M:%S")/ge
" Replace once and never update.
%s/Created: *$/\=strftime("Created: %Y-%m-%d %H:%M:%S")/ge
call RestoreWinPosn(curposn)
endfunction
command -nargs=0 TimeStamp call TimeStamp()

function AutoTimeStamp()
augr tagdate
au!
au BufWritePre,FileWritePre * call TimeStamp()
augr END
endfunction
command -nargs=0 AutoTimeStamp call AutoTimeStamp()

function NoAutoTimeStamp()
augr tagdate
au!
augr END
endfunction
command -nargs=0 NoAutoTimeStamp call NoAutoTimeStamp()

command -nargs=1 Margin exec "match Error /.\\%>" . (<args> + 1) . "v/"
command -nargs=0 NoMargin match none

function CodeLayout()
call CodeMode()
WManager
Tlist
endfunction
command -nargs=0 CodeLayout call CodeLayout()

function CodeLayoutSmall()
call CodeLayout()
set columns=141
exec "norm \<c-w>l"
set nu
set lines=100
endfunction
command -nargs=0 CodeLayoutSmall call CodeLayoutSmall()

function CodeLayoutLarge()
call CodeLayout()
set columns=226
exec "norm \<c-w>l"
set nu
vsplit
set lines=100
endfunction
command -nargs=0 CodeLayoutLarge call CodeLayoutLarge()

" ============================================================================
" Plugins settings
" ============================================================================

" Tlist
let Tlist_Use_Right_Window=1
let Tlist_File_Fold_Auto_Close=1

" A
let g:alternateNoDefaultAlternate=1
let g:alternateRelativeFiles=1

" Viki
let g:vikiNameSuffix=".viki"
autocmd! BufRead,BufNewFile *.viki call VikiMode()

" BBS
autocmd! BufRead,BufNewFile *.bbs call BBSMode()

" Mail
autocmd! FileType mail call MailMode()

" XML
autocmd! FileType html,xml,yaml setlocal sw=2 ts=2

" FencView
let g:fencview_autodetect=0

"=============================================================================
" Platform dependent settings
"=============================================================================

if (has("win32"))

"-------------------------------------------------------------------------
" Win32
"-------------------------------------------------------------------------

if (has("gui_running"))
set guifont=Bitstream_Vera_Sans_Mono:h9:cANSI
set guifontwide=NSimSun:h9:cGB2312
endif

" For Viki
let g:netrw_browsex_viewer="start"

" For tee
set shellpipe=2>&1\|\ tee

" VimTweak
if (has("gui_running"))
command -nargs=1 SetAlpha call libcallnr("vimtweak.dll",
\"SetAlpha", <args>)
command -nargs=0 TopMost call libcallnr("vimtweak.dll",
\"EnableTopMost", 1)
command -nargs=0 NoTopMost call libcallnr("vimtweak.dll",
\"EnableTopMost", 0)
endif

else

"-------------------------------------------------------------------------
" Linux
"-------------------------------------------------------------------------

if (has("gui_running"))
set guifont=Bitstream\ Vera\ Sans\ Mono\ 9
endif

" For Viki
let g:vikiHomePage="~/document/Viki/index.viki"
let g:netrw_browsex_viewer="kfmclient exec"

set makeprg=build

endif
文章不错,你都不请我喝杯茶,就是说你呀!
0%
upyun