Va Va Vim: Get Going With the Vim Text Editor
Tags: vim, beginners
Published: 2021-01-03 23:47:22

[Introduction](#what-is-vim?)
[Installation](#install-vim)
[Modes](#vim-modes)
[Commands](#vim-commands)
[Resources](#resources)

________________________________________________________

# What is Vim?

Vim (Vi IMproved) is an open-source text editor. It is a clone of [vi](https://www.thegeekdiary.com/basic-vi-commands-cheat-sheet/) 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)](https://en.wikipedia.org/wiki/Stevie_(text_editor)), 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](https://cmd.com/blog/vim-or-emacs-the-debate-is-over/), most of their users use vi/Vim as their primary file editor, with the breakdown corresponding with following statistics: 

* [vi/Vim](https://www.vim.org/): 93%
* [nano](https://www.howtogeek.com/howto/42980/the-beginners-guide-to-nano-the-linux-command-line-text-editor/): 5%
* [Emacs](https://www.redhat.com/sysadmin/beginners-guide-emacs): 2%

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 
```

__CentOS__
```
$ sudo yum install vim
```

__Fedora__ 
```
$ sudo dnf install vim
```

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

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


![Default page of Vim with version number](https://dev-to-uploads.s3.amazonaws.com/i/9za5r5oukh8rslghrst1.png)
___________________________________________________________

# 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](https://dev-to-uploads.s3.amazonaws.com/i/quhh0v3ycpmpmd3fzkyi.png)

__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 <kbd>F1</kbd> key or with the `:help` command.

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

* [Switching Between Modes](#switch-modes)
* [Navigation Keys](#navigation-keys)
* [Basic Commands for Normal Mode](#basic-normal-mode-commands)
* [Basic Commands for Insert Mode](#basic-insert-mode-commands)
* [Executing in Command Mode](#basic-command-mode-commands)
* [Search and Replace Command](#search-and-replace-command)

__________________________________________________________

### Switch Modes

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

To switch Vim to Insert mode, press <kbd>i</kbd>. 

Press <kbd>v</kbd> 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 |
| ----------  | ----------- |
| <kbd>h</kbd>| Left by one character        |
| <kbd>j</kbd>| Down by one row       |
| <kbd>k</kbd>| Up by one row         |
| <kbd>l</kbd>| Right by one character       |
| <kbd>w</kbd>| Move one word to the right        |
| <kbd>W</kbd>| Move one word to the right past punctuation |
| <kbd>b</kbd>| Move one word to the left         |
| <kbd>B</kbd>| Move one word to the left past punctuation |       
| <kbd>H</kbd>| Move to the top of the current screen |
| <kbd>M</kbd>| Move to the middle of the current screen   |
| <kbd>L</kbd>| 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 |
| ----------  | ----------- |
|<kbd>d</kbd> | Delete the text that is selected |
|<kbd>d</kbd><kbd>k</kbd>| Delete the current line and the line above it. <kbd>d</kbd> <kbd>↑</kbd> can also be used.
|<kbd>x</kbd> | Delete only the current character under the cursor|
|<kbd>c</kbd><kbd>c</kbd> | Delete the current line and change to Insert mode |
|<kbd>y</kbd>y | Copy the selected text |
|<kbd>p</kbd> | Paste the copied content |
|<kbd>u</kbd> | Undo the last edit. Press <kbd>u</kbd> again for further undos
|<kbd>U</kbd> | Undo changes on the current line
|<kbd>Ctrl</kbd><kbd>r</kbd> |Redo a change undone by <kbd>u</kbd>|

> You can combine keys to specify movement. For example,  <kbd>d</kbd><kbd>w</kbd> 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 
```

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
```
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
``` 

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.  


* [A Thorough Introduction to Vim](http://vimdoc.sourceforge.net/htmldoc/intro.html#vim-modes-intro)

* [Essential Vim Commands](https://catswhocode.com/vim-commands/)

* [Vim Mode Visualization](https://rawgit.com/darcyparker/1886716/raw/vimModeStateDiagram.svg)

* [Vim Tutorial](https://www.openvim.com/)

* [vi/Vim - Inspired Code Editors for Linux](https://www.tecmint.com/vi-vim-inspired-code-editors-for-linux/)

* [Vim Reference](https://learnbyexample.gitbooks.io/vim-reference/content/Introduction.html)