|
10 months ago | |
---|---|---|
Mastering Vim Quickly.pdf | 11 months ago | |
README.md | 10 months ago |
README.md
Personal notes
-
Chapter 3:
- vim +X file: ViM will open
file
at line numberX
(useful for debugging) - vim file +cmd: ViM will open
file
and executecmd
afterwards, e.g.:vim .ssh/known_hosts +"46d|x"
will delete line 46 from known_hosts
- vim +X file: ViM will open
-
Chapter 5:
- From within vim, one could open files by doing:
ESC+:e file_to_open
- From within vim, one could open files by doing:
-
Chapter 6:
-
More examples for reading files within ViM:
:r file_to_read
will insertfile_to_read
below the cursor in current buffer:0r file_to_read
will insertfile_to_read
before the first line:r!sed -n 2,8p file_to_read
will insert linest 2 to 8 fromfile_to_read
below the cursor:r !ls dir_to_list
will insert the output ofls dir_to_list
below the cursor
-
Navigation through words (if one wants to stop at delimiters and characters like ( ) . { } , $ use
w
, otherwise useW
:w
go to the start of next wordW
go to the start of next Worde
go to the end of next wordE
go to the end of next Wordb
go to the previous wordB
go to the previous Word
-
Scrolling pages:
Ctrl-d
: scroll down half a pageCtrl-u
: scroll up half a pageCtrl-f
: scroll down full pageCtrl-b
: scroll up full page
-
Jumping around the file:
gg
: go to the top of the fileG
: go to the bottom of the file{
: go to the beginning of the current paragraph}
: go to the end of the current paragraph%
: go to the matching pair of [], {}, ()50%
: go to the line at 50% of the file:NUM
: go to line NUM
-
Navigating inside the window
H
: move cursor to first line in current windowL
: move cursor to lowest line in current windowM
: move cursor to middle of the current window
-
Navigating in Insert mode (don't do it, do it in Normal mode)
Shift-Right-arrow
: go to the right, word by wordShift-Left-arrow
: go to the left, word by word
-
Basic search (done in Normal mode)
/+search_pattern+Enter
: will searchsearch_pattern
in the textn
: will search forwards for next occurrenceN
: will search backwards for previous occurrence
?+search_pattern+Enter
: will searchsearch_pattern
backwards in the textn
: will search backwards for previous occurrenceN
: will search forwards for next occurrence
*
: will search forwards for the exact word under the cursor#
: will search backwards for the exact word under the cursorg*
: will search forwards for the word (pattern) under the cursorg#
: will search backwards for the word (pattern) under the cursor/
or?
+ arrow up/down: go through previous search commands
-
File manager in Vim (netrw)
-
:Ex
: open current dir in current Vim windowZZ
: close it
-
:Ex directory
: opendirectory
in current Vim window -
:Sex
: open current dir in horizontal split window (same as:Ex
) -
:Vex
: open current dir in vertical split window -
:Tex
: open current dir in new tab -
:Lexplore
: open current dir in vertical split window (same as:Vex
) -
:40vs +Ex
: open current dir in vertical split window with width of 40 columns -
Vim can open directories as well to give you a list of all files and list all subdirs:
Enter
: open file under the cursor or enter dir under cursorD
: deletes file under the cursorR
: renames file under the cursorX
: executes file under the cursor%
: creates a new file in current dir
-
-
-
Chapter 7:
Skip chapter of configuration
-
Chapter 8:
u
: undo all recent changes on the current line. It accepts a prefix.Ctrl+r
: redo commandea[rlier] num
: will go backnum
changes. It accepts time periods, e.g.,earlier 10m
lat[er] num
: will go forwardnum
changes. It accepts time periodsg-
: move backward to previous branch. Useful in combination withu
g+
: move forward to next branch. Useful in combination withu
set undodir=~/.vim/undodir
: will save the hidden files for persistent undo under this, previously created, directory
-
Chapter 9:
-
Verbs:
-
x
: delete character under the cursor to the right -
X
: delete character under the cursor to the left -
r
: replace character under the cursor with another character -
s
: delete character under the cursor and enter the Insert mode -
y
: yank (copy) -
c
: change -
d
: delete -
v
: visually select
-
-
Modifiers:
i
: inner (inside)a
: a (around)NUM
: numbert
: searches for something and stops before it (search until)f
: searches for that thing and lands on it (find)/
: find a string (literal or regex)
-
Nouns:
-
w,W
: start of next word or WORD -
b,B
: start of previous word or WORD (start of word before) -
e,E
: end of word or WORD -
s
: sentence -
p
: paragraph -
t
: tag (in context HTML) -
b
: block (in context programming) -
h,j,k,l
: left, down, up, right -
$
: end of line -
^,0
: start of line -
aw
: a (complete) word -
as
: a (complete) sentence -
ap
: a (complete) paragraph -
iw
: inner word -
is
: inner sentence -
ip
: inner paragraph -
dw
: delete word from cursor position to the end of the word -
cis
: change current sentence -
ci"
: change a string inside quotes -
c/hello
: change until next occurrence ofhello
-
ctY
: change everything from here to the letterY
-
vap
: visually select this paragraph -
dd
: delete a line -
dw
: delete to the next word -
daw
: delete the whole current word -
dt,
: delete up until the next comma -
de
: delete to the end of current word -
d2e
: delete to the end of next word -
dj
: delete down a line -
dt)
: delete up until next closing paranthesis -
d/rails
: delete up until the first search match forrails
-
3wd2w
: jump 3 words from cursor forward and delete next 2 words -
dvb
: delete a word from cursor to beginnig of a word, including character under cursor
-
-
-
Chapter 10
-
Ranges:
-
:s/bad/good/g
: changes all wordsbad
togood
in the current line -
6,11s/bad/good/g
: same changes, but in lines 6 to 11 -
%s/bad/good/g
: same changes in entire file -
28
: line 28, e.g.,:28s/bad/good/g
-
1
: first line -
$
: last line -
%
: all lines in a file. Similar to1,$
-
6,28
: lines6
to28
-
11,$
: lines 11 to end of the file -
.,$
: current line to end of the file -
.+1,$
: line after current to end of the file -
.,+4
: current to current+5 -
?a?,/b
: between patternsa
andb
-
-
Search and replace:
s/\<is\>/was/g
: replace only whole wordis
bywas
s/\(pretty\|good\)/awesome/g
: replace eitherpretty
orgood
byawesome
%s/bad/good/gc
: run interactive search and replace (c for confirmation)
-
Search through multiple files:
vimgrep warning *.md
: will search for the stringwarning
in all markdown files of the current directory:cn
: jump to the next match:cN
: jump to the previous match:clist
: view all the files that contain the matched string:cc number
: jump to the specific match number, which you get from:clist
vimgrep error **/*.log
: search recursive
-
Match that string:
:match ErrorMsg /Error/
: highlight all matches of stringError
with the color defined byErrorMsg
-
The power of the global command:
:[range]g/pattern/cmd
: will look forpattern
in the file (by default, the whole file unless arange
is specified) and execute thecmd
command, such as:d
for delete:g/error/d
: will delete all ocurrences oferror
in the file:g!/important/d
: will delete all lines NOT containingimportant
:g/^\s*$/d
: will delete all blank lines ~g
: the global command ~^
: the start of line ~\s*
: zero or more space characters ~$
: the end of line ~d
: the Ex command delete
- Delete huge number of lines
:g/pattern/d_
: avoid the copy of lines to registers (put in the blackhole register_
). Very fast!
- Execute macros with the global command:
:g/vim/normal @a
: matches all lines with the stringvim
and executes the macro stored in registera
inNormal
mode
- Copy and move lines using
:g
::3,9t20
: copy lines 3 through 9 after line 20. No matter where your cursor is.:3,9m20
: move lines, instead of copy
- Reverse all the lines:
:g/^/m0
: will reverse the order of lines
-
Chapter 11: