DEV Community

Cover image for Va Va Vim: Get Going With the Vim Text Editor
J Edwards
J Edwards

Posted on

Va Va Vim: Get Going With the Vim Text Editor

Introduction
Installation
Modes
Commands
Resources


What is Vim?

Vim (Vi IMproved) is an open-source text editor. It is a clone of vi and is designed for use both from a command-line interface (CLI) and as a standalone application in a graphical user interface (GUI).

The vi editor, the first screen-oriented text editor developed by Billy Joy in 1976, was created for Unix environments and built for text manipulation.

Based on his work for Stevie (ST Editor for VI Enthusiasts), Bram Moolenaar developed and publicly released Vim (v1.14) in 1991.

Initially, the name Vim was an acronym for "Vi IMitation", but that changed to "'Vi IMproved" in 1993.

Vim supports more features than vi, is very configurable, and enables efficient text editing.

Why Use Vim?

Vim and similar text editors are incredibly powerful. By learning it, you gain the ability to navigate and modify files expertly and efficiently.

According to cmd.com, most of their users use vi/Vim as their primary file editor, with the breakdown corresponding with following statistics:

In Vim, all edits are saved, and you can load past revisions easily. If you make a mistake, you can look through the list of previous versions, and select the version that works best for you.

Vim's features include:

  • an extensive plugin system
  • integration with many tools
  • support for hundreds of programming languages and file formats
  • a powerful search and replace

Install Vim

To install Vim, enter the following command:

Ubuntu / Debian

$ sudo apt install vim 
Enter fullscreen mode Exit fullscreen mode

CentOS

$ sudo yum install vim
Enter fullscreen mode Exit fullscreen mode

Fedora

$ sudo dnf install vim
Enter fullscreen mode Exit fullscreen mode

After Vim is installed, you can open the editor by typing the following command in the terminal:

vi
Enter fullscreen mode Exit fullscreen mode

This command opens the default page that displays the Vim version number.

Default page of Vim with version number


Vim Modes

Vim is a modal text editor. Based on the vi editor, Vim inherits the key bindings of vi, but also adds more functionality and extensibility that is missing from the original vi text editor.

In Vim, the mode that the editor is in determines whether the alphanumeric keys will input characters or facilitate navigation throughout the document.

Vim has several different editing modes, but its basic modes are:

Normal mode

  • This mode is used for editor commands. This is usually designated as the default mode.

Visual mode

  • Similar to Normal mode, this mode is used to highlight areas of text. Normal commands run on the highlighted area, which can be used to move or edit a selection.

Insert mode

  • This mode is similar to editing in most editors. In Insert mode, you can input characters and modify files.

Switch between Insert mode and Visual mode

Cmdline mode

  • This is the command-line mode and supports a single line input at the bottom of the Vim window. Commands beginning with : activate this mode.

Vim Commands

All Vim commands can be carried out using the keyboard - although some can be executed through the command line, while others can be used as navigation keys. This allows you to keep your fingers on the keyboard and your eyes on the screen.

To learn more about the various commands and actions that can be used in Vim, use the help.txt file. It can be accessed in Vim with the F1 key or with the :help command.

In the meantime, here are a few basic commands to get started with:


Switch Modes

By default, Vim starts in Normal mode. Normal mode can be accessed from other modes by pressing Esc.

To switch Vim to Insert mode, press i.

Press v to enter Visual mode.

Navigation Keys

There are multiple ways to move around in an open file. In addition to using the cursor keys, you can use the keys in the table below to navigate through a file in Vim.

Key Description
h Left by one character
j Down by one row
k Up by one row
l Right by one character
w Move one word to the right
W Move one word to the right past punctuation
b Move one word to the left
B Move one word to the left past punctuation
H Move to the top of the current screen
M Move to the middle of the current screen
L Move to the bottom of the current screen

Row movement can be prefixed by a number to move several lines at a time.

Basic Normal Mode Commands

Command Description
d Delete the text that is selected
dk Delete the current line and the line above it. d can also be used.
x Delete only the current character under the cursor
cc Delete the current line and change to Insert mode
yy Copy the selected text
p Paste the copied content
u Undo the last edit. Press u again for further undos
U Undo changes on the current line
Ctrlr Redo a change undone by u

You can combine keys to specify movement. For example, dw deletes the first word on the right side of the cursor. It also copies the content, so that you can paste it to another location.

Basic Insert Mode Commands

Command Description
a Insert characters to the right of the cursor
A Append characters to the current line
i Insert characters to the left of cursor
I Insert characters at the beginning of the current line
o Add a new line after the current line
O Insert a new line above the current line

Basic Command Mode Commands

Command Description
:q! Quit but do not save changes
:w! Force save the file
:wq Save the file and exit Vim
:w Write out the file to save changes
:w <file_name> Write the file to the specified file
:r <file_name> Import a file into the current file

You can prefix commands with a number to couple navigation. For instance, :10 r <file_name> imports a file into the current file after line 10.

Search and Replace Command

To perform a basic search and replace function, use the following format in an editable mode:

%s/<search_term>/<replace_term>/g 
Enter fullscreen mode Exit fullscreen mode

This command replaces all instances of the search_term with the replace_term.

To breakdown the command:

  • : enters command mode
  • % means across all lines
  • s means to substitute

Optionally, you can use:

  • /foo is the regex (regular expression) to find things to replace
  • /bar/ is the regex to replace things with
  • /g means global, otherwise, the command will only execute once per line
  • c to confirm each replacement

Vim has a number of other methods that you can read about in the help documentation, by entering the command :h or :help.

For instance, if given the text file:

Hello world
You are my sunshine
In the world
Enter fullscreen mode Exit fullscreen mode

When the command :%s/world/planet/g/c is executed, all instances of world are changed to planet. The new file will look like this:

Hello planet
You are my sunshine
In the planet
Enter fullscreen mode Exit fullscreen mode

The c at the end of the command, prompts a confirmation message, and you will be asked if you want to proceed with the replacement.


Resources

Vim is a great, powerful, and very learnable tool. Becoming comfortable with the basics of Vim (or similar text editors) helps in becoming a more confident developer.

The are several amazing resources that can help you get better-acquainted and familiar with Vim. Check out the following links to learn more about using Vim as a text editor, as well as some of the available plugins that can be used with it.

Top comments (2)

Collapse
 
moopet profile image
Ben Sinclair

I'm pretty sure that image with the arrows has the arrows the wrong way around. It's saying that you hit esc in normal mode to enter visual mode. I mean, you could make mappings to do that, but it's not how it works out the box!

Collapse
 
epsi profile image
E.R. Nurwijayadi

I have blog full of ViM.