Vi Improved, a programmer's text editor
To open a file, type vim followed by the file name in terminal.
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.
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
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.
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
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
First thing first, make sure you are in normal mode.Then you need to press : followed by
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.
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.
/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.
So, we need to substitute word1 with word2 globally
:%s/word1/word2/g
In the above image all the string will be replaced with character
Undo
To undo the last changes made
:u
Redo
To Redo the changes
Ctrl + r
Editing
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
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
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.
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
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
To print the lines numbers in file
:set nu
To remove the line numbers
:set nonu
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
To read multiple files at once
vim -o file1 file2
To switch between files opened
Press twice
Ctrl + w
Compare two files
Side by side difference with highlighted changes
vim -d file1 file2
Time to practice and flex
Top comments (0)