Vim for Developers: Part 1 — The Basics

David Ondrich
Analytics Vidhya
Published in
8 min readJun 30, 2020

--

Check out Part 0 of this series if you haven’t already! It will tell you what you can expect from these posts.

TL;DR — If you’re somewhat familiar with NeoVim/Vim and you’re just looking for a badass config, you can check out my dotfiles here.

The Basics

This post will walk you through the very most basic functionalities of Vim. These are the fundamentals and are important to master before moving onto more complicated use-cases.

Installing NeoVim

Installing NeoVim is very simple and well-documented. You can find the details, by operating system, here. I’m a Mac user and installing it was as simple as:

brew install neovim

and voila! We’ve done it!

Modes

Before we go ahead and open Vim up for her maiden voyage we must talk about modes. Modes, in my opinion, are the real genius of Vim, especially for coding purposes. There are a number of Modes in Vim, but I’ll highlight the most important (you’ll use them 99.9% of the time) ones below.

It’s well known that code is read more than it is written and/or changed. So why then are most IDEs always in “edit” mode. That is, when you type letters on your keyboard they are spit out wherever your cursor currently happens to be.

Not in Vim. In Vim when you fire it up you’ll find yourself in something called Normal Mode.

Normal Mode

This is where you’ll spend most of your time. This is where you will read code and navigate files. Since in Normal Mode you aren’t editing text, we are now free to use all the keys on the keyboard to navigate text. This is Vim’s secret-sauce. More about navigating below.

Insert Mode

Insert Mode is entered whenever you’re ready to edit text. There are a few ways to enter Insert Mode which we’ll walk through later in this post. The main thing to remember is you’ll only want to enter Insert Mode when you want to change text, and you’ll want to leave it as soon as you’re finished editing. This is because you lose your ability to navigate efficiently whilst in Insert Mode.

Visual Mode

Visual Mode is probably used a little less than the first two modes mentioned, but can still be extremely useful. It’s mostly used to highlight blocks of text, and then perform a command on the entire block. For example, you maybe highlight a block and indent the entire thing.

Command Mode

Commands are essential for Vim, and will be used heavily, especially when we get to plugins. Commands, as we’ll see, make Vim do something. To enter a command you simply type : while in Normal Mode and then the command.

Opening NeoVim

Once you’ve got NeoVim installed you can launch it by typing:

nvim

I’ve aliased it in my terminal (I use iTerm2 and Z shell btw):

alias n="nvim"

Then you can launch NeoVim by simply typing n in your terminal. Imagine that?? One character and BOOM your (soon-to-be) IDE is good to go. You’ll notice that Vim launches near instantaneously. That’s a user-experience that just can’t be matched by IntelliJ, or even VS Code which is much lighter and speedier than IntelliJ.

To open a file in Vim, it’s as easy as putting the file name (or path to the file) after the command. For example:

nvim my_file.txt

If you omit a filename Vim will open up in whatever your present working directory is, but you won’t see anything just yet.

Closing Vim

There’s an old adage:

How do you make a random-string generator?
Open Vim and ask a non-Vim user to close it.

Or something like that. It’s true though, Vim doesn’t close like most programs because it runs inside your terminal. You don’t want to close your terminal just to exit Vim. So how do you do it? Simply type…

:q

Simple sure, but still a little strange. Let’s break it down.

:

The colon will become very familiar to you while using Vim. Typing : brings you into Command Mode, from Normal Mode, as we learned above. It can be a little cumbersome to always be typing : because you have to Shift+; but we’ll get to a workaround later.

q

This is your first lesson in mnemonics. You can remember q: quit and with that…

Mnemonics

Mnemonics are the key to learning the Vim-grammar. From the outside it looks like knowing Vim is just memorizing a bunch of hotkeys. Nope. It’s really speaking the language of hotkeys, using mnemonics. In the next section you’ll see that almost all commands have a mnemonic, and it makes them 1000x easier to remember.

Navigation Commands

These commands will all be used in Normal Mode to help you navigate files efficiently. They take practice, but soon you’ll find yourself loving them, and missing them when you’re Vim-less.

h - cursor left 1 character
j - cursor down 1 line
k - cursor up 1 line
l - cursor right 1 character

