A brief introduction to Vim (Part 3)

 
(Workflow & Setup)
 

Searching in Vim, commands in runtime and config file.


About

A table of contents of this series can be found here.
A summary of Vim can be found here.
Here is the second (previous) part of this series: A brief introduction to Vim (Part 2)
Here is the fourth (next) part of this series: A brief introduction to Vim (Part 4)

Searching

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:

  • n jumps to the next occurrence
  • N jumps to the previous occurrence

Commands

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

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

Runtime-only commands

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:

  • w saves the performed changes on the currently opened file
  • q quits 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:

  • edit [filename] and 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

More commands

There are a lot of possibilities to configure Vim via commands so I will mention the most noteworthy here:

  • syntax on enables syntax highlighting
  • set number enables line numbers
  • set relativenumber enables relative line numbers to where the cursor is currently at (can be used together with set number)
  • set ignorecase disables case sensitive searching
  • set smartcase enables case sensitive searching if the phrase you are searching for has uppercase letters in it
  • tabs can be configured with commands too
  • hlsearch highlights the phrase your searched for
  • nohlsearch removes 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!


Write a response

Your email address will not be published. Required fields are marked *