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

How to install Skype on Ubuntu 10.04 Lucid Lynx

UPDATE: Skype has now been uploaded to the "partner" official Ubuntu 10.04 Lucid Lynx repository. See Install Skype in Ubuntu 10.04 Lucid Lynx from official partner repository Command Line way Get the package: For 32-bit $ wget http://www.skype.com/go/getskype-linux-beta-ubuntu-32 For 64-bit $ wget http://www.skype.com/go/getskype-linux-beta-ubuntu-64 You will need to install the following libraries. $ sudo apt-get install libqt4-dbus libqt4-network libqt4-xml libasound2 Now install skype(the file u just downloaded, may be named intrepid-ibex) $ sudo dpkg -i skype-XXXXXXXXXXXX.deb If you still get some dependency error then just type the command below $ sudo apt-get -f install this will install the dependencies and the skype. Non - Terminal Download the setup 32 -bit 64 -bit You will these packages: libqt4-dbus libqt4-network libqt4-xml . Goto Synaptic manager and install them. Now Launch the Skype install .deb file (just double click it) and follow the on screen prompts....

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... ...

How to install Skype on Ubuntu 9.10 Karmic Koala

The two methods  below doesn't work now. Please see http://blog.dipinkrishna.info/2009/11/how-to-download-and-install-skype-for_3060.html for a successfull installation of skype on ubuntu. I have provided two methods. Please do comment which one is successful. Follow these steps to install skype in ubuntu 9.10 (64 bit).         First add:             deb http://packages.medibuntu.org/ karmic free non-free         to your source.list Now open a terminal and type: $ sudo wget http://www.medibuntu.org/sources.list.d/hardy.list -O /etc/apt/sources.list.d/medibuntu.list Then do an update: $ sudo apt-get update $ sudo apt-get --yes -q --allow-unauthenticated install medibuntu-keyring $ sudo apt-get -q update And finally install Skype. $ sudo apt-get install skype Ready ... now start skype with $ skype OR Download the setup 32 -bit 64 -bit Launch the Skype i...