
So You Want to Start Using Vim
Vim is a configurable text editor that is widely used among programmers. With its openness and flexibility, it’s no wonder that many are intimidated of using this text editor. I was in this group, and if you’re reading this article, you probably are too.
In this article, you will learn how to install and start using Vim at the very basic level. This article is meant for beginners who wants to get started. But before jumping into the action, I’m going to provide 2 primary factors that have convinced me to convert to Vim a few years ago.
Complete Keyboard Control
If I had to choose only 1 reason, this will be it. Gone are the days where I use my mouse for switching files, text selection, and jumping around blocks of code. Vim keybindings allows me to work entirely from the keyboard.
User Configuration
Many modern text editors such as Atom and Code allows users to customize their appearance, keybindings, and extend functionality with first and third party extensions. Vim does a great job of this by centralizing these into a single .vimrc file. Since this configuration file is decoupled from the editor, you can check this into your own Git repository and sync to other machines.
Getting Started
First things first, you will need to have Vim installed on your machine. If you’re using a Unix system, Vim should most likely already be installed on your machine.
If you’re using Windows, you can download an executable here.
Once installed, simply boot it up through in your choice of terminal:
$ vimNote: $ indicates a shell (bash, zsh, fish, etc.)
Modes
There are different modes in Vim that you can switch to based on what you are trying to do. For this article, I will only be talking about 2 of them: NORMAL, INSERT.
NORMAL mode is used primarily for navigation and is the default mode when you first start the Vim editor. At any time, you can press the ESC key to switch back to NORMAL mode if you are within a different mode.
INSERT mode is for text manipulation (writing, deleting, etc.). You can switch to INSERT mode with the i key.
Moving Around
There are many different options to move your cursor around. You would normally want to be in NORMAL mode. As a start, you can begin using the arrow keys for movement. The next level is to replace those arrows with h, j, k, l. While there are better ways to move around, it will help you navigate the cursor without ever having to leave the home row.
h → left one character
j → down one line
k → up one line
l → right one character
Don’t be scared to ESC to NORMAL mode to move your cursor around and switch back to INSERT mode with i. Many times I’ve seen people using Vim and all of a sudden, their right hand shift towards the arrow keys.
If you think moving one character at a time is slow, you’re in luck. You can move by n characters or lines by prepending your combo with a number n.
Example: 20h moves 20 characters to the left. 10j moves the cursor 10 lines down. Again, these should be used in NORMAL mode.
If characters isn’t to your taste, you can move the cursor based on blocks of words.
w → jump to the next word (beginning)
e → jump to the next word (ending)
b → jump back a word
In addition, you can jump by n words like how we did with characters.
Example: 20w jumps forward 20 words.
There are a lot more ways to move through your codebase than just the basic vertical and horizontal steps. Over time, you will eventually dig into new keys that allows you to jump to the beginning of a line (^), to the end of a line ($), top of file (g), end of file (G), and more. The Vim docs are there for you.
Write and Quit
This is one of the most highly asked question on StackOverflow. Lots of people use Git and other command line tools that pops up the default editor for your shell which most of the time happens to be Vim. At that point, inexperienced users get lost and have no idea how to quit Vim.
Simply, a command can be executed be typing :command when in NORMAL mode.
To write a file, :w
To exit the editor, :q
To write and quit, :wq
More Learning Resources
Vim is large. There’s way more commands and tips to learn about Vim as compared to the materials shown above. This is just a single drop of the ocean that is called Vim.
I’ve been an avid user for a few years now and still learning new things everyday. With repeated usage, I can guarantee that you will become an expert in no time. If you find this tutorial of use, please be sure to hit the clap button or tweet me @tranvu to show your support.
If you would like to learn more about Vim, please check out the official website here. If you’re a visual learner, vimgifs provides a great set of visual demos of some commands you can add to your repertoire.