These are the very basis of Vim. Goodbye arrow-keys, and more importantly goodbye mouse. The most genius feature of Vim is you don’t need to move your hand from the main typing position to navigate. This accounts for 50% of Vim’s speed right off the bat. Practice, practice, practice them. Really. Don’t touch those arrow-keys (some people even disable them, we’ll cover it), and definitely do not reach for your mouse.

Now let’s go through the entire alphabet. Some of these I use more than others. You’ll find which ones resonate with you.

a — append — brings you from Normal to Insert mode and places your cursor after the character it’s currently on.

A — append at end EOL — brings you from Normal to Insert mode BUT it places your cursor at the end of the current line.

b — back — moves your cursor back an entire word.

c — change — change is often paired with other commands, we’ll get to pairing in a later post.

d — delete — as the name suggests, however this too, like c , needs to be paired. For example: dd — deletes the entire line you’re currently on.

e — end (of word) — jumps cursor to last character of current/next word.

f — find — is paired with a character and will jump cursor to the next instance of the character in the current line. For example: fa will jump to the next a in the line. If there is no such character the cursor will not move.

g — go — another pairing command. Two common and useful examples are:
gg — jumps cursor to first top of the current file.
G — jumps cursor to bottom of the file.

i — insert — most common way to enter Insert Mode. It enters directly when your cursor is.
I — insert at end of line — enters Insert Mode at beginning of the current line.

m — mark — marks are a very useful trick in Vim. I will cover them in the next post about pairing commands.

n — next (occurrence of a search-term) — when searching a file if there’s more than one match, this will bring you to the next match (and cycle through them).

o — open — creates a blank, new line under the current line, enters Insert Mode, and places the cursor at the start of the new line.

p — put or paste — pastes whatever is in clipboard.

q — honestly idk a good mnemonic for this — how you record macros, which are hugely useful. We’ll cover them in the next post.

r — replace — this commands is paired with a letter, and will replace the character you’re currently on with that letter. For example typing rb will replace the current character with a b .

s — substitute — deletes the character the cursor is currently on, and enters Insert Mode.

t — to or towards — t combined with a letter will place the cursor directly in front of the next occurrence of that letter in the line. If there is no such character the cursor will not move.

u — undo — un-does the last command.

v — visual — enters Visual Mode.

w — word — jumps cursor forward an entire word.

x — cross-out — deletes the character the cursor is currently on, but stays in Normal Mode.

y — yank — essentially this is the copy command

z — fold — bad mnemonic, we’ll covers folds later, they aren’t critical to know just yet.

Woohooo! Now you know the Vim-bc’s and you’re ready to conquer the world. I just want to touch on a few more things to know which don’t fall into the alphabet.

0 — 0mg I'm at the beginning of the line — bad mnemonic again, my apologies. But, this commands jumps your cursor to the very start of the current line.

^ — just remember this — jumps your cursor to the first character in the line. I’ve found I use this more than 0 but they both have their moments.

$ — also remember this — jumps your cursor to the end of the line.

/ — search within a file — typing forward-slash plus your search-term, then hitting enter/return will search within the file. If there are multiple occurrences you can cycle through them by hitting n — next

esc — escape insert mode — brings you from Insert Mode to Normal Mode.

ctrl + [ — also brings you from Insert Mode to Normal Mode.

Practice

That was a lot all at once, and the only way to get it down-pat is to practice. My tip is to start small. Don’t try using Vim at work all at once. But perhaps you have a quick task that requires a small refactor, such as renaming some variables or functions. That’s a great way to start, with some true text-editing tasks.

Vimtutor

If you’re really looking to maximize you Vim-ninja skills I recommend checking out the tutor. You can access it in NeoVim with the command :Tutor you can also access the Vim version from your terminal by simply typing vimtutor. The tutor will open a text-file with TONS of information about commands, and a series of challenges, i.e. go to line X and delete word Y. Check it out!

Disclaimer

Vim has endless functionality, and this just scratches the surface. It’s a jumping-off point. If you run into something you think you need that’s not on the list, drop me a comment and I’ll help you out!

Up Next in the Series:

Vim for Developers: Part 2 — Advanced Basics

--

--