A beginner's guide to VIM (Text editor).
Vim is a highly configurable text editor for efficiently creating and changing any kind of text. It is the ubiquitous text editor. This means you can find it Linux, Mac, and Windows.
The fascinating thing about VIM is you can do whatever you want without moving your hand from the keyboard.
VIM is an improved version of VI. VIM is a superset of VI. This means you can perform everything in VIM that you can perform in VI. But you can’t perform the VIM task in VI.
In Ubuntu Vim is the only VI-like editor installed by default. If you don’t you can install it.
sudo apt install vim
If you don’t know much about
apt command read this.
There you can find a GUI version of VIM. I won’t show that. If you are curious about it. Go ahead, feel free to explore it.
I hope you successfully installed VIM. Let’s open the terminal and create a file called
Then open that file using VIM.
Strange! what is this? I can’t do anything.
Wait, nothing is strange. This is the ground for VIM. You will play here.
There is the various mode in VIM. We will explore the below-listed modes.
- Normal mode
- Insert mode
- Command mode
- Visual mode
Now we see the normal or default mode in VIM. In our
test.txt file is empty. We want to insert something. So we need to change the normal mode to insert mode. To do that press
i (from insert). We will learn insert mode later in detail. Then at the bottom, you see
-- INSERT --. This means, insert mode is active. Let’s insert these 3 quotes below or whatever you want.
Your time is limited, so don't waste it living someone else's life. Don't be trapped by dogma – which is living with the results of other people's thinking.The way to get started is to quit talking and begin doing.If you set your goals ridiculously high and it's a failure, you will fail above everyone else's success.
Note: From any mode to back to normal mode, You need to press
esc we are back in normal mode. We want to navigate the text. To move the cursor:
- One character left —
- One character right —
- One line up —
- One line down —
- Start of the next word (forward) —
- End of the next word (forward) —
- Start of the previous word (backward) —
- Start of the line —
- End of the line —
- Next paragraph —
- Previous paragraph —
If we want to move the cursor 100 characters right. Should we need to press
l 100 times? No! We can do that using
Note: If we want to execute a command
n times, we can just prepend the number before that command like
Now we can navigate through the text. It’s time to manipulate text.
Earlier we knew about insert mode. We can activate insert mode in various ways. Insert something:
- Where the cursor stay —
- After where the cursor stay —
- Next blank line where the cursor stay —
- The previous blank line where the cursor stay —
We just know how to add things to our file. How can we delete or remove somethings from our file? To remove:
- One character —
- One word —
- Entire line —
- From cursor to the end of the line —
- From cursor to the start of the line —
Undo and Redo
Now we can remove characters, words, and lines. Say, we have deleted a word accidentally. We don’t want it. Now what? Don’t worry!
- Undo changes —
- Redo changes —
ctrl + r.
Now we want to copy some words or lines from what we have written in our file. We can copy marked text. To mark text we need to active visual mode.
- To active visual mode —
After activating visual mode, we can mark text using navigating commands. If we want to mark a word just press
w. You can use all of the navigating commands.
Now marking is done. It’s time to copy.
- To copy select text —
We can copy text without marking. To copy this way.
- To copy one word —
- To copy one line —
To cut text exactly the same as copying. Active visual mode and press navigating command with your requirement to mark text. Then
- To cut select text —
We can also cut text without marking as copying. To cut this way.
- To cut one word —
- To cut one line —
You can choose whatever way you want. But my personal recommendation is to activate visual mode when performing cut and copy. This will give you a representation of which thing you are copying or cutting. Otherwise, it is confusing to detect what is done with my command.
Pasting is very easy. There we do not need to work like cut and paste. Just navigate the cursor where we want to paste. Then, press
- To paste —
To search for something, we need to make sure we are in normal mode. If you are not in normal mode press
- To match from the start of the word —
- To match from the end of the word —
Go to the next match press
n and previous match press
We found the word we are looking for. Now we want to replace that word with another word. To replace:
- Only one matched word —
- All the matched word —
The previous two commands will change every matching word without asking any permission. But sometimes we don’t want to change every matching word. There we need to ask for permission before making any changes.
- To replace anything by asking for permission —
We can also replace a character without searching. Navigate cursor where want replace then press
r and the new character.
- Replace one character —
We have made so many changes. Now we want to save it.
- To save —
What if we want to save these changes in another file.
- To save as a new file —
We have added text, removed text, replaced text as well as save those changes. Now we want to quit VIM.
- To quit —
We can save and quit together.
- To save and quit together —
Suppose, We make changes to our file but we want to quit without saving those changes.
- To quit by avoiding changes —
- Never forget to use this. When you want to execute a command
ntimes, we can just prepend the number before that command like
- When you want to perform the last command again instead of use that command again just press
- When you are in insert mode, you have made a spelling mistake to remove that word instead of using
ctrl + w. or you made so many mistakes use
ctrl + u. This will get things done faster.
- To get help from VIM enter
- when you stuck just enter your terminal
Congrats! 👏 👏 👏. Now we know a lot about VIM 💪💪.
🙏 Please! don’t stop there by thinking we know lots.
🏃 Practice! 🏃 Practice!🏃 Practice!
There is a more efficient way to perform these tasks and many fascinating things to do using VIM. Go ahead and explore it. Below I gave some useful links it will help a lot.
If you think this is useful, share it with your friends. And if you think I represent something wrong please let me know to fix it together.
Thanks for your valuable time and your great effort to read my article.