Skip to main content

VIM: Faster navigation in a file

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 beginning of the comment block
]/ : Move to the end of the comment block

Comments

Popular posts from this blog

Howto Install BSNL wll clarity phone in Ubuntu

Hello linixians, Failed to access internet through your bsnl clarity phone.... Here is an easy way 1. Just download this executable..... bsnlclarity and save it to your home folder. 2. Connect your phone to the system with the usb cable. 3. Now at terminal type $ sudo ./bsnlclarity You will be asked for your username and password for accessing the internet enter it ..... now start surffing Ctrl+C to stop

Blank (or black) screen after boot process in Ubuntu

You may encounter a blank screen after boot in ubuntu. This will be due to some update of certain packages. Follow these steps to correct the problem. 1. Select recovery mode from the boot menu. 2. Select login as root from the menu in recovery mode. 3. Type this at the prompt # sudo apt-get remove xorg-driver-fglrx # sudo dpkg-reconfigure -phigh xserver-xorg 4. Exit # exit 5. Now select Resume normal boot from the menu. Every thing should be OK by now. Please comment about your experience.

gtalx: Howto Gtalk in ubuntu ( google talk )

Did you fail to use " gtalk in ubuntu " ... Now you can chat and talk to your gmail buddies in ubuntu... 1. check for gtk2.0 and qt4 $ sudo apt-get install libgtk2.0-dev $ sudo apt-get install libqt4-dev libogg-dev libtheora-dev $ sudo apt-get install libsdl-dev libavcodec-dev libswscale-dev $ sudo apt-get install libexpat-dev libraw1394-dev libvorbis-dev $ sudo apt-get install libgsm1-dev  libspeexdsp-dev libmediastreamer0-dev libortp-dev 2. Remove 'pulse audio sound server' Open synaptic package manager and remove pulse audio 3. "Download gtalx" from here Dowload it here 4. Extract the file.... $ tar -zxvf 0.0.4.tar.gz 5. Get into the directory.... $ cd 0.0.4 and then... $ sudo chmod u+x make $ ./make $ sudo ./make install 6. Enjoy..... Please comment about your installation(whether it was a success or not....) Take gtalx from Applications > Internet > gtalx Enter your gmail login information..and click connect...