Sometimes even the simplest of tasks like navigating through a single file can be optimized. Vim offers several methods of navigation within a file, which can adapt to the contents of the file and how it is organized. Some of these methods are obvious, while others are more complex.
Mostly, the files we are editing are well structured. If our files are text, then this structure can be in the form of paragraphs, sentences, and words, or at other times code with functions, blocks, and code lines.
Vim supports jumping around the file, according to the structure in the file, and has key bindings that make it easy to go to the exact place in the file where you want to go.
Moving within a text file
You are working on a normal text file and in the middle of a sentence you realize that you have forgotten to make the first letter in the paragraphs uppercase. You could of course, use the arrow-keys or the h/j/k/l navigation keys to move to the beginning of the paragraph to correct this. It would, however, be faster to just press the following in normal mode:
{
You are now placed at the beginning of the paragraph—or in the empty line just above it (if one exists). Now, you go into normal mode by pressing the Esc key and then use the movement command { to go to the beginning of the paragraph. The same can be applied if you want to go back to the end of the paragraph—you simply have to use the counterpart of {, namely:}
Again, Vim helps you move faster and offers you a pair of commands to move to the beginning and end of the current sentence. The commands are as follows:
( : Move to the beginning of the sentence.
) : Move to the end of the sentence.
w : Move to the beginning of the next word.
b : Move to the beginning of the previous word.
e : Move to the end of the word.
These commands can be combined such that if you want to go to the end of the next word you simply press:
we
Moving in a Code file
Compared to text files, code does not have any paragraphs or sentences to navigate through. It does, however, frequently contain a lot of structures and blocks, each of which has a very specific contextual meaning within the code. An example could be the simple code block:
If( a == b) {
print "a and b are the same";
}
Here the line with print is within the context of the if block surrounding it.
Because Vim is the favorite editor of many programmers, it offers a lot of movement commands to use when you are working with code. Common for all of them is that the parts of the code you want to jump between need to have a contextual connection to each other.
A simple example could be a construction like the #if-#else-#endif construction from the C programming language. Here we have a beginning (#if), an end (#endif) and a midpoint (#else).
So if you are in the line with #if and press the following command:
%
you will go to the #else part. Pressing it again, will take you to the #endif, and yet another execution of the command will get you back to the #if.
Vim does not know all programming language constructs, but by default it knows most of the contextual constructs of the C programming language. Besides this, it knows the normal block construction methods from most programming languages—the use of parentheses and brackets (for example, '{' is block start and '}' is block end).
So for Vim to find the beginning of the function, it simply has to find the outermost bracket pair and then go to the
opening bracket.
function myExample() {
...many lines of code...
/* cursor is placed at the beginning of this line */
...many lines of code...
}
In the above example, the % command would take us to the closing bracket and pressing it again would take us to the opening bracket. But what if the cursor was actually placed inside another pair of brackets? In that case, the % command would only move the cursor to them, and not the beginning of the function.
Again Vim has some handy commands for you:
[[ and ][ : Move backwards/forward to the next section beginning (e.g., start of a function)
[] and ]] : Move backwards/forward to the next section end (e.g., end of a function)
Executing these commands multiple times in a row takes you to the beginning/end of the next/previous section, and therefore gives you a convenient way of cycling through the functions in a file.
[{ : Move to the beginning of the block
]} : Move to the end of the block
]} : Move to the end of the block
[/ : Move to the beginning of the comment block
]/ : Move to the end of the comment block
]/ : Move to the end of the comment block
Comments