petrhurtak.com site logopetrhurtak.com

Vim basics

Vim is a text editor. It is in the top 5 of the most popular text editors. Some of its advantages are:

  • Runs inside terminal, so it is easy to use on remote servers with ssh.
  • Widely available on Linux distributions. If you ssh somewhere, Vim is the probably the most likely terminal text editor to be installed.

This article focuses on Vim basics and the use case where you are in a terminal in the ssh session and want to quickly edit/explore files within the terminal.

Install

sudo apt-get install vim

Open file in Vim

vim filepath

Basic usage

Unlike more traditional text editors, where if you start typing the letters will be inserted at the place of your cursor, Vim works little differently. In Vim you have modes and commands, the default mode is for text navigation or manipulation, and letters on your keyboard are shortcuts for things like cut, paste, mode change or save file. So if you want to select text, you enter Visual mode, if you want to insert text, you enter Insert mode and so on.

Modes & Commands

Mode nameKeyDescription
NormalEscFor navigation and manipulation of text. The default mode. You can usually get back to this mode with Esc.
InsertInsert or iFor inserting text.
VisualvText selections.
Visual LineVSelection of whole lines.
Command:For entering commands like search, save file or exit Vim.

Normal mode

CommandKeyAbbreviation
Undouundo
RedoCtrl + rredo
Cut (delete) lined and ddelete
Scroll up/down whole pageCtrl + f/bforward/back
Scroll up/down half pageCtrl + u/dup/down
Move to the start of the documentg and gbeggining
Move to the end of the documentG

Visual mode

CommandKeyAbbreviation
Copyyyank
Pasteppaste
Cut (delete) selectionddelete
Scroll up/down whole pageCtrl + f/bforward/back
Scroll up/down half pageCtrl + u/dup/down
Move to the start of the documentg and gbeggining
Move to the end of the documentG

Command mode

CommandKeyAbbreviation
Save filewwrite
Quit Vimqquit
Force quit Vim (drop unsaved changes)q!quit
Save file and quit Vimwqwrite + quit
Search/searched term
Search next itemnnext
Search previous itemN