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

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.

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.

  1. Normal mode
  2. Insert mode
  3. Command mode
  4. 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.

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.
  • One character right — l.
  • One line up — k.
  • One line down — j.
  • Start of the next word (forward) — w.
  • End of the next word (forward) — e.
  • Start of the previous word (backward) — b.
  • Start of the line — 0.
  • 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 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.
  • After where the cursor stay — a.
  • Next blank line where the cursor stay — o.
  • The previous blank line where the cursor stay — O.

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.
  • One word — cw.
  • Entire line — cc.
  • From cursor to the end of the line — c$.
  • From cursor to the start of the line — c0.

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.
  • Redo changes — ctrl + r.

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.
  • To copy one line — yy.

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.
  • To cut one line — dd.

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 .
  • To match from the end 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.
  • All the matched word — :%s/search/replace/g.

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.
  • When you want to perform the last command again instead of use that command again just press . (dot/period).
  • When you are in insert mode, you have made a spelling mistake to remove that word instead of using backspace use ctrl + w. or you made so many mistakes use ctrl + u . This will get things done faster.
  • To get help from VIM enter :help.
  • when you stuck just enter your terminal vimtutor .

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.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store