Vim Learning 2 - The Vim Language, Basic Search, Substitution
Made by Mike_Zhang
Unfold Linux Topics $\triangledown$
0. Vim Cheat Sheet
Image source: Graphical vi-vim Cheat Sheet and Tutorial - www.viemu.com
1. The Vim Language
Vim, as a text editor tool, can be treated as a programming language. It has three language elements: verbs, modifiers and nouns;
1.1 Vim Language Elements
1.1.1 Verbs
Known as operator commands or operators
1.Powerless verbs
- Apply only to s single character
Key | Description |
---|---|
x |
Delete the char under the cursor to the right |
X |
Delete the char under the cursor to the left |
r |
Replace the char under the cursor with another character |
s |
Delete the char under the cursor and enter the Insert mode |
2.Powerful verbs
Key | Description |
---|---|
y |
yank (copy) |
c |
change |
d |
delete |
v |
visually select |
1.1.2 Modifiers
Modifiers are used right before the nouns, which influences the nouns.
Key | Description |
---|---|
i |
inner (inside) |
a |
around |
NUM |
number (e.g. 1 , 2 ) |
t |
searches for something and stops before it (until) |
f |
searches for that thing and lands on it (find) |
1.1.3 Nouns
Nouns can be objects you do something.
Key | Description |
---|---|
w,W |
Start of next word or WORD |
b, B |
Start of previous word or WORD (before) |
e, E |
End of word or WORD |
s |
Sentence |
p |
Paragraph |
t |
Tag (in context of HTML/XML) |
b |
Block (in context of programming) |
h,j,k,l |
Left, down, up, right |
$ |
end of line |
^, 0 |
start of line |
Expanded nouns:
Keys | Description |
---|---|
aw |
a (complete) word |
as |
a (complete) sentence |
ap |
a (complete) paragraph |
iw |
inner word |
is |
inner sentence |
ip |
inner paragraph |
1.2 Vim Language Sentences
Keys | Description |
---|---|
dw |
Delete the current word (delete word from cursor position to the end of the word) |
cis |
Change current sentence (change inside sentence) |
ci" |
Change a string inside quotes (change inside quote) |
c/hello |
Change until next occurrence of ‘hello’ (change search hello) |
ctY |
Change everything from here to the letter Y (change until Y) |
vap |
Visually select this paragraph (visual around paragraph) |
dd |
Delete a line |
dw |
Delete to the next word |
daw |
Delete the whole current word |
dt, |
Delete up until the next comma (,) on the current line |
de |
Delete to the end of the current word |
d2e |
Delete to the end of next word |
dj |
Delete down a line (current and one below) |
dt) |
Delete up until next closing parenthesis |
d/rails |
Delete up until the first search match for “rails” |
3wd2w |
Jump 3 words from cursor forward and delete next 2 words |
dvb |
Delete a word from cursor to beginning of a word, including the character under cursor: |
1.3 The Dot Command
.
(The Dot) in Normal mode: repeat the last native Vim command you’ve executed;
[Example]
To delete 5 words from the cursor forward:
5dw
, ordw
to delete one word, then press....
to call the dot command four times.
Use dd
to delete a line,
- Then use
4.
to delete 4 more lines
Note:
- Commands like j are called motions (or nouns)
- and they don’t affect the text itself.
- motions just moves the cursor, but doesn’t change text anyhow.
- These motions are not repeatable with the dot command.
2. Basic Search
All search operations are done in Normal mode.
/
: Search forward, then type the search pattern;<Esc>
: Cancel the search;<Enter>
: Execute the search;n
: Go to the next match;N
: Go to the previous match;ggn
: Go to the first match in the file;GN
: Go to the last match in the file;?
: Search backward, then type the search pattern, thenn
will go to the same direction (backward),N
will go to the opposite direction (forward).
2.1 Searching for the Current Word
Do search for words under the current cursor.
*
: Search forward for the exact word;#
: Search backward for the exact word;g*
: Search forward for the partial word;g#
: Search backward for the partial word.
2.2 Search History
Search history:
- Type
/
or?
, then use arrow up or down to navigate through the history.
Copy a word cursor is on to search:
- Press
/
; - Then press
<Ctrl-r>
, then<Ctrl-w>
to copy the word under the cursor to the search pattern. <Ctrl-o>
to go backward to the previous position;<Ctrl-i>
to go forward to the next position.
Do search on the last searched pattern:
- Press
/
and then pressEnter
, which will repeat the last search.
Do the count search:
5/pattern
: Jump to the 5th match of the pattern.5/*
: Jump to the 5th matchof the current word.
3. Substitution
3.1 Ranges
The default range of most Vim commands is the current line, which will affect only the current line.
However, we can control ranges of lines or characters to be affected by the command.
For example:
:s/bad/good/g
- changes all words bad to good in the current line.:6,11s/bad/good/g
- makes the same change, but in lines 6 to 11, including 6 and 11.:%s/bad/good/g
- makes the same change in entire file.- where
s
for substitute,g
for global,:
for running a command.
- where
If no range is specified, the current line is used as the default range.
Range | Description | Example |
---|---|---|
28 |
line 28 | :28s/bad/good/g |
1 |
first line | :1s/bad/good/g |
$ |
last line | :$s/bad/good/g |
% |
all lines in a file(same as 1 ,$ ) |
:%s/bad/good/g |
6,28 |
lines 6 to 28 | :6,28s/bad/good/g |
11,$ |
line 11 to end of the file | :11,$s/bad/good/g |
.,$ |
current line to end of the file | :.,$s/bad/good/g |
.+1,$ |
line after current line to end | :.+1,$s/bad/good/g |
.,+4 |
current to current+5 line, inclusive | :.,+4s/bad/good/g |
?a?,/b |
between patterns a and b , inclusive |
:?a?,/b?s/bad/good/g |
3.2 Search and Replace
text substitution command:
1 |
|
Everything enclosed between [ ]
in this command is optional.
[flag] options:
c
: to confirm each substitutiong
: to replace all occurrences in the linei
: ignore case for the patternI
: don’t ignore case for the pattern
After the first match:
- Press
n
: jump to the next match; - Press
N
: jump to the previous match;
3.2.1 Replacement in the whole file
To replace all occurrences of the string with new string in the whole file.
1 |
|
%s
: performed for all the lines.g
: performed over all occurrences in the line.- If
g
is not specified, only the first occurrence in the line will be replaced.
- If
3.2.2 Replacement within a single line
1 |
|
3.2.3 Replacement within a range of lines
To replace old with new on lines 15 to 30
1 |
|
3.2.4 Replacement inside visual selection
In Normal mode, use v
to visually select lines where you want to search and replace.
Then, type :
, which will automatically change to :'<,'>
.
1 |
|
3.2.5 Replace only the whole words
In order to search and replace only the whole words, to use \<
and \>
to specify that only whole word should be matched.
1 |
|
This sentence is short. -> This sentence was short.
3.2.7 Replace either string1 or string2 with a new word
1 |
|
\|
: logical OR (with escaped\
)
replace words pretty and good with awesome in this sentence:
That pretty girl did good on test.
to
That awesome girl did awesome on test.
3.2.8 Interactive search and replace
Using c
flag, we can run interactive search and replace in Vim.
1 |
|
options:
y
: yes - will replace the current highlighted word. After replacement of this word, Vim will highlight the next word that matched the search pattern.n
: no - will not replace the current highlighted word. However, it will highlight the next word that matched the search pattern.a
: all - will replace all of the remaining matches without asking you what to do.q
: quit - will simply quit the replacement process.l
: last - will replace only the current highlighted word and terminate the replacement process.^E
:Ctrl-e
, to scroll the screen up.^Y
:Ctrl-y
, to scroll the screen down.
References
Graphical vi-vim Cheat Sheet and Tutorial - www.viemu.com
“Interactive Vim tutorial,” www.openvim.com. https://www.openvim.com/ (accessed Jul. 07, 2023).
J. Ilic, Mastering Vim quickly : from WTF to OMG in no time. Berlin: Jovica Ilic, 2018.
Outro
尾巴
会继续更新Vim的学习。
最后,希望大家一起交流,分享,指出问题,谢谢!
原创文章,转载请标明出处
Made by Mike_Zhang