Vim

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

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

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

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

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.

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

To jump to the top of the file

gg

Find

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

Image description

/word

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

n

That’s not all.

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

So, we need to substitute word1 with word2 globally

:%s/word1/word2/g

Image description

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

Undo

Image description

To undo the last changes made

:u

Redo

Image description

To Redo the changes

Ctrl + r

Editing

Image description

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

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

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

Haha, now let’s see…

To select whole line

Shift + v

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

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

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

To remove the line numbers

:set nonu

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

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

To read multiple files at once

vim -o file1 file2

To switch between files opened
Press twice

Ctrl + w

Compare two files

Image description

Side by side difference with highlighted changes

vim -d file1 file2

Time to practice and flex

Image description

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
7-side-hustle-ideas-for-developers

7 Side Hustle Ideas for Developers

Next Post
unraveling-the-layers-of-system-requirements-in-software-architecture

Unraveling the Layers of System Requirements in Software Architecture

Related Posts