Trying out Vim
Tags: webdev, vim, tools
Published: 2020-06-28 15:58:24

---
title: Trying out Vim
published: true
description: Some basic Vim commands to get up and running with Vim.
tags: #productivity #webdev #vim #tools
//cover_image: https://camo.githubusercontent.com/537458336e8bb85785642c057a60a21d5dba5164/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f732f35327571667463637068756a32726c2f76696d2d73686f7274637574732d6461726b5f31323830783830302e706e67
---

[Vim](https://www.vim.org/) (**V**i **IM**proved) is a highly configurable text editor built to make creating and changing any kind of text very efficiently. It is included as "vi" with most UNIX systems and with Apple OS X.I attempted to learn Vim quite a few times before but could not cope up with it. But recently I thought I should at least know very basic usage of Vim. So I started learning the absolute basics. Here I am putting all the basic commands and usage of Vim for any absolute beginner like me. This can be helpful to get started with Vim I think. But there is a lot of things to learn about this handy tool that will take much time and practice. One will get proficient in using Vim only by using it consistently.

Vim has two basic modes:
1. One is `INSERT` mode, in which you write text as if in normal text editor.
2. Another is `NORMAL` mode which provides you efficient ways to navigate and manipulate text. This is also called `Command` mode cause we can do various vim commands on this mode.

To change between modes, use `esc` for normal mode and `i` for insert mode.

## VIM Commands

|Command        | Action                        |
|---------------|-------------------------------|
|:e *filename*  | Open *filename* for editon    |
|:w             | Save file                     |
|:q             | Exit vim                      |
|:q!            | Quit without saving           |
|:x             | Save and Exit                 |
|:sav *name*    | Save current file as *name*   |
|.              | Repeat last change made in `NORMAL` mode |

---

## Cursor Movements
* `h` to move cursor left (&#8592;)
* `l` to move cursor right (&#8594;)
* `k` to move cursor up (&#8593;)
* `j` to move cursor down (&#8595;)
* `b` moves to the beginning of the word
* `e` moves to the end of the word
* `w` moves to the beginning of the next word

## Number Based Movements
Using a `number` before each command can execute that command that many times. e.g `3w` will move to the 3rd next word.

## Insert Text Repeatedly
To insert the same text multiple times use `<number>i<text>esc`. e.g `3<i>go<esc>` will write `gogogo`.

## Find a Character
To find and move to the next (or previous) occurrence of a character, use `f` and `F`, e.g. `fo` finds next 'o'. You can combine f with a number. e.g. you can find 3rd occurrence of 'q' with `3fq`.

## Go to Matching Parenthesis
In text that is structured with parentheses or brackets, `(` or `{` or `[`, use `%` to jump to the matching parenthesis or bracket.

## Start/End of Line
To reach the beginning of a line, use `0`. For end of line use `$`.

## Find Word Under Cursor
Find the next occurrence of the word under cursor with `*`, and the previous with `#`.

## Go to Line
`gg` takes you to the beginning of the file; `G` to the end. To jump directly to a specific line, give its line number along with `G`. e.g `5G` will take you to the fifth line of the file.

## Search for Text
Searching text is a vital part of any text editor. In Vim, press `/`, and give the text to search for. The search can be repeated for next and previous occurrences with `n` and `N` respectively.For advanced use cases, it's possible to use regexps that help to find text of particular form.

## Insert New Line
To insert text into a new line **after** the current line use `o` and to insert a new line **before** the current line use `O`.

After new line is created, the editor is set to `insert` mode.

## Removing a Character
`x` and `X` delete the character under the cursor and to the left of the cursor, respectively.

Also adding a `number` before `x` of `X` can perform the action that many times. e.g `5x` will remove the **next** five characters **including** the character under the cursor and `5X` will remove the **previous** five characters **excluding** the character under cursor.

## Replace a Character
User `r` to replace only one character under the cursor.

## Deleting
`d` is the delete command. It can be combined it with movements, e.g. `dw` deletes the characters on the right side of the cursor upto the beginning of the next word. `de` deletes all the characters of the word on the right side of the cursor **including** the character under cursor. `db` will delete the previous word if the cursor is under the first letter of a word or else it will delete the characters left to the cursor upto the beginning of the word.

It also **copies** the content, so that you can **paste** it with `p` to another location.

`dd` will delete the whole line.

## Repeat Command
To repeat the previous command, just use `.` (period).

## Replace Mode
Use `R` to enter `REPLACE` mode. In this mode characters under the cursor can be replaced.

## Visual Mode
Use `v` to enter the `VISUAL` mode and `V` to enter `VISUAL LINE` mode. In this mode the text can be selected by the movement keys before deciding what to do with it.

Selected text can be **deleted/cut** using `d` or **coppied** using `y`. It can be **pasted** after the cursor using `p` or before the cursor using `P`.