loading...
Cover image for Let's learn Vim - The Basics

Let's learn Vim - The Basics

thetinygoat profile image Sachin Saini Updated on ・3 min read

Introduction

I was a vscode user for the longest time, but I was frustrated with the baggage it brought with itself being an electron app. Don't get me wrong, vscode is an excellent piece of software, it's just my personal experience. With that out of the way, after being frustrated with vscode, I decided to give Vim a try, I have attempted to learn vim in the past too but always gave up, but this time I decided to stick to the decision and make the switch. Now, I am not a vim expert so please feel free to correct me if I mess up somewhere.

Installation

I will be using neovim instead of vim because I think it's better, although all the instructions and settings are the same for vim apart from some changes, which I will mention if there are any.

What is Neovim ?

From the official website -> Neovim is a fork of the venerable text-editor vim, focused on extensibility and usability. It is not a rewrite but a continuation and extension of Vim. Many clones and derivatives exist, some very clever—but none are Vim. Neovim is built for users who want the good parts of Vim, and more

You can install neovim for your OS, I am using Arch Linux so I will install neovim using pacman.

$ sudo pacman -S neovim

You can verify your installation by running the following command.

$ nvim --version

There are plenty of resources available on how to install neovim, if you get stuck google is your friend or reach out to me, I'll love to help.

Vim Modes

There are 6 vim modes but mostly you will be working with 3, listed below.

  • Normal Mode: This is the default mode vim starts in. While in this mode you can't add or edit the text in your file. This mode is strictly for moving around in your file.
  • Insert Mode: As the name suggests, in this mode, you can edit your files.
  • Visual Mode: If you want to select some piece of text, this is the mode you do it in.

If all that didn't make sense don't worry I'll explain as we go, I just wanted to introduce the modes before we use them.

Opening a file

To open an existing file or to create a new one type the following.

$ nvim /path/to/file

this will open the file in normal mode, and as I mentioned before you won't be able to edit the file in normal mode.

Exiting Vim

vim meme

Exiting vim isn't that hard but it's quite odd when you are coming from a conventional text editor like vscode.
To exit vim while in normal mode press :q then press <enter>.

Note: If somehow you got out of normal mode just press <esc> to go back into normal mode.

Moving around in a file

Now that you know how to open and close a file in vim let's do something more ie moving around in a file. You can move around in your file using normal arrow keys but vim also provides a better way to move around in your file using the h j k l keys, where h is for left, j for down, k for up, l for right, this may seem weird at first but if you think about it, all these keys are in the home row, so they are easy to access than arrow keys. Try to use h j k l keys to move around in a file in normal mode to get used to it.

Insert Mode

To get into insert mode from normal mode press i key, now you can edit or add text to your file.
Note: Remember you will spend most of the time in normal mode, and to get to normal mode from any mode just press <esc> key.

The End

In this part, we went over the basics of vim, try to practice, and get used to it. In the next part, we will start with some interesting stuff like copying and pasting, the visual mode and we will start configuring vim to our liking.

Follow me on Github and Twitter.

Posted on by:

thetinygoat profile

Sachin Saini

@thetinygoat

Currently a student, love to post about all things tech.

Discussion

markdown guide
 

A quick tip: If you think Esc is too far away for your little pinky finger to reach, you can try these:

  • Alt + h
  • Alt + j
  • Alt + k
  • Alt + l
 

I have mapped esc to jk it's easier to reach. I didn't mention it here as it's a beginner post :)

 

That's a nice key mapping. I generally use ii

 

I have enabled the vim-plugin in vscode and this allows me to execute most of the vim commands.
Only challenge seen was for operations that require a particular number of lines to be selected for scope (like using :m,n )

 

Why do you think Nvim is better than Vim?
As a note aside, I think the first thing any person interested in learn Vim is to complete the vimtutor, it gives all the basic knowledge to start using it.

 

To be honest, there is actually not a huge difference between Nvim and Vim. And most probably, you won't feel any difference at all. Think Nvim as an updated version fo Vim with some bug fixes.

And yeah, the vimtutor is just awesome.

 

Vim8 has caught up to neovim in terms of features but as a project i think nvim has a better future, and yes vimtutor is awesome!!

 

spacemacs is also pretty good.

 

I have used Emacs before, but couldn't get used to it, is spacemacs similar ?

 

its a nice hibrate of vim and emacs. you can keep all the vim keybinding and powerful emacs features. (i.e emacs org)

 

Are you gonna write about .vimrc or init.vim the next time?

 

Yes I will go over all the customisation and plugin setup :)

 

I use vscode and when i try vscode vim in 3 month I am very happy with it.
Now I use neovim over 1 years but sơmetime I need to switch to vscode for debug.

 

Yeah sometimes, it's fine not to setup vim for a small task and use vscode for it. 😁😁