Getting started with VIM

SaswataBasu
ADGVIT
Published in
5 min readAug 29, 2021

What is Vim?

Vim is a sophisticated text editor. It’s the contraction for Vi + Improved. Vi is a text editor that was created for the UNIX statement around 1976. Vi is short for “Visual” and was developed as the visual mode for the even older “ex text editor”. Vi then improved and came along as Vim around 1995. It’s basically a text editor where you navigate around the screen with a keyboard rather than a mouse.

Why Vim?

So, in this day and age of modern IDE’s why would anyone ever need or use Vim! When you write code every day your productivity decreases when you touch the mouse, so learning Vim is like learning how to drive. At first, you are weary of everything but as you get accustomed it turns into second nature.

Vim runs on the terminal, which isavailable on almost every machine, and I’m sure those who use git have accidentally found their way into Vim with no way to escape.

If you discover yourself here you’ll be able to quit Vim by typing “:q” (quit) to shut an unmodified file or if it’s been modified use “:q!” to throw away your changes or “:wq” to write the changes then quit.

Vim allows you to do things quickly. Even making complex edits is fairly quicker and once you recognize a number of the powerful features like macros, registers, auto-completion, text object searching filters and you find out how to navigate you’ll find that you can use a number of these same navigation key bindings in other programs like MML.

You can also configure your command-line-shell to use Vim editing features. Services like Gmail also support Vim commands in the Gmail keyboard shortcuts. Even editors like VS Code, Xcode, Sublime Text, and Atom support Vim and it is cross-platform and also has a GUI mode.

GUI mode of gVim (source: vim.help)

Vim also has syntax highlighting and it is more than just for making your files look pretty. The foremost important thing about syntax highlighting is that it makes mistakes easy to identify. If we make an edit to a file and then the syntax highlighting disappears for that line take that as an indication that we need to check our work. By default, Vim includes syntax highlighting for more than just programming languages. If you are a Linux system administrator then syntax highlighting for the C programming language may not be as useful to you as syntax highlighting for configuration files by default then includes syntax highlighting for patchy configuration files, deny hosts files, get config files just to name a few.

So how do I edit stuff in Vim?

So first let us use Vim to open up a file. Type the command “Vim” followed by the file name to open the document.

This is the screen that we get. So now let us use vim commands to edit this file.

This is the normal mode of Vim but we can toggle between the different modes like insert command-line and visual depending on what we want to do.

So let us enter into insert mode to make some changes and we do it by pressing the “i” key.

Insert Mode

Now let’s write a simple code and execute it.

I’m dropping the sample code here for you guys to try:

function vimFun(){

console.log(‘Vim can be cool!’);

}

vimFun();

For our code we hit “:w” to save and then use “!” followed by the shell command which in this case is “node” to execute it.

And as you can see our code has been executed.

We use the arrow keys or the keys H, J, K, L for navigation.

source: YouTube/Fireship

To delete any character, we use the “X” key or we can use “dd” to delete an entire line. To undo we use “U”. Like this all the various functions like copy, paste, etc. are done using various key bindings from the keyboard and I will leave a Vim cheat sheet below for you to try out.

Some additional commands:

· v — starts visual mode for selecting the lines and you can operate on that like d delete

· :r — Filename will insert the content into the current file

· R — to replace more than one character

· y — operator to copy text using v visual mode and p to paste it

· yw — (copy)yanks one word

· o — opens a line below the cursor and starts Insert mode.

· O — opens a line above the cursor.

· a — inserts text after the cursor.

· A — inserts text after the end of the line.

How to set up Vim on VS Code?

First head over to the extensions panel of your VSC and search for Vim.

Then you just need to install this extension and you are all set to go!

Resources for the more curious bunch:

Hope this article was helpful, feel free to reach out for any queries.

--

--