Vim – Basics for safe use

Falke Stephan

28/01/2021

Why Vim? While some are happy to leave Vim, many swear by the power and efficiency of the text editor. The initial learning curve is worthwhile if you frequently work with text files in a Linux environment, and even the initially unintuitive interface becomes more manageable with a little practice.

Modes

One of the biggest hurdles when getting started with Vim is the fact that the editor distinguishes between different modes:

  • Normal mode – entering commands
  • Insert mode – Normal text input
  • Visual mode – changing entire text blocks

Vim starts in normal mode. With Eurovision Song Contest You always return to normal mode and are then ready to enter commands. Commands that begin with a : start, are displayed in the lower left corner when writing and can be TAB are automatically completed. Some of the most important are:

command Explanation
:w Save
:q Close
:wq Save and close
:q! Force close (without saving)
:e [file path] Open file
u Undo
Ctrl+r Redo

In the Insert mode one usually ends up with i. The file can then be edited as with any other text editor.

In the visual mode You can select and edit text blocks. There are three selection types, shown in the image below. If the visual mode is activated with v activated, then you select all characters between the start and end points. With Shift+v only whole rows are selected. Ctrl+v Allows you to select rectangular areas (in the image, width 1). This function can be used, for example, to insert or remove comments.

Text editing

In insert mode, as usual, you can delete and Backspace to delete characters, although normal and visual modes often offer more efficient methods.

From normal mode, a line with y, y copied and then with p A line containing d, d, which also causes it to end up in a buffer and p can be pasted elsewhere. The functionality is therefore more like cutting.

In visual mode, depending on the selection type, several lines, specific sections or selected columns can be d, y and p cut, copied or pasted. Cutting multiple lines is a particularly common use case. From normal mode, you can use Shift+v To select multiple complete lines, make your selection using the arrow keys and remove the lines with d. You can then continue at another location with p be inserted.

Visual mode can also be used to remove comment characters from multiple lines at once. To do this, select the comment characters with Ctrl+v and delete them with dInserting comment characters into multiple lines is only slightly more complicated. To do this, the first column of the relevant lines is Ctrl+v selected. You can then enter Shift+i As a C developer, for example, you now enter // followed by Eurovision Song Contest which comments out each of the lines.

navigation

As is usual in almost all editors, you can move the cursor with the arrow keys and Page Up and Page Down Navigate page by page. The mouse can also be used, but a setting must be changed (see the Settings section).

In addition, Vim offers the possibility to jump to a specific line. For example, line 70 is :70 Also, a position with m, s be saved for later use , s to return there again. s can be replaced by any letter to store different positions. G, ; takes you to the last point you were working on. Especially after searching, it's often useful to jump back to where you were last working.

You can search with /search term. With n you jump to the next hit and with Shift+n to the previous one. Regex expressions like /search.*iff are also allowed. Instead of entering the search term manually, you can also use * search for the term under the cursor and then also with n and Shift+n navigate.

To replace specific terms, you can :%s/foo/bar/g use. :%s searches the entire file (in contrast to :s only in the current row or selected range). foo is the search term and is replaced by bear replaced. With the option G all hits in a line are replaced. If you add a c , Vim will ask after each match whether the position should be replaced, allowing it to selectively replace matches. If you are looking for specific patterns in URLs, escaping the slashes in them quickly becomes annoying. Instead, you can simply change the search separator. For example, :%s#foo#bar#g equally valid.

Searching and replacing with regex expressions can save a lot of work, which we have already shown in this blog post with some examples.

Settings

You can edit or create the ~/.vimrc file to customize Vim. The following options are worth considering:

colorscheme torte "Color schemes can be tested with :colorscheme set tabstop=4 "Tabs are displayed four columns wide set shiftwidth=4 "One indentation level is four columns wide set shiftround "Indentation is limited to multiples of shiftwidth set expandtab "Tabs are replaced by spaces set autoindent "Indentation depth is maintained on new lines set smartindent "Indentation depth is affected by code syntax set number "Line numbers are displayed set linebreak "Use automatic line breaks set scrolloff=10 "Scroll before the cursor is at the edge of the screen set noerrorbells "Turn off error bells on invalid input set wildmode=longest,list "Tab completion setting set ignorecase "Ignore case when searching, ... set smartcase "... except when the search term contains uppercase letters "Enable mouse control. With this option, Shift must be held when selecting text if the text is to be copied outside of Vim should. set mouse=a

Window management

It's often useful to view multiple files at once. Vim offers two ways to do this: tabs and panes.

A new tab can be opened with :table Optionally, the command also accepts a file name for a file to be opened in the new tab. You can then switch between tabs with G, t be changed.

However, window dividers are usually more useful. Ctrl+w, v and Ctrl+w, s divide the window vertically or horizontally. Between the divisions you can use Ctrl+w and a corresponding arrow key.

Tips & Tricks

With :! You can execute commands like outside of Vim. For example, you can :!git status to query the status of your Git repository directly from Vim. If you add a dot, the output of the external command is inserted into the open Vim document, i.e. :.!date writes the current date to the file.

It often happens that you edit a file without root privileges and only realize when saving that you don't have the permission to do so. :w !sudo tee % you can still save the file, provided you have the sudo password.

More information and tricks can be found in Vim's help :help, on https://vim.fandom.com/ and, of course, on StackOverflow. Vim can do much more than what's listed here. This includes, for example, macros, a wealth of efficient keyboard shortcuts, correcting file indentation, and removing trailing whitespace.

Do you have any questions about using Vim? We're happy to help.


Written by Falke Stephan

Falke Stephan has been working as a software developer at MEDtech Ingenieur since 2020. His responsibilities include the development of medical devices based on embedded Linux.


More articles

  • 09/09/2025
  • General, Software

In previous blog posts, I have introduced two essential components of a simple and universally applicable software architecture: events with dispatchers, listeners, and data pools. These already allow for many simple use cases ...

Read more
  • 12/11/2024
  • General, Software, Testing, Tools

In safety-critical software projects, software quality is paramount. Especially for Class C software, which must be certified according to strict standards such as IEC 62304 (medical technology), it is essential that ...

Read more
  • 08/08/2024
  • General, Electrical Stimulation, Software, Testing

Nowadays, apps in the healthcare sector are very important. Apps that can read and process data from medical sensors are particularly useful. Flutter is an open-source framework from Google that is excellent for ...

Read more
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.