Skip to main content

VIM: Search the Current File

Even though your file might not be that long, it can still be a pain to find something you are looking for. Vim has several ways to help you find what you are looking for.

Find next occurrence of a word

Search for a word by executing the following command in normal mode:
?someWord
The command searches backwards in the file for the first occurrence of the word after the question mark. If you are at the end of the file, this is the perfect way to search for a word, but if you were at the beginning of the file it would make more sense to search forward in the file. This is done by exchanging the question mark for a slash:
/someWord
The word might be in the file several times, and maybe the first place you found wasn't the place you were looking for. No worries, you simply need to press n to go to the next occurrence of the word in the direction of the search. If you would rather change direction, then simply press N instead, and it will instead find the preceding occurrence of the word.
If you want to do the same search again, simply use ?? or // instead of writing the entire word again.


Search for word under cursor

If you are already near one occurrence of the word you are looking for, but it is just not the right one, or maybe you want to look through all places where a certain word is used, and the word is already written, why use extra key presses on writing the word again? Vim has just the right commands for you. Place the cursor on or just in front of the word you want to search for and press either one of the following two keys in normal mode:
#
*
The first one searches for the previous occurrence of the word under the cursor, and the second one searches for the next occurrence of the word. Pressing the key multiple times jumps to the next/previous occurrence of the word, over and over again. This makes it really fast to jump through all occurrences of the word.
Maybe your word isn't actually a complete word, but just a part of a word. Vim also has a command for this. Simply press the following key combination in normal mode:
g#
g*
Now Vim does not just jump to the next occurrence of the word, but also to any occurrences where the word is part of another word. For example, placing the cursor on the word "foo" and pressing g# will make Vim jump to the next "foo", in both "foobar" and "food."

Comments

Mischa Molhoek said…
you can also search with specific word boundries:

in normal mode:
/phone\>

will search for phone with word boundry at the end so it will match telephone but fail phonebooth

in normal mode:
/\<phone

will match phonebooth but fail telephone

and in normal mode:

/\<phone\>

will match phone only :)

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