-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVim.txt
More file actions
1594 lines (1151 loc) · 41.2 KB
/
Vim.txt
File metadata and controls
1594 lines (1151 loc) · 41.2 KB
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
**************************************************
Window Splitting
**************************************************
:vsp Vertical split
:sp Horizontal split
:vsp <file> Opens <file> in new window
:10sp Specify split height
:set splitright
Opens window in right [for vertical splits] and becomes active
:set splitbelow
Same as above, but for horizontal window
CTRL+w is same as :wincmd
* Wherever we use CTRL+w, we can do :wincmd
CTRL+w <number> <
reduce width of window by <number>
CTRL+w <number> >
increase width of window by <number>
<number> CTRL+w +
increase length of window by <number>
<number> CTRL+w -
decrease length of window by <number>
CTRL+w |
max out window width
CTRL+w _
max out window height
CTRL+w =
normalize all window sizes (equal sizes)
CTRL+w R
swap both windows [including buffers]
<number> CTRL+w w
switch to window <number>
CTRL+w T
make current split window in to a tab (capital T)
**************************************************
Tab Views
**************************************************
:tabnew <file>
opens <file> in a new tab
:tabp
previous tab
:tabn
next tab
In Normal mode
gt // go to next tab
gT // go to previous tab
ngt // got to tab no. n
vim -p file1 file 2 // open multiple files in tab mode
**************************************************
Regular Expressions
**************************************************
. (dot) Any single character except newline
* zero or more occurances of any character
[...] Any single character specified in the set
[^...] Any single character not specified in the set
^ Anchor - beginning of the line
$ Anchor - end of line
\< Anchor - begining of word
\> Anchor - end of word
\(...\) Grouping - usually used to group conditions
\n Contents of nth grouping
[...] - Set Examples
--------------------
[A-Z] The SET from Capital A to Capital Z
[a-z] The SET from lowercase a to lowercase z
[0-9] The SET from 0 to 9 (All numerals)
[./=+] The SET containing . (dot), / (slash), =, and +
[-A-F] The SET from Capital A to Capital F and the dash (dashes must be specified first)
[0-9 A-Z] The SET containing all capital letters and digits and a space
[A-Z][a-zA-Z] In the first position, the SET from Capital A to Capital Z
In the second character position, the SET containing all letters
Regular Expression Examples
---------------------------
/Hello/ Matches if the line contains the value Hello
/^TEST$/ Matches if the line contains TEST by itself
/^[a-zA-Z]/ Matches if the line starts with any letter
/^[a-z].*/ Matches if the first character of the line is a-z and there is at least one more of any character following it
/2134$/ Matches if line ends with 2134
/\(21|35\)/ Matches if the line contains 21 or 35
Note the use of ( ) with the pipe symbol to specify the 'or' condition
/[0-9]*/ Matches if there are zero or more numbers in the line
/^[^#]/ Matches if the first character is not a # in the line
- Regular expressions are case sensitive
- Regular expressions are to be used where pattern is specified
Reference for Regex:
- http://vimregex.com/
**************************************************
Code Folding
**************************************************
Reference - :help folding
:help za
:help zo
:help zc
Synopsis - Using commands here, code in a file can be temporarily folded
[hidden]. Folding helps in navigating a file quickly as parts of the file
is hidden.
zc - close a fold
zo - open a fold
za - toggle a fold [open if its closed and vice versa]
zC, zO, zA are similar but inner folding levels are also affected by them,
not just the level at which cursor is present.
**************************************************
Motion
**************************************************
CTRL+d Scroll half page down
CTRL+u Scroll half a page up
CTRL+f Scroll full page down
CTRL+b Scroll full page up
L last line of current screen
M middle line of current screen
^e move the screen by one line down, with cursor staying in its place
^y move the screen up by one line
0 beginning of the line
$ end of the line
^ first non-blank character of the line
g_ last non-blank character of the line
n% takes you to nth percent position of the file
* search for occurance of word under cursor
# same as *, but move in opposite direction
Q: What is a word in Vim?
- Hyphen, Exclamation, Period delianated ... stuff?
Word wise navigation:
w small word
W big word
e vs E
b vs B
Scrolling
---
^E or ^e Scroll window down
^Y or ^y Scroll window up
^F Scroll down one page
^B Scroll up one page
H Move cursor to top of window
M Move cursor to middle of window
L Move cursor to bottom of window
zt puts current line to top of screen
z. or zz puts current line to center of screen
zb puts current line to bottom of screen
Ref: http://unix.stackexchange.com/a/110252/45157
**************************************************
Key Mappings
**************************************************
{cmd} {attr} {lhs} {rhs}
where
{cmd} is one of ':map', ':map!', ':nmap', ':vmap', ':imap',
<buffer>, <silent>, <expr> <script>, <unique> and
<special>. More than one attribute can be specified to a map.
{lhs} left hand side, is a sequence of one or more keys that are being
mapped.
{rhs} right hand side, is the sequence of keys that the {lhs} keys are
Ex: Map Ctrl+Tab to next tab and Ctrl+Shift+Tab to prev tab
nnoremap <C-Tab> <ESC>:tabnext<CR>
nnoremap <C-S-Tab> <ESC>:tabprevious<CR>
# Unmap arrow keys
no <up> <Nop>
no <down> <Nop>
no <left> <Nop>
no <right> <Nop>
**************************************************
Miscellaneous
**************************************************
gf ("goto file") Open file under cursor in current window
CTRL+w f Open file under cursor in (horizontal) split window
CTRL+w gf Open file under cursor in new tab
More details here: https://vim.fandom.com/wiki/Open_file_under_cursor
vim -O file1 file2 // open two files side by side
Substituting Tips
-----------------
Save typing by using \zs and \ze to set the start and end of a pattern.
For example, instead of:
:s/Copyright 2007 All Rights Reserved/Copyright 2008 All Rights Reserved/
Use:
:s/Copyright \zs2007\ze All Rights Reserved/2008/
Diff
----
vimdiff file1 file2 // diff two files side by side
Reformatting
------------
In the .vimrc file, `set textwidth=80` (or whatever column width).
gg - go to top of file
gqG - globally set the entire file to 80 width column. It joins lines. If you
dont want lines to be joined, have a blank one in between.
gq} - change only current paragraph
G - go to beginning of last line
ga - Gives ASCII value of character under cursor
Power of global(g) command
------------------
:[range]g/pattern/command
- Above command acts on [range] (default - whole file), by executing 'cmd'
command, for each line matching 'pattern'.
Examples:
:g/pattern/d
- Delete all lines matching 'pattern'
:g/pattern/z#.5
- Display context (5 lines) for all occurrences of a pattern.
:g/pattern/z#.5 | echo "==========="
- Same as above but with some beautification.
:g!/pattern/d
:v/pattern/d
- Do NOT delete lines matching 'pattern'
:g/^\s*$/d
- Delete all blank lines. '^' start of line, '\s*' is zero or more white
spaces, '$' end of line.
http://vim.wikia.com/wiki/Power_of_g
Wikia Tips
----------
1. Insert line numbers in file (not just display) or a portion of file?
http://vim.wikia.com/wiki/Insert_line_numbers
:%s/^/\=printf("%d", line(".")) // prints current line number. Read
// above link for more granularity.
Code Indentation
----------------
1. Shift+V and then up/down
2. hit =
Vim Tips from Youtube talks
---------------------------
1) Highlight 80th column
" Make the 81st column stand out with magenta color
highlight ColorColumn ctermbg=magenta
" Call ColorColumn on 81st column virtually.
call matchadd('ColorColumn', '\81xv', 100)
Completion tips (tags, file name etc):
-------
2)
CTRL+r --> insert text from a register
CTRL+a --> insert last inserted text (access . register)
CTRL+p --> Keyword completion. 'p' means, look back in the document
CTRL+n --> Keyword completion. 'n' mean next
CTRL+x --> puts you in a special X mode (submode of insert mode). This allows
you to autocomplete tags using tags file.
CTRL+x CTRL+] --> completes a class name (and such tags) when pressed after
letter
3) Vim keeps track of Jump List: files that Vim jumped to.
CTRL+i, CTRL+o --> you can go in and out of that list
4) Filename completion:
CTRL+x, CTRL+f --> vim completes file names from current directory
5) Context aware completion
CTRL+x, CTRL+p or CTRL+n
CTRL+x, CTRL+l --> context aware line completion
6) Context aware Omni completion
CTRL+x, CTRL+o
7) Auto completion settings
:set complete
complete=.,w,b,u,t,i
. = look in current buffer
w = look in split window buffer (adjacent window)
b = Other buffers, this or other Vim tab
u = look in unloaded buffer, not active in any window
t = look in tags file
i = look in inserted file
[1] https://www.youtube.com/watch?v=aHm36-na4-4
[2] https://www.youtube.com/watch?v=3TX3kV3TICU
Vim Marks
---------
- Marks are points in file(s) that you can jump between.
- Three types of marks:
* Lowercase marks - for points within a buffer/file.
* Uppercase marks - for points across buffers/files.
* Numbered marks - unmodifiable marks stored in viminfo file
- Mark can be set only in Normal mode as: m<alphabet>
* <alphabet> can be uppper or lower case.
* If reused, latest mark overrides previous mark.
- Jumping to a mark:
`<alphabet> takes you to position where mark is set
'<alphabet> takes you to beginning of line where mark is set
- Full power of mark is in combining them with other composable commands.
Vim Scripting Language
-------
Syntax of the Language: Verb + Noun
---
d for delete
w for word
combine to be "delete word"
- Commands are repeatable and undoable
Verbs in Vim
---
d Delete
c Change (delete and enter insert mode)
> Indent
< Outdent
v Visually select
y Yank (copy)
Nouns in Vim -- Motions
---
w word (forward by a "word")
b back (back by a "word")
2j down 2 lines
Nouns in Vim -- Text Objects
---
iw "inner word" (works from anywhere in a word)
it "inner tag" (the contents of an HTML tag)
i" "inner quotes"
ip "inner paragraph"
as "a sentence"
Nouns in Vim -- Parameterized Text Objects
---
f, F "find" the next character
t, T "find" the next character
/,? Search up to next match
Tips for Mastering Vim Language
-----
- The 'dot' command should be guiding rule
- Visual mode means something is wrong
File Navigation
---
set path+=** " recursive search all files in subdirectories
" accepts wild chars. Works like fuzzy file search
" Use with wildmenu
:b *file " searches for files in buffer
Tag Jumping
----
command! MakeTags !ctags -R .
- Use ^] to jump to tag under cursor
- Use g^] for ambiguous tags
- Use ^t to jump back the tag stack
Autocomplete
----
^x^n for just this file
^x^f for file names
^x^] tags only
^x^d definition (in Vim's dictionary)
^x^i Keywords in included files
^x^l Lines (useful while writing Markdown)
^x^t Thesaurus (Needs an input file)
^x^k Dictionary
^x^s Spelling
^x^v Vim commands
^n for anything specified by complete option
^n and ^p go back and forth in suggestion list
Read docs at |ins-completion|
Intermediate Vim [5]
===
- Most commands are composed of an action and a motion
* Actions do something
* Motions move the cursor
- Uncommon Motions
* Text objects: w, s, p, b, B, etc
* Pneumonic?
+ "a word" -> aw
+ "inner block" -> ib
* Last position in change list: g; (g, is go back)
Copy & Paste
---
- Forget Copy/Paste and think Registers
- You now have 35 copy buffers/registers
"+ is X server (i.e system) copy/paste register, copy to here to copy out of Vim
:reg view content of registers
<C-r> in insert mode puts the register
Common Registers
---
"[1-9] history registers (or delete registers. It acts like a Queue)
"0 the yank register
"[a-z] are named registers
"[A-Z] samd as "[a-z] but append
Important Registers
---
"/ current search pattern
"- small delete
"= expression register
"_ the black hole register
Read-Only Registers
---
": last :command
". last inserted text
"% filename of current buffer
"# filename of alternate file (last file you edited)
:h registers
Undo and Redo Trees
---
Macros
---
- Recording a Macros starts by hitting q and then a letter, then sequence of
actions are recorded in that register.
- Hit q again to stop recording
- @<register-alphabet> will replay the macros actions
Windows
---
:h windows
- Lots of commands/help
Digraphs & Special Characters
---
- Digraphs: insert weird chars like: é,ä,etc
- In insert mode, ^k followed by one or two chars
:h digraphs
Templates
---
0r ~/path/to/template << this puts entire template in to this file
Snippets
---
:ab list of abbreviations
- Ultisnips and Snipmate: provide better snippets support and advance features
Notes from [6, 7]
---
Vim Concepts
---
- Modal Editing: Normal, Insert, Visual, Ex (or Command) modes
text-objects
---
w word
s sentence
p paragraph
t tags as in XML/HTML files
Motions: Getting from a to b
---
word = Any set of alphanumeric chars. Does not include commas, period etc
WORD - Chars separated by whitespace (or the like)
Basics: wWbBeE
w Forward to beginning of next word
W Forward to beginning of next WORD
b Backwards to beginning of next word
B Backwards to beginning of next WORD
e Forward to next end of word
E Forward to next end of WORD
0 Zeroth char of the line
^ First non-blank char of line
$ Last char of line
Slightly less basic: fFtT
All follow: [number]<verb><noun> syntax
[n]f<o> Forward until (nth) (o) Inclusive
[n]F<o> Backward until (nth) (o) Inclusive
[n]t<o> Forward until (nth) (o) Exclusive
[n]T<o> Backward until (nth) (o) Exclusive
Searching
/ Forward
? Backward
* Word under cursor - Forward search (bounded)
g* Word under cursor - Forward search (unbounded)
# Word under cursor - Backward search (bounded)
g# Word under cursor - Backward search (unbounded)
Motions
---
a all
i in
t until
f find
F Find backwards
Commands
---
d delete
c change (delete + insert mode)
y yank (copy)
v visually select
[number]{command}{text object | motion}
// TODO: Below commands are 'repeatable', meaning you can use DOT command to
// repeat what they do
diw
daw
ca[ change everything including []
ci] change everything inside []
ca << there's space after 'a'
va" visually select everything including "
Basic Insert Mode Operations
---
i Enter insert mode at cursor
I Enter insert mode at first non-blank char
s Delete char under cursor and enter insert mode
S Delete line and insert mode at beginning of same line
a Enter insert mode _after_ cursor
A Enter insert mode at end of line
o Enter insert mode on next line
O Enter insert mode above line
C Delete from cursor to end of line and enter insert mode
========== Ten Things You Don't Know About Vim [8] ====================
In .vimrc, do following
set nocompatible " not compatible with vi
filetype off
1) Help System. Very extensive
:help i_CTRL-V " tells what CTRL-V does in insert mode
:help :help " help about help
:help text-objects " help about concepts 'text-objects'
:help gU " help about key strokes
:help 'compatible' " options
:help submatch() " Vimscript functions
:help surround " help about plugins after installing
- Vim is Upside-Down vs GUI editor. In Vim, action comes first and object next
* [number]<verb><object> or [number]<verb><adverb><object>
* Ex: diw, yw, c$
- Visual Mode is somewhat similar to GUI
* Select object first and perform operation.
- Example. verb -> gU (change to Uppercase)
object -> char, word or line
2) Visual Block Mode
- CTRL-V puts vim into Visual Block mode
3) Text-Object
- aw or iw: around word, inner word
- All text-objects start with a (around) or i (inner)
- Around includes whitespace or things like that with object of your interest
- Inner is just the object
- word, sentence (ending with .), paragraph
4) Registers
- Put something in a register
"<alphabet><operation>
- Play something from a register
@<register>
5) Macros
- Record operations in to a register and repeatedly apply them
q start recording
a record in to register a
<do your operations>
q end recording
- But typing entire operation on a line and saving them into a register is more
convenient.
- Suppose, at 36.48 in [1], user has list of names. He want's to number them and
add ; at the end. They can type this on a line
A; I =b:i
) l~j:let b:i+=1
6) Shell output in to Vim directly
:4,8!<shell cmd>
- Result of shell command is pasted in lines 4,8
7) Advanced search & replace
set hlsearch " highlight search
set incsearch " find as you type
- Search history by q/
8) Regular Exp in search
- Vim's regex is very bad
- Instead use "magic v"
/\v<regex>
- Now regex behaves like Perl regex
9) Browse vimrc session from within vim and live update settings and save it
using :mksession <session-name>.vim
- Later on, load that session using :source <session-name>.vim
10) Open an HTTPS file using :edit https://github.com/umbs/dotfiles/blob/master/vimrc
11) You can edit a tar.gz file "live" from within Vim
=======================
Vim important links
[1] https://raw.github.com/shawncplus/vim-classes/master/expert-1.md
[2] https://raw.github.com/shawncplus/vim-classes/master/expert-2.md
.vim/
- after/
- autoload/
- bundle/ <<< when using Pathogen/Vundle etc
- colors/
- plugins/
- syntax/
============= Standard Plugins ==========
- AKA Global Plugins
- :help usr_05.txt (or :help standard-plugin)
- :help standar-plugin-list will list all of them
============= Autocommands =============
- Run commands automatically, as trigger to certain events such as file
save/write etc.
- :help usr_40.txt
- :help autocmd.txt
- Syntax
autocmd [group] {events} {file_pattern} [nested] {command}
- :help autocmd-events
// This is very extensive documentation. Read up for more info.
[1] https://www.youtube.com/watch?v=aHm36-na4-4
[2] https://www.youtube.com/watch?v=Qem8cpbJeYc
[3] https://www.youtube.com/watch?v=wlR5gYd6um0
[4] https://www.youtube.com/watch?v=XA2WjJbmmoM
[5] https://www.youtube.com/watch?v=v0W7JkzQAzA
[6] https://www.youtube.com/watch?v=5r6yzFEXajQ
[7] https://www.youtube.com/watch?v=Nim4_f5QUxA << basic Vim stuff
[8] Ten Things You Don't Know About Vim
https://www.youtube.com/watch?v=ZtfSm8nK1ag
[9]
================ Autocompletion in Vim ==============
by Stephen Belcher
- Works in 'Insert' mode
- Ctrl-X puts you in special mode and then a command
* Line completion (Ctrl-l)
* File keywords (Ctrl-n/Ctrl-p)
* Keywords in included files (Ctrl-i)
* Dictionary Words (Ctrl-k)
* Thesaurus Words (Ctrl-t)
* Filenames (Ctrl-f)
* Definitions/Macros (Ctrl-d) // C and C++ languages
* Vim command-line (Ctrl-v)
* User-defined functions (Ctrl-u). These functions are custom
auto-completion
* Omni Completion (Ctrl-o)
- Use :pclose after Omni Completion suggestions
- :help ins-complete
[1] https://www.youtube.com/watch?v=VMPtoBe8Yi0
================ Registers in Vim ==============
by Stephen Belcher
- Like clipboard, but there are many of them
- Persists between sessions
* Saved in .viminfo
* Read .viminfo, there'll be lot of useful info from previous Vim sessions
- Registers are (almost) always prefixed with "
* Only scripts or macros prefix them with @
- To write in to register 'r', do the following
"rdd // dd is for write to
- To read from register 'e', do the following
"ep // p is for paste
- Vim doc breaks registers in to 10 groups
"" This is unnamed register. Any delete/change/x/s operation will put that text
in to "" register and then to named register if provided.
"0 Default destination for yanked text unless another register is provided like
this "ay. Now, text is copied to "a register and unnamed register :)
If you yank to unnamed register (""y) then a copy is made in "0 register. Vim
probably wants to maintain two copies
"1 thru "9 default destination for any multi-line deleted text
"1 is written on every delete, unless another register is specified ("bd4j)
Before delete in to "1, current contents of "1 are moved to "2, "2 to "3 and so
on. Contents of "9 fall off.
"- deletions that are small (less than one line x, dw etc)
"a to "z. Only written to/read from when specifically asked.
"A thru "Z behave differently to "a thru "z
* "add overwrites the "a register
* "Add appends to "a register
* Useful when u want to append non-contiguous lines
:help registers
:let @x = "value" This will manually set contents of x register
[1] https://www.youtube.com/watch?v=sf1prtd_2E8
================ Macros in Vim ==============
by Stephen Belcher
Definition: Recorded series of commands that can easily be replayed again and
again
Recursive Macros: A macro which calls itself, so that the macro automatically
repeats until it's finished
Recording Macros:
* In NORMAL mode, press q and then any letter (example: qe). This is the
register where macro is recorded
* Follow with sequence of commands
* Return to NORMAL mode and press q to stop recording
Playing Macros:
* In NORMAL mode, press @, then the letter of register (example: @e)
* This replays macros stored in register "e
* Macro replay will stop when finished or hit an error
Viewing Macros:
* In NORMAL mode, just do "<register-alphabet> where macro is registered
* :registers
* :registers "e
Editing Macros:
- You can copy contents of a Macro to file like this: "ep
- Edit the contents
- Copy them back to the register as: "edaW
"e -> In to register e
d -> delete
a ->
W -> Big word
More Macro Concepts:
- Macros are transactional
* Entire actions of macros can be undone using 'u'
* What happens if macro opens new file. Can you undo it?
- Macros can be repeated by pressing @@ in NORMAL mode
* Think like @@ is special macro-specific register containing last executed
macro
- Like any regular macro, they have command to execute themselves
* Example: cw<REDACTED>^[n@e
* The last @e makes it recursive macro
- Recursive Macros continue until they hit error condition
Creating Recursive Macros
Option 1:
* Empty a macro register (qeq)
* Record macro, including execution of current macro (@e) at the end
Option 2:
* Record macro as usual (Example: qe ... q)
* Append a macro execution via special upper-case register. So, have @e in
your file, have cursor on @ and do this "E2x
* Since upper-case registers are append only, they'll append @e to the "E
register
Option 3:
* Record macro as usual (Example: qe ... q)
* In command-line mode, run following Vimscript command
:let @e = @e . '@e'
[1] https://www.youtube.com/watch?v=ejOF_N8Hd04
====================== Vim Help System ========================
:help
CTRL-] Jump to tag
CTRL-T, CTRL-O Jump back
CTRL-I Jump forward
:help jumps These are called jumplist
:helpgrep
- This does text search in entire help manual.
- Results are in quickfix window.
- Do :copen and open results in quickfix.
- :copen, cclose
:set wildmenu
- Now :help pyth<tab> will show various options statusline like bar.
$ vim -c "help help" " Launch Vim. Enters :help help
$ vim -c "help help | only | syntax enable"
Bash function
$ function vimhelp() { vim -u NONE -c "help help | only | syntax enable"; }
$ vimhelp python " this will open vim help for python
[1] https://www.youtube.com/watch?v=ghKR9rclP_8
====================== Vimscript (Talk by Prabir Shresta) ========================
:help variables
string Job
number Channel
float Blob
null
dictionary
list
funcref
let string = "help"
let number = 1
let float = 1.0
let list = [1, 2, "hello", "world"]
let dictionary = {"message": "hello world"}
- Multiline statement
let list = [
\ 1, 2,
\ "hello",
\ "world" ]
- Some quirks. Comments cannot be inline in a multiline code. Example of multi
line code
- Special Variables
let t = v:true
let f = v:false
let n = v:null
" All variables starting with v:<something> are read only.
- Variable Scoping
let g:global_var = "global variable accessible across all script"
let s:script_var = "script level variable accessible across the entire script/file"
let l:local_var = "local variable accessible only in the scope"
let b:buffer_var = "buffer scope"
let w:window_var = "window scope"
let t:tab_var = "tab scope"
Functions
---
- :help function
:help list
:help dict
APIs to interact with list and dict
add, remove, has_key, copy, keys, items()
:h write-plugin
[1]
https://www.youtube.com/watch?v=7fcJWwl1-SU&list=PLcTu2VkAIIWzD2kicFNHN2c35XQCeZdsv&index=18
====================== Vimscript (Steve Losh) ========================
- On Linux and Mac OSX, ~/.vimrc and Windows ~/_vimrc
- :echo $MYVIMRC, tells where this file is on any OS