This repository has been archived by the owner on Jun 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
advanced.tex
425 lines (365 loc) · 13.4 KB
/
advanced.tex
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
\section{Advanced}
\subsection{Command line tricks}
\begin{center}
\begin{longtable}{l|l}
\verb!cat xx | gvim - -c "v/^\d\d\|^[3-9]/d"! & filter a stream\\
\verb!ls | gvim -! & edit a stream!\\
\verb!gvim ftp://www.somedomain.com/index.html! & uses netrw.vim\\
\verb!gvim -h! & help\\
\verb!gvim -o file1 file2! & open into a split\\
\verb!gvim -c "/main" joe.c & open joe.c !& jump to "main"\\
\verb!gvim -c "%s/ABC/DEF/ge | update" file1.c! & execute multiple command on a single file\\
\verb!gvim -c "argdo %s/ABC/DEF/ge | update" *.c! & execute multiple command on a group of files\\
\verb!gvim -c "argdo /begin/+1,/end/-1g/^/d | update" *.c! & remove blocks of text from a series of files\\
\verb!gvim -s "convert.vim" file.c! & automate editing of a file (ex commands in convert.vim)\\
\verb!gvim -u NONE -U NONE -N! & load vim without .vimrc and plugins (clean vim)\\
\verb!gvim -c 'normal ggdG"*p' c:/aaa/xp! & access paste buffer contents (put in a script/batch file)\\
\verb?gvim -c 's/^/\=@*/|hardcopy!|q!'? & print paste contents to default printer\\
\verb!gvim -d file1 file2! & vimdiff (compare differences)\\
\verb!dp! & "put" difference under cursor to other file\\
\verb!do! & "get" difference under cursor from other file\\
\verb!:grep somestring *.php! & internal grep creates a list of all matching files\\
\verb!:h grep! & use :cn(ext) :cp(rev) to navigate list
\end{longtable}
\end{center}
\subsection{External programs}
\begin{center}
\begin{longtable}{l|l}
\verb?:r!ls.exe? & reads in output of ls\\
\verb?!!date? & same thing (but replaces/filters current line)\\
\verb?:%!sort -u? & sort unique content\\
\verb?:'a,'b!sort -u? & as above\\
\verb?!1} sort -u? & sorts paragraph (note normal mode!!)\\
\verb?map <F9> :w:!c:/php/php.exe %? & run file through php\\
\verb?map <F2> :w:!perl -c %? & run file through perl\\
\verb?:runtime! syntax/2html.vim? & convert txt to html
\end{longtable}
\end{center}
\subsection{Recording}
\begin{center}
\begin{longtable}{l|l}
\verb!qq! & record to q\\
\verb!q! & end recording\\
\verb!@q! & to execute\\
\verb!@@! & to repeat\\
\verb!5@@! & to repeat 5 times\\
\verb!"qp! & display contents of register q (normal mode)\\
\verb!<ctrl-R>q! & display contents of register q (insert mode)\\
\verb!"qdd! & put changed contacts back into q\\
\verb!@q! & execute recording/register q\\
\verb!nnoremap ] @l:wbd! & combining a recording with a map (to end up in command mode)
\end{longtable}
\end{center}
\subsubsection{Operating a Recording on a Visual BLOCK}
define recording/register
\begin{verbatim}
qq:s/ to/ from/g^Mq
\end{verbatim}
define Visual BLOCK
\begin{verbatim}
V
\end{verbatim}
hit : and the following appears
\begin{verbatim}
:'<,'>
\end{verbatim}
complete as follows
\begin{verbatim}
:'<,'>norm @q
\end{verbatim}
\subsection{Quick jumping between splits}
\begin{verbatim}
map <C-J> <C-W>j<C-W>_
map <C-K> <C-W>k<C-W>_
\end{verbatim}
\subsection{Visual mode basics}
\begin{center}
\begin{longtable}{l|l}
\verb!v! & enter visual mode\\
\verb!V! & visual mode whole line\\
\verb!<C-V>! & enter VISUAL BLOCK mode\\
\verb!gv! & reselect last visual area (ultra)\\
\verb!o! & navigate visual area\\
\verb!"*y! & yank visual area into paste buffer\\
\verb!V%! & visualise what you match\\
\verb!V}J! & join visual block (great)\\
\verb!V}gJ! & join visual block w/o adding spaces\\
\verb!0<C-V>10j2ld! & delete first 2 characters of 10 successive lines
\end{longtable}
\end{center}
\subsection{vimrc essentials}
\begin{center}
\begin{longtable}{l|l}
\verb!set incsearch! & jumps to search word as you type\\
\verb!set wildignore=*.o,*.obj,*.bak,*.exe! & tab complete now ignores these\\
\verb!set shiftwidth=3! & for shift/tabbing\\
\verb!set vb t_vb=".! & set silent (no beep!)\\
\verb!set browsedir=buffer! & make 'open directory' use current directory\\
\end{longtable}
\end{center}
\subsubsection{Launching IE}
\begin{verbatim}
:nmap ,f :update<CR>:silent !start c:\progra~1\intern~1\iexplore.exe file://%:p<CR>
:nmap ,i :update<CR>: !start c:\progra~1\intern~1\iexplore.exe <cWORD><CR>
\end{verbatim}
\subsubsection{FTPing from vim}
\begin{verbatim}
cmap ,r :Nread ftp://209.51.134.122/public_html/index.html
cmap ,w :Nwrite ftp://209.51.134.122/public_html/index.html
gvim ftp://www.somedomain.com/index.html # uses netrw.vim
\end{verbatim}
\subsubsection{Autocmd}
\begin{center}
\begin{longtable}{l|l}
\verb?autocmd bufenter *.tex map <F1> :!latex %? & programming keys depending on file type\\
\verb?autocmd bufenter *.tex map <F2> :!xdvi -hush %<.dvi&? & launch xdvi with current file dvi\\
\verb?autocmd BufRead * silent! %s/[\r \t]\+\$//? & automatically delete whitespace, trailing dos returns\\
\verb!autocmd BufEnter *.php :%s/[ \t\r]\+\$//e! & same but only for php files
\end{longtable}
\end{center}
\subsection{Conventional shifting and indenting}
\begin{center}
\begin{longtable}{l|l}
\verb!:'a,'b>>! & conventional Shifting/Indenting\\
\verb!:vnoremap < <gv! & visual shifting (builtin-repeat)\\
\verb!:vnoremap > >gv! & visual shifting (builtin-repeat)\\
\verb!>i{! & block shifting (magic)\\
\verb!>a{!\\
\verb!>%!\\
\verb!<%!
\end{longtable}
\end{center}
\subsection{Pulling objects onto command/search line}
\begin{center}
\begin{longtable}{l|l}
\verb!<C-R><C-W>! & pull word under the cursor into a command line or search\\
\verb!<C-R><C-A>! & pull WORD under the cursor into a command line or search\\
\verb!<C-R>-! & pull small register (also insert mode)\\
\verb!<C-R>[0-9a-z]! & pull named registers (also insert mode)\\
\verb!<C-R>%! & pull file name (also \verb!#!) (also insert mode)\\
\verb!<C-R>=somevar! & pull contents of a variable (eg \verb!:let sray="ray[0-9]"!)
\end{longtable}
\end{center}
\subsection{Capturing output of current script}
\begin{center}
\begin{longtable}{l|l}
\verb?:new | r!perl #? & opens new buffer,read other buffer\\
\verb?:new! x.out | r!perl #? & same with named file\\
\verb?:new+read!ls?\\
\verb?:new +put q|%!sort? & create a new buffer, paste a register "q" into it, then sort new buffer
\end{longtable}
\end{center}
\subsection{Inserting DOS carriage returns}
\begin{center}
\begin{longtable}{l|l}
\verb!:%s/$\<C-V><C-M>&/g! & that's what you type\\
\verb!:%s/$\<C-Q><C-M>&/g! & for Win32\\
\verb!:%s/$/\^M&/g! & what you'll see where \verb!^M! is ONE character\\
\verb!:set list! & display "invisible characters"
\end{longtable}
\end{center}
\subsection{Perform an action on a particular file or file type}
\begin{verbatim}
autocmd VimEnter c:/intranet/note011.txt normal! ggVGg?
autocmd FileType *.pl exec('set fileformats=unix')
\end{verbatim}
Retrieving last command line command for copy \& pasting into text
\begin{verbatim}
i<c-r>:
\end{verbatim}
Retrieving last Search Command for copy \& pasting into text
\begin{verbatim}
i<c-r>/
\end{verbatim}
\subsection{Inserting line number}
\begin{verbatim}
:g/^/exec "s/^/".strpart(line(".")." ", 0, 4)
:%s/^/\=strpart(line(".")." ", 0, 5)
:%s/^/\=line('.'). ' '
\end{verbatim}
\subsection{Numbering lines}
\begin{center}
\begin{longtable}{l|l}
\verb!:set number! & show line numbers\\
\verb?:map <F12> :set number!<CR>? & map to toggle line numbers\\
\verb?:%s/^/\=strpart(line('.')." ",0,&ts)?\\
\verb?:'a,'b!perl -pne 'BEGIN{$a=223} substr($_,2,0)=$a++'? & number lines starting from arbitrary number\\
\verb!qqmnYP`n^Aq! & in recording q repeat with @q\\
\verb?:.,$g/^\d/exe "normal! \<c-a>"? & increment existing numbers to end of file\\
\end{longtable}
\end{center}
\subsection{Advanced incrementing}
\begin{verbatim}
let g:I=0
function! INC(increment)
let g:I =g:I + a:increment
return g:I
end function
\end{verbatim}
\subsubsection{Create list starting from 223 incrementing by 5 between markers a,b}
\begin{verbatim}
:let I=223
:'a,'bs/^/\=INC(5)/
\end{verbatim}
\subsubsection{Create a map for INC}
\begin{verbatim}
cab viminc :let I=223 \| 'a,'bs/$/\=INC(5)/
\end{verbatim}
\subsection{Digraphs (non alpha-numerics)}
\begin{center}
\begin{longtable}{l|l}
\verb!:digraphs! & display table\\
\verb!:h dig! & help\\
\verb!i<C-K>e'! & enters \'{e}\\
\verb!i<C-V>233! & enters \'{e} (Unix)\\
\verb!i<C-Q>233! & enters \'{e} (Win32)\\
\verb!ga! & View hex value of any character\\
\verb!:%s/\[<C-V>128-<C-V>255\]//gi ! & where you have to type the Control-V\\
\verb!:%s/\[€-ÿ\]//gi! & Should see a black square \& a dotted y\\
\verb!:%s/\[<C-V>128-<C-V>255<C-V>01-<C-V>31\]//gi! & All pesky non-asciis\\
\verb!%:exec "norm /\[\\x00-\\x1f\\x80-\\xff\]/"! & same thing\\
\verb!yl/<C-R>"! & Pull a non-ascii character onto search bar
\end{longtable}
\end{center}
\subsection{Complex vim}
\begin{center}
\begin{longtable}{l|l}
\verb!:%s/\<\(on\|off\)\>/\=strpart("offon", 3 * ("off" == submatch(0)), 3)/g! & swap two words\\
\verb!:vnoremap <C-X> <Esc>`."gvP"P! & swap two words
\end{longtable}
\end{center}
\subsection{Syntax highlighting}
\begin{center}
\begin{longtable}{l|l}
\verb!:set syntax=perl! & force Syntax coloring for a file that has no extension .pl\\
\verb!:set syntax off! & remove syntax coloring (useful for all sorts of reasons)\\
\verb!:colorscheme blue! & change coloring scheme (any file in $\sim$vim/vim??/colors)\\
\verb!vim:ft=html:! & force HTML Syntax highlighting by using a modeline\\
\verb!:syn match DoubleSpace " "! & example of setting your own highlighting\\
\verb!:hi def DoubleSpace guibg=#e0e0e0! & sets the editor background
\end{longtable}
\end{center}
\subsection{Preventions and security}
\begin{center}
\begin{longtable}{l|l}
\verb!:set noma! & prevents modifications (non modifiable)\\
\verb!:set ro! & protect a file from unintentional writes (Read Only)\\
\verb!:X! & encryption (do not forget your key!)
\end{longtable}
\end{center}
\subsection[Taglist]
{Taglist \footnote{Script required: taglist.vim (\href{http://www.vim.org/scripts/script.php?script\_id=273}{http://www.vim.org/scripts/script.php?script\_id=273})}}
\begin{center}
\begin{longtable}{l|l}
\verb!:Tlist! & display tags (list of functions)\\
\verb!<C-]> ! & jump to function under cursor
\end{longtable}
\end{center}
\subsection{Folding}
\begin{center}
\begin{longtable}{l|l}
\verb!zf}! & fold paragraph using motion\\
\verb!v}zf! & fold paragraph using visual\\
\verb!zf'a! & fold to mark\\
\verb!zo! & open fold\\
\verb!zc! & re-close fold
\end{longtable}
\end{center}
\subsection{Renaming files}
Rename files without leaving vim
\begin{verbatim}
:r! ls *.c
:%s/\(.*\).c/mv & \1.bla
:w !sh
:q!
\end{verbatim}
\subsection{Reproducing previous line word by word}
\begin{verbatim}
imap ] @@@<esc>hhkyWjl?@@@<cr>P/@@@<cr>3s
nmap ] i@@@<esc>hhkyWjl?@@@<cr>P/@@@<cr>3s
\end{verbatim}
\subsection[Reading MS-Word documents]
{Reading MS-Word documents \footnote{Program required: Antiword (http://www.winfield.demon.nl)}}
\begin{verbatim}
:autocmd BufReadPre *.doc set ro
:autocmd BufReadPre *.doc set hlsearch!
:autocmd BufReadPost *.doc %!antiword "%"
\end{verbatim}
\subsection{Random functions}
\subsubsection{Save word under cursor to a file}
\begin{verbatim}
function! SaveWord()
normal yiw
exe ':!echo '.@0.' >> word.txt'
endfunction
\end{verbatim}
\subsubsection{Delete duplicate lines}
\begin{verbatim}
function! Del()
if getline(".") == getline(line(".") - 1)
norm dd
endif
endfunction
\end{verbatim}
\subsubsection{Columnise a CSV file for display}
\begin{verbatim}
:let width = 20
:let fill=' ' | while strlen(fill) < width | let fill=fill.fill | endwhile
:%s/\([^;]*\);\=/\=strpart(submatch(1).fill, 0, width)/ge
:%s/\s\+$//ge
\end{verbatim}
\subsubsection{Highlight a particular csv column}
\begin{verbatim}
function! CSVH(x)
execute 'match Keyword /^\([^,]*,\){'.a:x.'}\zs[^,]*/'
execute 'normal ^'.a:x.'f,'
endfunction
command! -nargs=1 Csv :call CSVH(<args>)
\end{verbatim}
Call with \verb!:Csv 5! to highlight fifth column
\subsection{Miscallaenous commands}
\begin{center}
\begin{longtable}{l|l}
\verb!:scriptnames! & list all plugins, .vimrcs loaded (super)\\
\verb!:verbose set history?! & reveals value of history and where set\\
\verb!:function! & list functions\\
\verb!:func SearchCompl! & List particular function
\end{longtable}
\end{center}
\subsection{Vim traps}
\begin{list}{}
\item In regular expressions you must backslash + (match 1 or more)
\item In regular expressions you must backslash | (or)
\item In regular expressions you must backslash ( (group)
\item In regular expressions you must backslash \{ (count)
\end{list}
\begin{center}
\begin{longtable}{l|l}
\verb!/fred\+/! & matches fred/freddy but not free\\
\verb!/\(fred\)\{2,3}/! & note what you have to break\\
\verb!/codes\(\n\|\s\)*where! & normal regexp\\
\verb!/\vcodes(\n|\s)*where! & very magic
\end{longtable}
\end{center}
\subsection{Help}
\begin{center}
\begin{longtable}{l|l}
\verb!:h quickref! & vim quick reference sheet (ultra)\\
\verb!:h tips! & vim's own tips help\\
\verb!:h visual<C-D><TAB>! & obtain list of all visual help topics\\
\verb!:h ctrl<C-D>! & list help of all control keys\\
\verb!:helpg uganda! & grep help files use :cn, :cp to find next\\
\verb!:h :r! & help for :ex command\\
\verb!:h CTRL-R! & normal mode\\
\verb!:h /\r! & what's \verb!\r! in a regexp (matches a <CR>)\\
\verb!:h \\zs! & double up backslash to find \verb!\zs! in help\\
\verb!:h i_CTRL-R! & help for say <C-R> in insert mode\\
\verb!:h c_CTRL-R! & help for say <C-R> in command mode\\
\verb!:h v_CTRL-V! & visual mode\\
\verb!:h tutor! & vim tutor\\
\verb!<C-[>, <C-T>! & Move back \& forth in help history\\
\verb!gvim -h! & vim command line help\\
\verb!:helptags /vim/vim64/doc! & rebuild all *.txt help files in /doc\\
\verb!:help add-local-help! &
\end{longtable}
\end{center}