-
Notifications
You must be signed in to change notification settings - Fork 0
/
vim_notes.txt
437 lines (326 loc) · 15.1 KB
/
vim_notes.txt
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
Ctrl-w s for horizontal splitting
Ctrl-w v for vertical splitting
Ctrl-w q to close window
Ctrl-w w to switch windows
1. Vim Line Navigation
* k -- navigate upwards
* j -- navigate downwards
* l -- navigate right side
* h -- navigate left side
* 0 -- go to the starting of the current line.
* ^ -- go to the first non blank character of the line.
* $ -- go to the end of the current line.
* g_ -- go to the last non blank character of the line.
2. Vim Screen Navigation
* H -- Go to the first line of current screen.
* M -- Go to the middle line of current screen.
* L -- Go to the last line of current screen.
* ctrl+f -- Jump forward one full screen.
* ctrl+b -- Jump backwards one full screen
* ctrl+d -- Jump forward (down) a half screen
* ctrl+u -- Jump back (up) one half screen
3. Vim Special Navigation
* N% -- Go to the Nth percentage line of the file.
* NG -- Go to the Nth line of the file.
* G -- Go to the end of the file.
* `" -- Go to the position where you were in NORMAL MODE while last closing the file.
* `^ -- Go to the position where you were in INSERT MODE while last closing the file.
* g -- Go to the beginning of the file.
4. Vim Word Navigation
* e -- go to the end of the current word.
* E -- go to the end of the current WORD.
* b -- go to the previous (before) word.
* B -- go to the previous (before) WORD.
* w -- go to the next word.
* W -- go to the next WORD.
WORD -- WORD consists of a sequence of non-blank characters, separated with white space.
word -- word consists of a sequence of letters, digits and underscores.
5. Vim Paragraph Navigation
* { -- Go to the beginning of the current paragraph. By pressing { again and again move to the previous paragraph beginnings.
* } -- Go to the end of the current paragraph. By pressing } again and again move to the next paragraph end, and again.
6. Vim Search Navigation
* /i -- Search for a pattern which will you take you to the next occurrence of it.
* ?i -- Search for a pattern which will you take you to the previous occurrence of it.
* * - Go to the next occurrence of the current word under the cursor.
* # - Go to the previous occurrence of the current word under the cursor.
7. Vim Code Navigation
* % -- Go to the matching braces, or parenthesis inside code.
To reselect what you just had selected use 'gv'
To redo the very last operation just hit "."
To go a page forward, or a page back use control-f and control-b.
To go a paragraph forward, or a paragraph back, use { and } (in normal mode of course)
To go a sentence forward, or a sentence back, use ( and ) (in normal mode of course)
To go to the next instance of a char you use f-char
ge (Backword Word Movement at the end of word Ex. ge,3ge )
Moving to the Start or End of a Line
$(End Of Line Ex. $,2$)
^(Start Of Line Ex. ^,3^)
Searching Along a Single Line
f<Charcter> (Searching Charcter Forward Ex. fa, 2fv)
F<Charcter> (Searching Charcter Backword Ex. Fg,2Ft)
t<Charcter> (Search till Forward )
T<Charcter> (Search till Backword)
; repeat last f, F, t, T
, repeat last f, F, t, T in the opposite direction
Current line information:
CTRL-G (Where I am in the File)
g CTRL-G (Count col, line, words and bytes)
Jumping:
CTRL-O Go to [count] Older cursor position in jump list
(not a motion command).
CTRL-I or <Tab> Go to [count] newer cursor position in jump list
(not a motion command).
:ju[mps] Print the jump list (not a motion command).
Scrolling Up and Down
z<Enter> (screen line on the top)
88z<Enter> positions line 88 at the top.
zt (Leaves the cursor where it is.)
z- (scrolls line to the end of the screen)
zb (Leaves the cursor where it is)
z. (Center of the screen)
zz (Leaves the cursor where it is .)
Deleting
x delete character under the cursor (short for "dl")
X delete character before the cursor (short for "dh")
dw (Delete Word Ex. dw,3dw,d3w,3d2w,d$,d^,df> )
dd (Delete Line Ex. dd,3dd)
D (Delete up to end of line. )(short for "d$")
diw delete word under the cursor (excluding white space)
daw delete word under the cursor (including white space)
dG delete until the end of the file
dgg delete until the start of the file
Arthemetic
CTRL-A Increment number (123, 0177, 0x1f,-98)
CTRL-X Decrement number
Changing Text
C stands for c$ (change to end of the line)
s stands for cl (change one character)
S stands for cc (change a whole line)
Joining Lines
J (Join Lines to One. Ex J,3J)
gJ (Join Lines without Spaces)
Replacing Charcter
r<Charcter> (Replace Charater Under Cursour. Ex. ru,5ra,3r<Enter> )
R<Charcter>
Changing Case
~ (Change Case of Character Ex. ~,12~,~fq)
U (Make the text Uppercase)
u (Make the text Lowercase)
g~motion (It does not depend on tildeop)
g~~ or g~g~ (Changes case of whole line)
gUmotion (All uppercase)
gUU (Changes to uppercase for whole line)
gUw (Changes to uppercase for word)
guw (Changes to lowercase for word)
> shift right (indent)
< shift left (unindent)
v When used after an operator, before the motion command: Force
the operator to work characterwise, also when the motion is
linewise. If the motion was linewise, it will become
|exclusive|.
If the motion already was characterwise, toggle
inclusive/exclusive. This can be used to make an exclusive
motion inclusive and an inclusive motion exclusive.
Example:
dj
deletes two lines
dvj
deletes from the cursor position until the character below the cursor
Yank/delete/select inside blocks
yi' (yank inside single quotes; works for ", (, [, { as well)
ya' (yank around single quotes)
yib (yank inside paranthesis block)
yiB (yank inside curly brace block)
See also :help text-objects
Replace a word with yanked text (type the quotes!): "_cw^R"
Replace word under cursor with yanked text: :%s/^R^W/^R"/gc
Replace WORD under cursor with yanked text: :%s/^R^A/^R"/gc
There is mru.vim, which adds the :MRU command.
vim -p <files> opens files in individual tabs.
:tabnew <file> opens file in a new tab.
:tabclose close current tab
:tabclose {i} close i-th tab
:tabonly close all other tabs (show only the current tab)
:tab split copy the current window to a new tab of its own
:tabs list all tabs including their displayed windows
:tabm 0 move current tab to first
:tabm move current tab to last
:tabm {i} move current tab to position i+1
:tabn go to next tab
:tabp go to previous tab
:tabfirst go to first tab
:tablast go to last tab
gt go to next tab
gT go to previous tab
{i}gt go to tab in position i
:tabdo run a command on all of the tabs at once
Crtl-Alt-PgDown/Up go to previous/next tab
= aligns code
yank, delete, paste-from-yank:
Copy into a named register "ayw, do your delete and then paste from the named register "ap.
Alternatively, you could delete into the black hole register "_d.
Show all registers:
:reg[isters]
Search-and-replace within visually selected code only:
:s/\%V<a>/<b>/g
Using vim is like talking to your editor in "verb modifier object" sentences, turned into acronyms
* learn some verbs: v (visual), c (change), d (delete), y (yank/copy). these are the most important. there are others
* learn some modifiers: i (inside), a (around), t (till..finds a character), f (find..like till except including the char), / (search..find a string/regex)
* learn some text objects: w (word), s (sentence) p (paragraph) b (block/parentheses), t (tag, works for html/xml) there are others
To move efficiently in vim, don't try to do anything by pressing keys many times, instead speak to the editor in sentences
* delete the current word: diw (delete inside word)
* change current sentence: cis (change inside sentence)
* change a string inside quotes: ci" (change inside quote)
* change until next occurrence of "foo": c/foo (change search foo)
* change everything from here to the letter X: ctX
* visually select this paragraph: vap (visual around paragraph)
If you run :shell or just :sh while you're in the editor, Vim will place you in an interactive shell. You can run whatever commands you want, and resume your Vim session by exiting the shell.
Vim also allows you to execute a command directly from the editor, without needing to drop to a shell, by using bang (!) followed by the command to be run.:! wc %
Reload .vimrc:
:source $MYVIMRC
CTRL-W T Move the current window to a new tab page.
Create tags for keyword searching between files:
ctags -R * at trunk
ctrl-] to go to object definition/declaration
ctrl-t to go back up a level
You can use Ctrl-C instead of ESC to switch out of insert mode.
select blocks (columns) CTRL + q
indent selected text >
unindent selected text <
convert selected text to uppercase U
convert selected text to lowercase u
invert case of selected text ~
start recording a macro qX (X = key to assign macro to)
stop recording a macro q
playback macro @X (X = key macro was assigned to,
prefix with with n to repeat n times)
replay previously played macro * @@
bookmark current place in file mX (X = key to assign bookmark to)
jump to bookmark `X (X = key bookmark was assigned to
` = back tick/tilde key)
show all bookmarks :marks
navigating split screens CTRL + w + j = move down a screen
CRTL + w + k = move up a screen
CRTL + w + h = move left a screen
CRTL + w + l = move right a screen
close all other split screens :only
gi switches to insertion mode placing the cursor at the same location it was previously.
If you're editing a line that is wrapped because it's wider than your buffer, you can move up/down using gk and gj.
^wi: Jump to the tag under the cursor by splitting the window.
cib/ciB: Change the text inside the current set of parenthesis () or braces {}, respectively.
:w <filename> saves buffer as <filename> and leaves the old buffer open.
:sav <filename> saves buffer as <filename> and opens a new buffer with <filename>.
Any Vim search pattern can include the special sequence \v (very magic), and this will make every following character except a-zA-Z0-9 and '_' have special meaning. Using \V has the opposite effect: all characters have their literal meaning and must be preceded by \ to activate their special meaning. So if you have forgotten which characters have special meaning in Vim, start the pattern with \v or \V to switch them all on or off.
Check the current value of a setting:
:set <setting>?
Re-wrap comments to find in specified textwidth:
gq<motion>
Re-wrap inside current block:
gqip
Replace tabs with spaces in current document:
:set expandtab [if not already set]
:retab
Update help after adding items to $HOME/.vim/doc:
:helptags $HOME/.vim/doc
Auto-indenting doesn't work but textwidth and formatoptions are set correctly:
Make sure paste isn't set.
I append to start of current line (first non-whitespace character)
A append to end of current line
Avoid escape key:
ctrl-[ or ctrl-C
Numpad doesn't work over PuTTY:
PuTTY Configuration -> Terminal -> Features -> Disable application keypad mode
Capitalize the first letter of every word:
:%s/\v<(.)(\w*)/\u\1\L\2/g
Scroll through search history:
q/ and then use j and k to scroll
Scroll through command history:
q: and then use j and k to scroll
Open command history while entering a command:
ctrl-f
Previous command:
ctrl-p
Next command:
ctrl-n
To enter special characters (digraphs):
Ctrl-k a: Ä
Use :digraphs to see the full list.
Or enter by decimal value: ^Vnnn
Ctrl-v 223 ß
Note that special characters seem to show up weird in Cygwin vim.
To substitute a newline in a search and replace command:
\r
Despite that \n matches a newline in the search side.
http://stackoverflow.com/questions/71323/how-to-replace-a-character-for-a-newline-in-vim
http://stackoverflow.com/questions/71417/why-is-r-a-newline-for-vim
Replace ^M (linebreak) with an actual linebreak:
:%s/<Ctrl-V><Ctrl-M>/\r/g
Search or replace a range instead of entire file:
:.,$s/search/replace/gc
Actually, % is just shorthand for 1,$. Also, the initial period is assumed and can be left off.
See :h range for more information.
Resize split windows:
Ctrl-w =: Distribute space equally to all windows (perfect if terminal has been resized)
Ctrl-w r: Rotate windows (also ctrl-w ctrl-r)
Ctrl-w -/+: Decrease/increase vertical size of current window
Ctrl-w </>: Decrease/increase horizontal size of current window
# Ignore whitespace while diffing:
set diffopt+=iwhite
# Ignore case/capitalization:
set diffopt+=icase
Open tagged definition in a new tab:
https://stackoverflow.com/questions/539231/how-to-use-multiple-tabs-when-tagging-to-a-function-in-vim
https://stackoverflow.com/questions/563616/vim-and-ctags-tips-and-tricks
<C-w><C-]><C-w>T
Initialize cscope:
$ cscope -Rb [in terminal at project root]
:cs add cscope.out [in vim]
Reset cscope:
https://stackoverflow.com/questions/2744010/update-cscope-db-from-vim
First, you need to update cscope database.
:!cscope -Rbq
Then, reinit the database from vim.
:cs reset
Find function callers:
:cs f c <func_name>
Find what filetype is loaded:
https://stackoverflow.com/questions/2779379/find-what-filetype-is-loaded-in-vim
:set filetype?
(or just :se ft)
Keyboard navigation of command line/mode/window (either works):
https://stackoverflow.com/questions/6920943/navigating-in-vims-command-mode
https://stackoverflow.com/questions/2075569/how-can-i-move-around-in-the-vim-command-line
q:
: Ctrl-f
Syntastic commands:
:SyntasticCheck (manually check right now)
:Errors (open the |location-list| window)
:lclose (close it)
CtrlP commands:
:CtrlP (invoke CtrlP in find file mode)
<F5> to purge the cache for the current directory to get new files, remove deleted files and apply new ignore options.
<c-f> and <c-b> to cycle between modes.
<c-d> to switch to filename search instead of full path.
<c-r> to switch to regexp mode.
<c-j>, <c-k> or the arrow keys to navigate the result list.
<c-t> or <c-v>, <c-x> to open the selected entry in a new tab or in a new split.
<c-n>, <c-p> to select the next/previous string in the prompt's history.
<c-y> to create a new file and its parent directories.
<c-z> to mark/unmark multiple files and <c-o> to open them.
Repeat a command only on lines within a given range matching a pattern (similiar to :s//):
:[range]g[lobal]/{pattern}/[cmd]
Built-in autocomplete:
Ctrl-n or Ctrl-p
Whole-line completion:
Ctrl-x Ctrl-l
Return to directory navigation after opening a file:
https://stackoverflow.com/questions/9160499/go-to-back-directory-browsing-after-opening-file-in-vim
:Rex[plore]
Enable spellchecking:
:setlocal spell spelllang=en_us
Move to next bad word:
]s
Find suggestions for a bad word:
z=
Non-greedy regex search (stop at first match found):
Instead of .* use .\{-}.