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.

VIM logo.
credit: Wikipedia.

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 test.txt.

touch text.txt

Then open that file using VIM.

vim text.txt

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.

  1. Normal 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.

Navigate

After pressing esc we are back in normal mode. We want to navigate the text. To move the cursor:

  • One character left — h.

If we want to move the cursor 100 characters right. Should we need to press l 100 times? No! We can do that using 100l.

Note: If we want to execute a command n times, we can just prepend the number before that command like 100h.

Insert

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 — i.

Remove

We just know how to add things to our file. How can we delete or remove somethings from our file? To remove:

  • One character — x.

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 — u.

Copy

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 — v.

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 — y.

We can copy text without marking. To copy this way.

  • To copy one word — yw.

Cut

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 — d.

We can also cut text without marking as copying. To cut this way.

  • To cut one word — dw.

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.

Paste

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 p.

  • To paste — p.

Search

To search for something, we need to make sure we are in normal mode. If you are not in normal mode press esc.

  • To match from the start of the word — /pattern .

Go to the next match press n and previous match press N.

Replace

We found the word we are looking for. Now we want to replace that word with another word. To replace:

  • Only one matched word — :%s/search/replace.

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 — :%s/search/replace/gc.

We can also replace a character without searching. Navigate cursor where want replace then press r and the new character.

  • Replace one character — r.

Save

We have made so many changes. Now we want to save it.

  • To save — :w .

What if we want to save these changes in another file.

  • To save as a new file — :saveas filename

Quit

We have added text, removed text, replaced text as well as save those changes. Now we want to quit VIM.

  • To quit — :q .

We can save and quit together.

  • To save and quit together — :wq.

Suppose, We make changes to our file but we want to quit without saving those changes.

  • To quit by avoiding changes — :q!.

Tips

  • Never forget to use this. When you want to execute a command n times, we can just prepend the number before that command like 100h.

Conclusion

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.

Resources

Developer, Book lover.