Vim
Tags: linux, devops, cheatsheet, vim
Published: 2024-02-11 17:29:42

# Vi Improved, a programmer's text editor

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r92l2fwju1l2eyu8m0do.gif)

To open a file, type vim followed by the file name in terminal.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g1albchiyb56udne54nt.png)

```
vim file
```

# Modal Mindset

Unlike your typical text editor, Vim operates in different "modes." This might seem daunting at first, but mastering these modes unlocks its true potential.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/600mc1sdgmimoppvu63e.png)


- **Normal Mode**: Your command center. Move the cursor, navigate text, issue commands with keyboard shortcuts. Think of it as your spaceship's bridge.

- **Insert Mode**: Where you type like usual. Imagine yourself at the helm, crafting your code or text.

- **Visual Mode**: Highlight and manipulate text blocks like a Jedi wielding a lightsaber.

# Essential Navigation

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbnao8oyzh757czmhl5m.gif)
- **Cursor Movement**: h, j, k, l: Left, down, up, right (more ergonomic than arrows).

- **Jumping Around**: /pattern: Search for text and jump to its first occurrence. n: Repeat search in the same direction.

- **Going Places**: G: Jump to a specific line number. gg: Go to the very beginning. G$: Go to the very end.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1j5l2t8sdkjm4p05ga7.gif)

> Now, as we know **Vim** has multiple modes to start editing your file you need to be in _insert_ mode. By default when we open vim it's always in _normal_ mode.

To switch to **insert mode** press
```
i
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yj83toks36mq7s1rko94.png)

As we switched to insert mode now,we can type and it will be visible.

To go back to **normal mode** press
```
Esc
```

# Exit Vim 

Haha,no need to terminate or kill the process :V

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxbc1k6c5ma0tu541yz9.gif)

First thing first, make sure you are in normal mode.Then you need to press : followed by


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0z4u9jwmj4om5u53zj5v.png)

Save and exit
```
:wq
```

Exit without saving
```
:q!
```

Great it's not that hard to exit right. So, now try to open vim and write some random paragraph. Lazy you could use Lorem too.
<br>

## _Let's now see how to navigate._
<br>

Here, we have two paragraph in this file.Again **we need to be normal mode to navigate**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eq1p1br89vq7utd4z71h.png)

To jump to the last of the line
```
Shift + g
```

To jump to the top of the file
```
gg
```


# Find

To find something in file, make sure you are in normal mode.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3mc8i6utuarkf7md8gj.gif)

```
/word
```

For next (same word can be repeated multiple times in the file)
```
n
```

_That's not all._
<br>
We can even search in reverse direction (bottom to top)
```
?word
```
For next
```
n
```
---

## If the cursor is in the word you want to find.

_In forward direction(asterisk)_
```
* 
```
_In Reverse direction direction(hash)_
```
#
```

# Replace

Replacing a word in the file.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ic2frywzhdl1xotm0yuy.gif)

So, we need to substitute word1 with word2 globally
```
:%s/word1/word2/g
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/brzayb86uzyukx1u1gbx.png)

In the above image all the _string_ will be replaced with _character_

# Undo

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/unl59adznvkx23qlqeak.gif)

To undo the last changes made
```
:u
```

# Redo

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6to3tobn0qeczfcyznxp.gif)

To Redo the changes
```
Ctrl + r
```

---

# Editing


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4j9a5nnza8r1onak900o.gif)

In **Normal mode** to start editing in next line
```
o
```

To start editing on line above the cursor

```
Shift + o
```

To start from the start of the line _if cursor in b/w the line._
```
Shift + i
```

To start editing from the end of the line
```
Shift + a
```

---

# Delete

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utaeltcgackibho3nw5a.gif)

To delete a character where cursor is present
_small_
```
x
``` 

To delete a line
_twice_
```
dd
```

To replace a single character
_small_
```
r
```

Delete multiple lines
> In **normal mode** make sure the cursor is on the line from where you want to delete

_Let's say you want to delete 10 lines_
```
10 dd
```

To undo all the changes
```
:e!
```

# Copy/Paste

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lua369q9hr05mksasodg.gif)

To paste cut/copied text
```
p
```

To paste above the cursor
```
Shift + p
```

In copy/paste section we will see the visual mode sit back and focus.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r4pemikdx1t8twy0mm6b.gif)

Haha, now let's see...
<br>

To select whole line
```
Shift + v
```

Okay,but how to select what we need huh???<br>
To select what we need we need to go in **visual mode** how you ask
```
v
```
Yes, that's it _just press v_ and the best part is **using the arrow keys you can even select up,down,left,right**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b34ymdl6grqqt6gsn8xx.gif)

Now after selecting <br>

To copy_(yank)_ the selected text
```
y
```

_To paste press p and it will get pasted._

> Remember **dd** command it used to cut it will be your clipboard, it can be paste by hitting **p**

# Misc

## Line Number

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oeio309omqh3d0ohn1e8.gif)

To print the lines numbers in file
```
:set nu
```

To remove the line numbers
```
:set nonu
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bm9jet07egkvlbuf0kyi.jpg)

This serial line numbers you can enable an disable as you wish.

## Syntax Highlighting 

This is for Programmers to turn on the syntax highlighting feature, it's possible.

Turn on color coding
```
:syntax on
```

Turn off color coding
```
:syntax off
```

## Jump to given line

Turn on your line numbers, suppose you want to jump to 20th line
```
:20
```

## Working with Multiple files


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sb7md1wymapt9mx1cg1r.png)

To read multiple files at once
```
vim -o file1 file2
```

To switch between files opened
Press **twice**
```
Ctrl + w
```

## Compare two files


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fm2c9fhsqwx3drznhfgr.png)

Side by side difference with highlighted changes
```
vim -d file1 file2
```

Time to practice and flex


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vgyrc7w15ocpi2nrgd3p.gif)