A brief introduction to Vim (Part 3)
In Vim you can search by pressing
/ followed by a phrase and pressing
ENTER. If an occurrence is found, the cursor will move to that position in the file.
If the phrase you want to search for contains special characters, you have to escape these characters with a backslash (
Example: You want to search the occurrences of
/* in your code. For that you can type in the following:
/\/\*. This might look odd at first but it does make sense.
If you want to tab through the found occurrences of the phrase you searched for, there are two noteworthy commands:
njumps to the next occurrence
Njumps to the previous occurrence
Vim can be customized with commands. These commands can either be executed in runtime or be set in a config file. By default, Vim searches in your home directory for a config file called
Executing commands in runtime
In my first Post called A brief introduction to Vim (Part 1) we talked about saving and quitting Vim. We actually did this by using a mode called command-line mode.
To enter the command-line mode, we have to be in normal mode first and then press
:. (Simply press
ESC if you want to go back to the normal mode from the command mode)
Now we can enter the command we want to execute followed by pressing
There are commands which can only be executed in runtime because it doesn’t make sense to set them in a config file:
We already know a few of these commands:
wsaves the performed changes on the currently opened file
qquits Vim if there are no unsaved changes present in the currently opened file
q!quits Vim, discarding unsaved changes in the currently opened file
But there are a few more noteworthy:
e [filename]opens filename relative to the directory, you opened Vim in
w [filename]you can save the currently opened file to a new or an already existing but different file by specifying a filename
There are a lot of possibilities to configure Vim via commands so I will mention the most noteworthy here:
syntax onenables syntax highlighting
set numberenables line numbers
set relativenumberenables relative line numbers to where the cursor is currently at (can be used together with
set ignorecasedisables case sensitive searching
set smartcaseenables case sensitive searching if the phrase you are searching for has uppercase letters in it
- tabs can be configured with commands too
hlsearchhighlights the phrase your searched for
nohlsearchremoves the highlighting of the searched phrase
Indenting and unindenting
Speaking of tabs.. It is possible to indent one line by pressing
>>. To unindent, simply press
<<. The cursor does not have to be at the beginning of the line.
Ask me questions
I will be happy to answer your questions in the comments section below. Also let me know if you have any tips or ideas for me to improve my post!