A brief introduction to vim 3/9
Marc Aug 10 Updated on Sep 10, 2018
Table of contents
A table of contents of this series can be found here.
Yesterday I wrote a Post called A brief introduction to vim 2/9, explaining basic and advanced cursor navigation and scrolling in vim.
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
SHIFT+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 1/9 we talked about saving and quitting vim. We actually did this by using a mode called command mode.
To enter the command 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 comand 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!
Here is the fourth part of this series: A brief introduction to vim 4/9