Let’s learn Vim! Part 1

Written by mottakin | Published 2019/01/11
Tech Story Tags: vim | programming-tools | learn-vim | code-vim | learning-vim

TLDRvia the TL;DR App

You can finally quit it!

So I tried to get the hold of Vim once upon a time, when I was taller, younger and sharper.

It was probably some cold windy midnight of the late December of 2016. I opened up my vault, picked up my black hoodie with the Linux Penguin on it and put it on. Then I turned off all the lights of my room and locked it up from the inside.

Only the black-shadowy light from my terminal’s light green font was illuminating my room. With this 10x hacker environment, and of course without even thinking of further consequences, as I always do, I opened up my terminal and simply wrote **vim** and hit enter.

And the rest is a story of nightmares.

Okay I am exaggerating a lot here. 😛

Recently, when I was asked by the boss at the office to learn Vim (as I had to navigate through an **ssh** server), I decided to finish my unfinished business with Vim.

Which brought me here on Medium, to write about Vim. As a wise man once posted on Facebook, sharing knowledge is power.

So, open up your terminal and let’s talk about it.

Step 1: Install Vim

I am assuming you have **bash** and you are a Linux user. I hope there are similar ways for Windows users to install Vim.

  • On your terminal, write: **sudo apt-get update**.
  • Install Vim using: **sudo apt-get install vim**. That’s it.

Step 2: Initiate Vim

This is going to be fun!

  • Write **vim mytext.txt** on terminal and hit enter.
  • You will see something like the following picture. Remember I am using a special tool called **vimrc**. That’s why it will probably look different from your terminal. You can find **vimrc** here. For now I guess you should ignore it.

Vim!

  • Don’t try to hit any keys randomly! At this very stage, your Vim is on Normal Mode. This mode is like an inspection mode, you basically navigate, delete, replace and do a bunch of other operations in this mode. You cannot write text when this mode is on.
  • Let’s first exit from Vim without breaking anything!

Just write **:wq** and hit enter.

What it does is, it writes the file on your disk and saves all the changes you made, and quits. If you do not want to save the changes, hit **:q!**.

Yep, that’s it!

Step 3: Write Something Using Vim

So now we will actually write something on Vim.

  • In the previous step, as you have seen, we were working on Normal Mode. You cannot really write inside your file when this mode is on. You have to switch to Insert Mode.
  • To do that, all you need to do is type and hit **i** from Normal Mode.

  • Now you can simply type whatever you want.

Poem from Reddit.

  • We want to save it, right? To save the change on the disk, hit **ESC** to switch to Normal Mode. And type **:w**. Or you can also write **:wq** to write and quit.
  • Feel like a scientist.

  • When you are in Normal Mode, to move up-down-left-right, you should use:**h** — left**j** — down**k** — up**l** — rightYeah this is so weird. But it is meant for convenience. I guess once you are very accustomed to the key bindings of Vim, you can realize that.

  • You can use the arrow keys in Insert Mode to navigate.
  • Assume you wish to append some text at the end of a particular line of your text file. Move the cursor to that particular line (of course in Normal Mode) and hit **A** with **<shift>** key.

Step 4: Edit the File

  • Let’s assume we want to edit our previous text file. Say, you want to replace the word gray with blue.To do this, of course you can go the Insert Mode and use backspace to remove and write whatever you want_._ But we can do it more smoothly in Normal Mode. Move your cursor to the beginning of the word you want to delete, and hit **dw**.

  • Then switch to Insert Mode and write blue.

  • Now the problem is, our poem does not rhyme! We should make it rhyme to claim a Nobel prize in Literature. Let’s delete the last line and write something worthy.
  • To do that, in Normal Mode, move your cursor to the line you want to delete, and hit **dd**.

  • And switch to Insert Mode to write what you want.

Poem by M Chowdhury (1995 — )

Step 5: Some More Commands

  • There is this command **:d$** which deletes from the position of the cursor to the end of the line.
  • You can easily do undo operation in Normal Mode. Just hit **u**. To redo, hit **CTRL-R** .
  • Suppose you have made a typo somewhere and you want to replace one character with another one. You simply move the cursor on top of the character in Normal Mode, hit **rx** where x is the character you want the current character to be replaced with.
  • Or you just want to remove a character. Move the cursor on that and hit **x**.

I guess this is it for the part 1. I hope to get the next part done very soon! Hope you learned something new. Now go and show it off to your girlfriend.

Thanks!

For part 2: Lets learn Vim! Part 2


Published by HackerNoon on 2019/01/11