A beginner's guide to VIM (Text editor).

VIM logo.
credit: Wikipedia.
sudo apt install vim
touch text.txt
vim text.txt
  1. Normal mode
  2. Insert mode
  3. Command mode
  4. Visual mode
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.

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

Insert

Now we can navigate through the text. It’s time to manipulate text.

  • 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.
  • To copy select text — y.
  • 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.
  • To cut one word — dw.
  • To cut one line — dd.

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 .

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.
  • To replace anything by asking for permission — :%s/search/replace/gc.
  • Replace one character — r.

Save

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

  • To save — :w .
  • 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 .
  • To save and quit together — :wq.
  • 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 💪💪.

Resources

--

--

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