DEV Community

Cover image for Vim
Code Drug
Code Drug

Posted on

Vim

Vi Improved, a programmer's text editor

Image description

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

Image description

vim file
Enter fullscreen mode Exit fullscreen mode

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

  • 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

  • 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

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
Enter fullscreen mode Exit fullscreen mode

Image description

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

To go back to normal mode press

Esc
Enter fullscreen mode Exit fullscreen mode

Exit Vim

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

Image description

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

Image description

Save and exit

:wq
Enter fullscreen mode Exit fullscreen mode

Exit without saving

:q!
Enter fullscreen mode Exit fullscreen mode

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.

Let's now see how to navigate.

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

Image description

To jump to the last of the line

Shift + g
Enter fullscreen mode Exit fullscreen mode

To jump to the top of the file

gg
Enter fullscreen mode Exit fullscreen mode

Find

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

Image description

/word
Enter fullscreen mode Exit fullscreen mode

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

n
Enter fullscreen mode Exit fullscreen mode

That's not all.


We can even search in reverse direction (bottom to top)

?word
Enter fullscreen mode Exit fullscreen mode

For next

n
Enter fullscreen mode Exit fullscreen mode

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

In forward direction(asterisk)

* 
Enter fullscreen mode Exit fullscreen mode

In Reverse direction direction(hash)

#
Enter fullscreen mode Exit fullscreen mode

Replace

Replacing a word in the file.

Image description

So, we need to substitute word1 with word2 globally

:%s/word1/word2/g
Enter fullscreen mode Exit fullscreen mode

Image description

In the above image all the string will be replaced with character

Undo

Image description

To undo the last changes made

:u
Enter fullscreen mode Exit fullscreen mode

Redo

Image description

To Redo the changes

Ctrl + r
Enter fullscreen mode Exit fullscreen mode

Editing

Image description

In Normal mode to start editing in next line

o
Enter fullscreen mode Exit fullscreen mode

To start editing on line above the cursor

Shift + o
Enter fullscreen mode Exit fullscreen mode

To start from the start of the line if cursor in b/w the line.

Shift + i
Enter fullscreen mode Exit fullscreen mode

To start editing from the end of the line

Shift + a
Enter fullscreen mode Exit fullscreen mode

Delete

Image description

To delete a character where cursor is present
small

x
Enter fullscreen mode Exit fullscreen mode

To delete a line
twice

dd
Enter fullscreen mode Exit fullscreen mode

To replace a single character
small

r
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To undo all the changes

:e!
Enter fullscreen mode Exit fullscreen mode

Copy/Paste

Image description

To paste cut/copied text

p
Enter fullscreen mode Exit fullscreen mode

To paste above the cursor

Shift + p
Enter fullscreen mode Exit fullscreen mode

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

Image description

Haha, now let's see...

To select whole line

Shift + v
Enter fullscreen mode Exit fullscreen mode

Okay,but how to select what we need huh???

To select what we need we need to go in visual mode how you ask

v
Enter fullscreen mode Exit fullscreen mode

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

Now after selecting

To copy_(yank)_ the selected text

y
Enter fullscreen mode Exit fullscreen mode

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

To print the lines numbers in file

:set nu
Enter fullscreen mode Exit fullscreen mode

To remove the line numbers

:set nonu
Enter fullscreen mode Exit fullscreen mode

Image description

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
Enter fullscreen mode Exit fullscreen mode

Turn off color coding

:syntax off
Enter fullscreen mode Exit fullscreen mode

Jump to given line

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

:20
Enter fullscreen mode Exit fullscreen mode

Working with Multiple files

Image description

To read multiple files at once

vim -o file1 file2
Enter fullscreen mode Exit fullscreen mode

To switch between files opened
Press twice

Ctrl + w
Enter fullscreen mode Exit fullscreen mode

Compare two files

Image description

Side by side difference with highlighted changes

vim -d file1 file2
Enter fullscreen mode Exit fullscreen mode

Time to practice and flex

Image description

Top comments (0)