Chapter 2: Opening Files, Finding Content
Open Files
Ha, now you have learned a way to open files in Vim, although this way is not the most common, but it is the most direct, especially when your code includes a certain file, the following introduces two other commonly used open methods.
Open files in Vim
:e <filename>open the file which named<filename>, if the file is not exists, it will create a new one:Exopen the directory tree in Vim, press-to enter the parent directory and press Enter to open the corresponding file
Find
Find content in document
*find the word under the cursor and jump to the next#find the word under the cursor and jump to the previous/<search>find the specified string forward?<search>find the specified string backwardncontinue to find the nextNcontinue to find the previous
Note: n and N are directional. If you previously searched with *, n will continue to search toward the end of the document, and N toward the beginning of the document; conversely, if you search with #, n points to the beginning and N points to the end of the document.
Search in line
f<X>FindsXon a line toward the end of the line and positions the cursor over the characterX.`t<X>FindsXon a line toward the end of the line and positions the cursor over the character beforeX.F<X>FindsXon a line toward the beginning of the line and positions the cursor over the characterX.T<X>FindsXon a line toward the beginning of the line and positions the cursor over the character afterX.;Find the next character in the current direction,Find the previous character in the current direction
There are several "Vim" words in the current document, you can try to use
*and#to find and feel the directionality ofnandN.And there are several "n" characters in the "Note" above, you can try to find them in the line and feel the directionality of
;and,.
Matching
In Vim, you can use % to match ( and ), [ and ], { and }. When the cursor is on one of the symbols, press %, the cursor will jump to the other symbol that matches it.
Press
%on the()[]{}characters in the following text to see the effect. Press%repeatedly to jump back and forth between each matching pair.
const func = (win, doc) => {
const SEVEN = ((1 + 2) * (3 + 4) * (5 - 6)) / 7;
const YU = [1, 2, [[3, 4], 5, 6], 7];
if (true) {
return SEVEN;
} else {
return YU;
}
};Next Chapter introduces editing. First, meet the Vim buffer: a buffer is text loaded into memory for editing. It is not a window or merely a file history. One buffer can appear in several windows, and closing a window does not necessarily remove its buffer.
Now you should have two files in your buffer, you can use
:buffersor:lscommand to view, see the buffer list, probably like this:
:ls
1 #h "chapter01.md" line 66
2 %a "chapter02.md" line 1
Press ENTER or type command to continueThen, you can use the following commands to switch between buffers:
:bnopen the next file in the buffer:bpopen the previous file in the buffer:b<N>open the Nth file in the bufferUse
:bdelete <N>(or:bd <N>) to remove a buffer from the list. Vim will refuse if it contains unsaved changes; do not immediately add!until you have decided whether those changes matter.Of course, you can also use the
:Excommand, selectchapter03.mdand open it, enter Chapter 3.