-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvimtips
1556 lines (1556 loc) · 50.3 KB
/
vimtips
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
zz to center the cursor vertically on your screen. useful when you 250gzz, for instance.
%
git config --global core.editor "gvim --nofork"
%
ci" inside a " " will erase everything between "" and place you in insertion mode.
%
:set guifont=* in gvim or MacVim to get a font selection dialog. Useful while giving presentations.
%
:h slash<CTRL-d> to get a list of all help topics containing the word 'slash'.
%
guu converts entire line to lowercase. gUU converts entire line to uppercase. ~ inverts case of current character.
%
<CTRL-o> : trace your movements backwards in a file. <CTRL-i> trace your movements forwards in a file.
%
:ju(mps) : list your movements {{help|jump-motions}}
%
:history lists the history of your recent commands, sans duplicates.
%
"+y to copy to the X11 (or Windows) clipboard. "+p to paste from it.
%
noremap ' ` and noremap ` ' to make marks easier to navigate. Now ` is easier to reach!
%
2f/ would find the second occurrence of '/' in a line.
%
:tab sball will re-tab all files in the buffers list.
%
:%s/joe|fred/jerks/g will replace both 'fred' and 'joe' with 'jerks'.
%
* # g* g# each searches for the word under the cursor (forwards/backwards)
%
:vimgrep pattern **/*.txt will search all *.txt files in the current directory and its subdirectories for the pattern.
%
== will auto-indent the current line. Select text in visual mode, then = to auto-indent the selected lines.
%
Count the number of occurences of a word in a file with :%s/<word>//gn
%
:set foldmethod=syntax to make editing long files of code much easier. zo to open a fold. zc to close it. See more http://is.gd/9clX
%
Need to edit and run a previous command? q: then find the command, edit it, and Enter to execute the line.
%
@: to repeat the last executed command.
%
:e $MYVIMRC to directly edit your vimrc. :source $MYVIMRC to reload. Mappings may make it even easier.
%
g<CTRL-G> to see technical information about the file, such as how many words are in it, or how many bytes it is.
%
gq{movement} to wrap text, or just gq while in visual mode. gqap will format the current paragraph.
%
:E to see a simple file explorer. (:Ex will too, if that's easier to remember.)
%
:vimgrep pattern *.txt will search all .txt files in the current directory for the pattern.
%
:match ErrorMsg '\%>80v.\+' uses matching to highlight lines longer than 80 columns.
%
:%s/\r//g to remove all those nasty ^M from a file, or :%s/\r$//g for only at the end of a line.
%
% matches opening and closing chars (){}[], and with matchit.vim, def/end, HTML tags, etc. as well!
%
<CTRL-n><CTRL-p> offers word-completion while in insert mode.
%
<CTRL-x><CTRL-l> offers line completion in insert mode.
%
/<CTRL-r><CTRL-w> will pull the word under the cursor into search.
%
gf will open the file under the cursor. (Killer feature.)
%
Ctrl-a, Ctrl-x will increment and decrement, respectively, the number under the cursor. May be precede by a count.
%
:scriptnames will list all plugins and _vimrcs loaded.
%
:tabdo [some command] will execute the command in all tabs. Also see windo, bufdo, argdo.
%
:vsplit filename will split the window vertically and open the file in the left-hand pane. Great when writing unit tests!
%
qa starts a recording in register 'a'. q stops it. @a repeats the recording. 5@a repeats it 5 times.
%
:%s/\v(.*\n){5}/&\r will insert a blank line every 5 lines
%
Ctrl-c to quickly get out of command-line mode. (Faster than hitting ESC a couple times.)
%
Use '\v' in your regex to set the mode to 'very magic', and avoid confusion. (:h \v for more info.)
%
; is a motion to repeat last find with f. f' would find next quote. c; would change up to the next '
%
/\%>80v.\+ with search highlighting (:set hlsearch) will highlight any text after column 80.
%
ga will display the ASCII, hex, and octal value of the character under the cursor.
%
:%s/[.!?]\_s\+\a/\U&\E/g will uppercase the first letter of each sentence (except the very first one).
%
:r !date will insert the current date/time stamp (from the 'date' command -- a bit OS-specific).
%
:lcd %:p:h will change to the directory of the current file.
%
% matches brackets {} [] (), and with matchit.vim, also matches def/end, < ?php/?>, < p>/< /p>, etc.
%
:g/search_term/# display each line containing 'search_term' with line numbers.
%
:%s/<!--\_.\{-}-->// will delete HTML comments, potentially spanning multiple lines.
%
'. jumps to the last modified line. `. jumps to the exact position of last modification
%
[I (that's bracket open, capital i) show lines containing the word under the cursor.
%
:%s/\\/\//g replaces all backslashes with forward slashes
%
:vimgrep /stext/ **/*.txt | :copen searches for stext recursively in *.txt files and show results in separate window
%
ysiw' to surround current word with ',cs' {changes word to {word}} using the surround plugin: http://t.co/7QnLiwP3
%
use \v in your regex to set the mode to 'very magic' and avoid confusion (:h \v for more info) http://t.co/KWtRFNPI
%
in gvim, change the cursor depending on what mode you are (normal, insert, etc...) http://is.gd/9dq0
%
In visual mode, use " to surround the selected text with " using the surround plugin http://is.gd/fpwJQ
%
:tabo closes all tabs execpt the current one.
%
<C-U> / <C-D> move the cursor up/down half a page (also handy :set nosol)
%
:set titlestring=%f set the file name as the terminal title.
%
p / P paste after/before the cursor. Handy when inserting lines.
%
daw/caw deletes/changes the word under the cursor.
%
vim -d file1 file2 shows the differences between two files.
%
:set smartcase case sensitive if search contains an uppercase character and ignorecase is on.
%
:sh or :shell to open a console (then exit to come back to vim).
%
= : re-indent (e.g. == to re-indent the current line).
%
:%y c copies the entire buffer into register c. "cp inserts the content of register c in the current document.
%
ctrl-v blockwise visual mode (rectangular selection).
%
I/A switch to insert mode before/after the current line.
%
o/O insert a new line after/before the current line and switch to insert mode.
%
I/A in visual blockwise mode (ctrl-v) insert some text at the star/end of each line of the block text.
%
Need to edit a file in hex ? :help hex-editing gives you the manual.
%
Ctrl + o : Execute a command while in insert mode, then go back to insert mode. e.g. ctrl+o, p; paste without exiting insert mode
%
ctrl-r x (insert mode): insert the contents of buffer x. For example: "ctrl-r +" would insert the contents of the clipboard.
%
ctrl-r ctrl-w: Pull the word under the cursor in a : command. eg. :s/ctrl-r ctrl-w/foo/g
%
':%y c': yank entire file into register c. '"cp': Paste contents of c into document.
%
a/A : append at the cursor position / at the end of the line (enters insert mode)
%
ctrl-x ctrl-f (insert mode): complete with the file names in the current directory (ctrl-p/n to navigate through the candidates)
%
set mouse=a - enable mouse in terminal (selection, scroll, window resizing, ...).
%
J: join two lines
%
gg/G: go to start/end of file.
%
Ctrl-y (insert mode): insert character which is on the line above cursor. example: handy to initialize a structure.
%
:set nowrap - disable line wrapping
%
vim -p <files> - load all files listed in separate tabs. e.g. vim -p *.c
%
vmap out "zdmzO#if 0<ESC>"zp'zi#endif<CR><ESC> - macro to comment out a block of code using #if 0
%
<CTRT-W>v == :vsplit like <CTRL-w>s == :split
%
If gvim is started from a terminal it opens at the same width as the terminal. To prevent this, add "set columns=80" to ~/.vimrc
%
Prefixing G or gg (command mode) with a number will cause vim to jump to that line number.
%
set showbreak - set characters to prefix wrapped lines with. e.g. ":set showbreak=+++\ " (white space must be escaped)
%
When editing multiple files (e.g. vim *.c), use :n to move to the next file and :N to move to the previous file. :ar shows the list of files
%
:split - split the current window in two
%
vim --remote <file> - open a file in an existing vim session
%
A - enter insert mode at the end of the line (Append); I - insert before the first non-blank of the line
%
%< - resolves the current filename without extension. e.g. :e %<.h - open the header file for the current file
%
:set softtabstop <n> - set the number of spaces to insert when using the tab key (converted to tabs and spaces if expandtab is off).
%
:set expandtab - use spaces rather than the tab character to insert a tab.
%
:set guioptions - set various GUI vim options. e.g. to remove the menubar and toolbar, :set guioptions-=Tm
%
"vim - " - start vim and read from standard input. e.g. with syntax enabled, get a coloured diff from git: git diff | vim -
%
set mousemodel=popup - enable a popup menu on right click in GUI vim
%
r!cat - reads into the buffer from stdin and avoids using :set paste (use ctrl-d to finish)
%
:set title - display info in terminal title. Add let &titleold=getcwd() to .vimrc to set it to something useful on quit
%
:set pastetoggle=key - specify a key sequence that toggles the paste option, e.g. set pastetoggle=<F2>
%
:set paste - allows you to paste from the clipboard correctly, especially when vim is running in a terminal
%
substitute flag 'n' - count how many substitutions would be made, but don't actually make any
%
set wildmenu - enhanced filename completion. left and right navigates matches; up and down navigates directories
%
zt, zz, zb: scroll so that the current position of the cursor is moved to the top, middle or bottom of the screen
%
[range]sort - sort the lines in the [range], or all lines if [range] is not given. e.g. :'<,'>sort - sort the current visual selection
%
%:exec ":new ".(substitute(expand("%"), ".c$", ".h", "")) - open the associated .h file for the current .c file in a new window; more concisely :new %:p:r.h
%
noh - stop highlighting the current search (if 'hlsearch' is enabled). Highlighting is automatically restored for the next search.
%
when substituting, \u makes just first character upper (like \l for lower) and \U is upper equivalent for \L
%
:retab <ts> - convert strings of white-space containing <Tab> with new strings using the <ts> value if given or current value of 'tabstop'
%
ctrl-v u <hex code> - enter a unicode character in insert mode
%
:set laststatus=2 - always show the status line (0 = never, 1 = (default) only if there are two or more windows, 2 = always)
%
b - go back a word (opposite of w)
%
} - move to the next blank line ( { - move to previous blank line)
%
s - delete characters and start insert mode (s stands for Substitute). e.g. 3s - delete three characters and start insert mode.
%
0 - Move to the first character of the line
%
:set columns=X - set the width of the window to X columns. For GUI vim, this is limited to the size of the screen
%
:only - close all windows except the current one (alternatives: ctrl-w ctrl-o or :on)
%
ctrl-<pagedown> / ctrl-<pageup> - switch to next/previous tab. (alternatives: gt/gT, :tabn/:tabp, etc)
%
:tabe <filename> - open <filename> in a new tab (same as :tabedit and :tabnew)
%
Ctrl-T and Ctrl-D - indent and un-indent the current line in insert mode
%
vim +<num> - start vim and place the cursor on line <lnum>. If lnum is not specified, start at the end of the file
%
gj, gk (or g<Up> g<Down>) - move up or down a display line (makes a difference for wrapped lines)
%
>{motion} and <{motion} - (normal mode) increase/decrease the current indent. e.g. << - decrease the indent of the current line
%
"+ and "* - clipboard and current selection registers under X. e.g. "+p to paste from the clipboard and "+y to copy to the clipboard
%
:r!<cmd> - insert the result of <cmd> into the current buffer at the cursor. e.g. :r!ls *.h
%
& - re-run last :s command (&& to remember flags)
%
set wildignore - ignore matching files when using tab complete on filenames. e.g. :set wildignore=*.o,*.lo
%
CTRL-V <tab> - in insert mode, enters a real tab character, disregarding tab and indent options
%
CTRL-U/CTRL-D - scroll up/down, moving the cursor the same number of lines if possible (unlike <PageUp>/<PageDown>)
%
:set cursorline - Highlight the current line under the cursor
%
:set showcmd - show the number of lines/chacters in a visual selection
%
:x is like ":wq", but write only when changes have been made
%
ctrl-b / ctrl-f : page up / page down
%
ctrl-clic / ctrl-t : go to symbol definition (= ctrl-]) (using tags) and back. You can use "make tags" autotooled projects to create tags
%
0/joe/+3 -- find joe move cursor 3 lines down
%
/^joe.*fred.*bill/ -- find joe AND fred AND Bill (Joe at start of line)
%
/^[A-J]/ -- search for lines beginning with one or more A-J
%
/begin\_.*end -- search over possible multiple lines
%
/fred\_s*joe/ -- any whitespace including newline [C]
%
/fred\|joe -- Search for FRED OR JOE
%
/.*fred\&.*joe -- Search for FRED AND JOE in any ORDER!
%
/\<fred\>/ -- search for fred but not alfred or frederick [C]
%
/\<\d\d\d\d\> -- Search for exactly 4 digit numbers
%
/\D\d\d\d\d\D -- Search for exactly 4 digit numbers
%
/\<\d\{4}\> -- same thing
%
/\([^0-9]\|^\)%.*% -- Search for absence of a digit or beginning of line
%
/^\n\{3} -- find 3 empty lines -- finding empty lines
%
/^str.*\nstr -- find 2 successive lines starting with str
%
/\(^str.*\n\)\{2} -- find 2 successive lines starting with str
%
/\(fred\).*\(joe\).*\2.*\1 -- using rexexp memory in a search find fred.*joe.*joe.*fred *C*
%
/^\([^,]*,\)\{8} -- Repeating the Regexp (rather than what the Regexp finds)
%
:vmap // y/<C-R>"<CR> -- search for visually highlighted text -- visual searching
%
:vmap <silent> // y/<C-R>=escape(@", '\\/.*$^~[]')<CR><CR> -- with spec chars
%
/<\zs[^>]*\ze> -- search for tag contents, ignoring chevrons -- \zs and \ze regex delimiters :h /\zs
%
/<\@<=[^>]*>\@= -- search for tag contents, ignoring chevrons -- zero-width :h /\@=
%
/<\@<=\_[^>]*>\@= -- search for tags across possible multiple lines
%
/<!--\_p\{-}--> -- search for multiple line comments -- searching over multiple lines \_ means including newline
%
/fred\_s*joe/ -- any whitespace including newline *C*
%
/bugs\(\_.\)*bunny -- bugs followed by bunny anywhere in file
%
:h \_ -- help
%
:nmap gx yiw/^\(sub\<bar>function\)\s\+<C-R>"<CR> -- search for declaration of subroutine/function under cursor
%
:bufdo /searchstr/ -- use :rewind to recommence search -- multiple file search
%
:bufdo %s/searchstr/&/gic -- say n and then a to stop -- multiple file search better but cheating
%
?http://www.vim.org/ -- (first) search BACKWARDS!!! clever huh! -- How to search for a URL without backslashing
%
/\c\v([^aeiou]&\a){4} -- search for 4 consecutive consonants -- Specify what you are NOT searching for (vowels)
%
/\%>20l\%<30lgoat -- Search for goat between lines 20 and 30 [N]
%
/^.\{-}home.\{-}\zshome/e -- match only the 2nd occurence in a line of "home" [N]
%
:%s/home.\{-}\zshome/alone -- Substitute only the occurrence of home in any line [N]
%
^\(.*tongue.*\)\@!.*nose.*$ -- find str but not on lines containing tongue
%
\v^((tongue)@!.)*nose((tongue)@!.)*$
%
.*nose.*\&^\%(\%(tongue\)\@!.\)*$
%
:v/tongue/s/nose/&/gic
%
:%s/fred/joe/igc -- general substitute command -- *best-substitution*
%
:%s//joe/igc -- Substitute what you last searched for [N]
%
:%s/~/sue/igc -- Substitute your last replacement string [N]
%
:%s/\r//g -- Delete DOS returns ^M
%
:%s/\r/\r/g -- Turn DOS returns ^M into real returns -- Is your Text File jumbled onto one line? use following
%
:%s= *$== -- delete end of line blanks
%
:%s= \+$== -- Same thing
%
:%s#\s*\r\?$## -- Clean both trailing spaces AND DOS returns
%
:%s#\s*\r*$## -- same thing
%
:%s/^\n\{3}// -- delete blocks of 3 empty lines -- deleting empty lines
%
:%s/^\n\+/\r/ -- compressing empty lines
%
:%s#<[^>]\+>##g -- delete html tags, leave text (non-greedy)
%
:%s#<\_.\{-1,}>##g -- delete html tags possibly multi-line (non-greedy)
%
:%s#.*\(\d\+hours\).*#\1# -- Delete all but memorised string (\1) [N]
%
%s#><\([^/]\)#>\r<\1#g -- split jumbled up XML file into one tag per line [N]
%
:'a,'bg/fred/s/dick/joe/igc -- VERY USEFUL -- VIM Power Substitute
%
:%s= [^ ]\+$=&&= -- duplicate end column -- duplicating columns
%
:%s= \f\+$=&&= -- same thing
%
:%s= \S\+$=&& -- usually the same
%
:%s#example#& = &#gic -- duplicate entire matched string [N] -- memory
%
:%s#.*\(tbl_\w\+\).*#\1# -- extract list of all strings tbl_* from text [NC]
%
:s/\(.*\):\(.*\)/\2 -- \1/ : reverse fields separated by :
%
:%s/^\(.*\)\n\1$/\1/ -- delete duplicate lines
%
:%s/^\(.*\)\(\n\1\)\+$/\1/ -- delete multiple duplicate lines [N]
%
:%s/^.\{-}pdf/new.pdf/ -- delete to 1st occurence of pdf only (non-greedy) -- non-greedy matching \{-}
%
:%s#\<[zy]\?tbl_[a-z_]\+\>#\L&#gc -- lowercase with optional leading characters -- use of optional atom \?
%
:%s/<!--\_.\{-}-->// -- delete possibly multi-line comments -- over possibly many lines
%
:help /\{-} -- help non-greedy
%
:s/fred/<c-r>a/g -- sub "fred" with contents of register "a" -- substitute using a register
%
:s/fred/<c-r>asome_text<c-r>s/g
%
:s/fred/\=@a/g -- better alternative as register not displayed (not *) [C]
%
:%s/\f\+\.gif\>/\r&\r/g | v/\.gif$/d | %s/gif/jpg/ -- multiple commands on one line
%
:%s/a/but/gie|:update|:next -- then use @: to repeat
%
:%s/goat\|cow/sheep/gc -- ORing (must break pipe) -- ORing
%
:'a,'bs#\[\|\]##g -- remove [] from lines between markers a and b [N]
%
:%s/\v(.*\n){5}/&\r -- insert a blank line every 5 lines [N]
%
:s/__date__/\=strftime("%c")/ -- insert datestring -- Calling a VIM function
%
:inoremap \zd <C-R>=strftime("%d%b%y")<CR> -- insert date eg 31Jan11 [N]
%
:%s:\(\(\w\+\s\+\)\{2}\)str1:\1str2: -- Working with Columns sub any str1 in col3
%
:%s:\(\w\+\)\(.*\s\+\)\(\w\+\)$:\3\2\1: -- Swapping first & last column (4 columns)
%
:%s#\<from\>\|\<where\>\|\<left join\>\|\<\inner join\>#\r&#g -- format a mysql query
%
:redir @*|sil exec 'g#<\(input\|select\|textarea\|/\=form\)\>#p'|redir END -- filter all form elements into paste register
%
:nmap ,z :redir @*<Bar>sil exec 'g@<\(input\<Bar>select\<Bar>textarea\<Bar>/\=form\)\>@p'<Bar>redir END<CR>
%
:%s/^\(.\{30\}\)xx/\1yy/ -- substitute string in column 30 [N]
%
:%s/\d\+/\=(submatch(0)-3)/ -- decrement numbers by 3
%
:g/loc\|function/s/\d/\=submatch(0)+6/ -- increment numbers by 6 on certain lines only
%
:%s#txtdev\zs\d#\=submatch(0)+1#g -- better
%
:h /\zs
%
:%s/\(gg\)\@<=\d\+/\=submatch(0)+6/ -- increment only numbers gg\d\d by 6 (another way)
%
:h zero-width
%
:let i=10 | 'a,'bg/Abc/s/yy/\=i/ |let i=i+1 # convert yy to 10,11,12 etc -- rename a string with an incrementing number
%
:let i=10 | 'a,'bg/Abc/s/xx\zsyy\ze/\=i/ |let i=i+1 # convert xxyy to xx11,xx12,xx13 -- as above but more precise
%
:%s/"\([^.]\+\).*\zsxx/\1/ -- find replacement text, put in memory, then use \zs to simplify substitute
%
:nmap <leader>z :%s#\<<c-r>=expand("<cword>")<cr>\># -- Pull word under cursor into LHS of a substitute
%
:vmap <leader>z :<C-U>%s/\<<c-r>*\>/ -- Pull Visually Highlighted text into LHS of a substitute
%
:'a,'bs/bucket\(s\)*/bowl\1/gic [N] -- substitute singular or plural
%
:%s,\(all/.*\)\@<=/,_,g -- replace all / with _ AFTER "all/"
%
:s#all/\zs.*#\=substitute(submatch(0), '/', '_', 'g')# -- Same thing
%
:s#all/#&^M#|s#/#_#g|-j! -- Substitute by splitting line, then re-joining
%
:%s/.*/\='cp '.submatch(0).' all/'.substitute(submatch(0),'/','_','g')/ -- Substitute inside substitute
%
:g/gladiolli/# -- display with line numbers (YOU WANT THIS!) -- *best-global* command
%
:g/fred.*joe.*dick/ -- display all lines fred,joe & dick
%
:g/\<fred\>/ -- display all lines fred but not freddy
%
:g/^\s*$/d -- delete all blank lines
%
:g!/^dd/d -- delete lines not containing string
%
:v/^dd/d -- delete lines not containing string
%
:g/joe/,/fred/d -- not line based (very powerfull)
%
:g/fred/,/joe/j -- Join Lines [N]
%
:g/{/ ,/}/- s/\n\+/\r/g -- Delete empty lines but only between {...}
%
:v/\S/d -- Delete empty lines (and blank lines ie whitespace)
%
:v/./,/./-j -- compress empty lines
%
:g/^$/,/./-j -- compress empty lines
%
:g/<input\|<form/p -- ORing
%
:g/^/put_ -- double space file (pu = put)
%
:g/^/m0 -- Reverse file (m = move)
%
:g/^/m$ -- No effect! [N]
%
:'a,'bg/^/m'b -- Reverse a section a to b
%
:g/^/t. -- duplicate every line
%
:g/fred/t$ -- copy (transfer) lines matching fred to EOF
%
:g/stage/t'a -- copy (transfer) lines matching stage to marker a (cannot use .) [C]
%
:g/^Chapter/t.|s/./-/g -- Automatically underline selecting headings [N]
%
:g/\(^I[^^I]*\)\{80}/d -- delete all lines containing at least 80 tabs
%
:g/^/ if line('.')%2|s/^/zz / -- perform a substitute on every other line
%
:'a,'bg/somestr/co/otherstr/ -- co(py) or mo(ve)
%
:'a,'bg/str1/s/str1/&&&/|mo/str2/ copy or move or substitute
%
:%norm jdd -- delete every other line
%
:.,$g/^\d/exe "norm! \<c-a>" -- increment numbers
%
:'a,'bg/\d\+/norm! ^A -- increment numbers
%
:g/fred/y A -- append all lines fred to register a
%
:g/fred/y A | :let @*=@a -- put into paste buffer
%
:let @a=''|g/Barratt/y A |:let @*=@a
%
:'a,'bg/^Error/ . w >> errors.txt -- filter lines to a file (file must already exist)
%
:g/./yank|put|-1s/'/"/g|s/.*/Print '&'/ -- duplicate every line in a file wrap a print '' around each duplicate
%
:g/^MARK$/r tmp.txt | -d -- replace string with contents of a file, -d deletes the "mark"
%
:g/<pattern>/z#.5 -- display with context -- display prettily
%
:g/<pattern>/z#.5|echo "==========" -- display beautifully
%
:g/|/norm 2f|r* -- replace 2nd | with a star -- Combining g// with normal mode commands
%
:nmap <F3> :redir @a<CR>:g//<CR>:redir END<CR>:new<CR>:put! a<CR><CR> -- send output of previous global command to a new window
%
:'a,'bg/fred/s/joe/susan/gic -- can use memory to extend matching -- *Best-Global-combined-with-substitute* (*power-editing*)
%
:/fred/,/joe/s/fred/joe/gic -- non-line based (ultra)
%
:/biz/,/any/g/article/s/wheel/bucket/gic: non-line based [N]
%
:/fred/;/joe/-2,/sid/+3s/sally/alley/gIC -- Find fred before beginning search for joe
%
:g/^/exe ".w ".line(".").".txt" -- create a new file for each line of file eg 1.txt,2.txt,3,txt etc
%
:.g/^/ exe ".!sed 's/N/X/'" | s/I/Q/ [N] -- chain an external command
%
d/fred/ :delete until fred -- Operate until string found [N]
%
y/fred/ :yank until fred
%
c/fred/e :change until fred end
%
. last edit (magic dot) -- Summary of editing repeats [N]
%
:& last substitute
%
:%& last substitute every line
%
:%&gic last substitute every line confirm
%
g% normal mode repeat last substitute
%
g& last substitute on all lines
%
@@ last recording
%
@: last command-mode command
%
:!! last :! command
%
:~ last substitute
%
:help repeating
%
; last f, t, F or T -- Summary of repeated searches
%
, last f, t, F or T in opposite direction
%
n last / or ? search
%
N last / or ? search in opposite direction
%
* # g* g# -- find word under cursor (<cword>) (forwards/backwards)
%
% -- match brackets {}[]()
%
. -- repeat last modification
%
@: -- repeat last : command (then @@)
%
matchit.vim -- % now matches tags <tr><td><script> <?php etc
%
<C-N><C-P> -- word completion in insert mode
%
<C-X><C-L> -- Line complete SUPER USEFUL
%
/<C-R><C-W> -- Pull <cword> onto search/command line
%
/<C-R><C-A> -- Pull <CWORD> onto search/command line
%
:set ignorecase -- you nearly always want this
%
:set smartcase -- overrides ignorecase if uppercase used in search string (cool)
%
:syntax on -- colour syntax in Perl,HTML,PHP etc
%
:set syntax=perl -- force syntax (usually taken from file extension)
%
:h regexp<C-D> -- type control-D and get a list all help topics containing regexp (plus use TAB to Step thru list)
%
:nmap ,s :source $VIM/_vimrc -- MAKE IT EASY TO UPDATE/RELOAD _vimrc
%
:nmap ,v :e $VIM/_vimrc
%
:e $MYVIMRC -- edits your _vimrc whereever it might be [N]
%
:vsplit other.php # vertically split current file with other.php [N] -- splitting windows
%
:vmap sb "zdi<b><C-R>z</b><ESC> -- wrap <b></b> around VISUALLY selected Text
%
:vmap st "zdi<?= <C-R>z ?><ESC> -- wrap <?= ?> around VISUALLY selected Text
%
vim -p fred.php joe.php -- open files in tabs
%
:tabe fred.php -- open fred.php in a new tab
%
:tab ball -- tab open files
%
:close -- close a tab but leave the buffer *N*
%
:nnoremap gf <C-W>gf -- vim 7 forcing use of tabs from .vimrc
%
:cab e tabe
%
:tab sball -- retab all files in buffer (repair) [N]
%
:e . -- file explorer -- Exploring
%
:Exp(lore) -- file explorer note capital Ex
%
:Sex(plore) -- file explorer in split window
%
:browse e -- windows style browser
%
:ls -- list of buffers
%
:cd .. -- move to parent directory
%
:args -- list of files
%
:pwd -- Print Working Directory (current directory) [N]
%
:args *.php -- open list of files (you need this!)
%
:lcd %:p:h -- change to directory of current file
%
:autocmd BufEnter * lcd %:p:h -- change to directory of current file automatically (put in _vimrc)
%
guu -- lowercase line -- Changing Case
%
gUU -- uppercase line
%
Vu -- lowercase line
%
VU -- uppercase line
%
g~~ -- flip case line
%
vEU -- Upper Case Word
%
vE~ -- Flip Case Word
%
ggguG -- lowercase entire file
%
vmap ,c :s/\<\(.\)\(\k*\)\>/\u\1\L\2/g<CR> -- Titlise Visually Selected Text (map for .vimrc)
%
vnoremap <F6> :s/\%V\<\(\w\)\(\w*\)\>/\u\1\L\2/ge<cr> [N] -- Title Case A Line Or Selection (better)
%
nmap ,t :s/.*/\L&/<bar>:s/\<./\u&/g<cr> [N] -- titlise a line
%
:%s/[.!?]\_s\+\a/\U&\E/g -- Uppercase first letter of sentences
%
gf -- open file name under cursor (SUPER)
%
:nnoremap gF :view <cfile><cr> -- open file under cursor, create if necessary
%
ga -- display hex,ascii value of char under cursor
%
ggg?G -- rot13 whole file (quicker for large file)
%
:8 | normal VGg? -- rot13 from line 8
%
:normal 10GVGg? -- rot13 from line 8
%
<C-A>,<C-X> -- increment,decrement number under cursor
%
win32 users must remap CNTRL-A
%
<C-R>=5*5 -- insert 25 into text (mini-calculator)
%
:h 42 -- also http://www.google.com/search?q=42 -- Make all other tips superfluous
%
:h holy-grail
%
:h!
%
ggVGg? -- rot13 whole file (toggles) -- disguise text (watch out) [N]
%
:set rl! -- reverse lines right to left (toggles)
%
:g/^/m0 -- reverse lines top to bottom (toggles)
%
:%s/\(\<.\{-}\>\)/\=join(reverse(split(submatch(1), '.\zs')), '')/g -- reverse all text *N*
%
g; -- cycle thru recent changes (oldest first)
%
g, -- reverse direction
%
:changes
%
:h changelist -- help for above
%
:help jump-motions
%
:history -- list of all your commands
%
:his c -- commandline history
%
:his s -- search history
%
q/ -- Search history Window (puts you in full edit mode) (exit CTRL-C)
%
q: -- commandline history Window (puts you in full edit mode) (exit CTRL-C)
%
:<C-F> -- history Window (exit CTRL-C)
%
:map <f7> :'a,'bw! c:/aaa/x -- save text to file x
%
:map <f8> :r c:/aaa/x -- retrieve text
%
:map <f11> :.w! c:/aaa/xr<CR> -- store current line
%
:map <f12> :r c:/aaa/xr<CR> -- retrieve current line
%
:ab php -- list of abbreviations beginning php
%
:map , -- list of maps beginning ,
%
set wak=no -- :h winaltkeys -- allow use of F10 for mapping (win32)
%
<CR> -- carriage Return for maps -- For use in Maps
%
<ESC> -- Escape
%
<LEADER> -- normally \
%
<BAR> -- | pipe
%
<BACKSPACE> -- backspace
%
<SILENT> -- No hanging shell window
%
:nmap <leader>c :hi Normal guibg=#<c-r>=expand("<cword>")<cr><cr> -- display RGB colour under the cursor eg #445588
%
map <f2> /price only\\|versus/ :in a map need to backslash the \
%
imap ,,, <esc>bdwa<<esc>pa><cr></<esc>pa><esc>kA -- type table,,, to get <table></table> ### Cool ###
%
:for i in range(1, 12) | execute("map <F".i.">") | endfor [N] -- list current mappings of all your function keys
%
:cab ,f :for i in range(1, 12) \| execute("map <F".i.">") \| endfor -- for your .vimrc
%
iab phpdb exit("<hr>Debug <C-R>a "); -- Simple PHP debugging display all variables yanked into register a
%
:let @m=":'a,'bs/" -- Using a register as a map (preload registers in .vimrc)
%
:let @s=":%!sort -u"
%
"ayy@a -- execute "Vim command" in a text file -- Useful tricks
%
yy@" -- same thing using unnamed register
%
u@. -- execute command JUST typed in
%
"ddw -- store what you delete in register d [N]
%
"ccaw -- store what you change in register c [N]
%
:r!ls -R -- reads in output of ls -- Get output from other commands (requires external programs)
%
:put=glob('**') -- same as above [N]
%
:r !grep "^ebay" file.txt -- grepping in content [N]
%
:20,25 !rot13 -- rot13 lines 20 to 25 [N]
%
!!date -- same thing (but replaces/filters current line)
%
:%!sort -u -- use an external program to filter content -- Sorting with external sort
%
:'a,'b!sort -u -- use an external program to filter content
%
!1} sort -u -- sorts paragraph (note normal mode!!)
%
:g/^$/;/^$/-1!sort -- Sort each block (note the crucial ;)
%
:sort /.*\%2v/ -- sort all lines on second column [N] -- Sorting with internal sort
%
:new | r!nl # [N] -- number lines (linux or cygwin only)
%
:bn -- goto next buffer -- Multiple Files Management (Essential)
%
:bp -- goto previous buffer
%
:wn -- save file and move to next (super)
%
:wp -- save file and move to previous
%
:bd -- remove file from buffer list (super)
%
:bun -- Buffer unload (remove window but not from list)
%
:badd file.c -- file from buffer list
%
:b3 -- go to buffer 3 [C]
%
:b main -- go to buffer with main in name eg main.c (ultra)
%
:sav php.html -- Save current file as php.html and "move" to php.html
%
:sav! %<.bak -- Save Current file to alternative extension (old way)
%
:sav! %:r.cfm -- Save Current file to alternative extension
%
:sav %:s/fred/joe/ -- do a substitute on file name
%
:sav %:s/fred/joe/:r.bak2 -- do a substitute on file name & ext.
%
:!mv % %:r.bak -- rename current file (DOS use Rename or DEL)
%
:help filename-modifiers
%
:e! -- return to unmodified file
%
:w c:/aaa/% -- save file elsewhere
%
:e # -- edit alternative file (also cntrl-^)
%
:rew -- return to beginning of edited files list (:args)
%
:brew -- buffer rewind
%
:sp fred.txt -- open fred.txt into a split
%
:sball,:sb -- Split all buffers (super)
%
:scrollbind -- in each split window
%
:map <F5> :ls<CR>:e # -- Pressing F5 lists all buffer, just type number
%
:set hidden -- Allows to change buffer w/o saving current buffer
%
map <C-J> <C-W>j<C-W>_ -- Quick jumping between splits
%
map <C-K> <C-W>k<C-W>_
%
@@ -- Repeat a macro
%
5@@ -- Repeat a macro 5 times
%
qQ@qq -- Make an existing recording q recursive [N]
%
"qp --display contents of register q (normal mode) -- editing a register/recording
%
<ctrl-R>q --display contents of register q (insert mode)
%
"qdd --put changed contacts back into q -- you can now see recording contents, edit as required
%
v -- enter visual mode
%
V -- visual mode whole line
%
<C-V> -- enter VISUAL BLOCKWISE mode (remap on Windows to say C-Q *C*
%
gv -- reselect last visual area (ultra)
%
o -- navigate visual area
%
"*y or "+y -- yank visual area into paste buffer [C]
%
V% -- visualise what you match
%
V}J -- Join Visual block (great)
%
V}gJ -- Join Visual block w/o adding spaces
%
`[v`] -- Highlight last insert
%
:%s/\%Vold/new/g -- Do a substitute on last visual area [N]
%
08l<c-v>10j2ld (use Control Q on win32) [C] -- Delete 8th and 9th characters of 10 successive lines [C]
%
<C-V> then select "column(s)" with motion commands (win32 <C-Q>)
%
daW -- delete contiguous non whitespace -- text objects :h text-objects [C]
%
di< yi< ci< -- Delete/Yank/Change HTML tag contents
%
da< ya< ca< -- Delete/Yank/Change whole HTML tag
%
dat dit -- Delete HTML tag pair
%
diB daB -- Empty a function {}
%
das -- delete a sentence
%
:imap <TAB> <C-N> -- set tab to complete [N] -- _vimrc essentials
%
:set incsearch -- jumps to search word as you type (annoying but excellent)
%
:set wildignore=*.o,*.obj,*.bak,*.exe -- tab complete now ignores these
%
:set shiftwidth=3 -- for shift/tabbing
%
:set vb t_vb=". -- set silent (no beep)
%
:set browsedir=buffer -- Maki GUI File Open use current directory
%
:nmap ,f :update<CR>:silent !start c:\progra~1\intern~1\iexplore.exe file://%:p<CR> -- launching Win IE
%
:nmap ,i :update<CR>: !start c:\progra~1\intern~1\iexplore.exe <cWORD><CR>
%
cmap ,r :Nread ftp://209.51.134.122/public_html/index.html -- FTPing from VIM
%
cmap ,w :Nwrite ftp://209.51.134.122/public_html/index.html
%
gvim ftp://www.somedomain.com/index.html # uses netrw.vim
%
"a5yy10j"A5yy
%
[I -- show lines matching word under cursor <cword> (super)
%
:'a,'b>> -- Conventional Shifting/Indenting
%
:vnoremap < <gv -- visual shifting (builtin-repeat)
%
:vnoremap > >gv
%
>i{ -- Block shifting (magic)
%
>a{ -- Block shifting (magic)
%
>% and <% -- Block shifting (magic)
%
== -- index current line same as line above [N]
%
:redir @* -- redirect commands to paste buffer -- Redirection & Paste register *
%
:redir END -- end redirect
%
:redir >> out.txt -- redirect to a file
%
"*yy -- yank curent line to paste -- Working with Paste buffer
%
"*p -- insert from paste buffer
%
:'a,'by* -- Yank range into paste -- yank to paste buffer (ex mode)
%
:%y* -- Yank whole buffer into paste
%
:.y* -- Yank Current line to paster -- filter non-printable characters from the paste buffer -- useful when pasting from some gui application
%
:nmap <leader>p :let @* = substitute(@*,'[^[:print:]]','','g')<cr>"*p
%
:set paste -- prevent vim from formatting pasted in text
%
gq} -- Format a paragraph -- Re-Formatting text
%
gqap -- Format a paragraph
%
ggVGgq -- Reformat entire file
%
Vgq -- current line
%
:s/.\{,69\};\s*\|.\{,69\}\s\+/&\r/g -- break lines at 70 chars, if possible after a ;
%
:argdo %s/foo/bar/e -- operate on all files in :args -- Operate command over multiple files
%