Start using Vim today

Kelvin Salton do Prado
10 min readSep 5, 2019
My own Vim 💚

In this story, I will try to convince you to use Vim 😜

Actually, I started using Vim in my daily work routine just a month ago but I’m already noticing the improvements in my productivity.

I believe you can also improve your productivity by using Vim, so I decided to write this story to help you to get started.

In this story, we’ll cover a few topics like installation, basic commands, plugins, and extra tips briefly, so you don’t get bored.

Let’s start understanding what is Vim.

What is Vim

Accordingly to the Vim website:

Vim is a highly configurable text editor for efficiently creating and changing any kind of text.

Furthermore, Vim is persistent (multi-level undo tree), has an extensive plugin system, has support for hundreds of programming languages and file formats, has a powerful search and replace feature and several other features you will discover on the way.

Vim comes from Vi IMproved and it is an improved version of the Vi text editor, created by Bill Joy, for Unix.

Besides that, Vim is free, open-source and encourages users to contribute socially:

Vim is free and open-source software and is released under a license that includes some charityware clauses, encouraging users who enjoy the software to consider donating to children in Uganda.

Isn’t Vim amazing? 😁

You can read more about Vim in Wikipedia.

Motivations

In this section, I’m writing about my own motivations of choosing Vim but keep in mind that the aim of moving to Vim is to increase productivity while keeping code quality.

I started my professional career working with PHP and C++ but in recent years I have worked mainly with Python and Go (for some side projects). In all these years I have worked with a few IDEs and code editors like Eclipse, SublimeText, Qt Creator, PyCharm and VS Code, so I have some previous knowledge with other tools to compare against Vim. I will not go into detail but each IDE and code editor has its own pros and cons.

To be honest, I started using Vim some years ago to quickly edit files (instead of Nano), but in that time I only knew the very basic commands, to open a file, insert code, save and exit.

I only started using Vim for real, in my daily work, about a month ago, motivated by some coworkers that were already using it and I fell in love with this incredible code editor. My main motivations of migrating to Vim were:

  • Edit code directly in the terminal: as I use terminal for almost everything (e.g. run tests, use git, manage files, interact with the cloud) it is a good idea to write/edit code directly on it.
  • Write code in any language: as Vim is free, open-source and very extensible, we can write code in any language.
  • Improve productivity: the purpose of using Vim, at least in my opinion, is to improve productivity. When you dominate Vim commands you will notice how your productivity was improved compared with other IDEs/code editors.

Now that we have some motivations of choosing Vim, let’s see how to install and use it.

Installation

Since Vim comes pre-installed on almost all Unix based systems, let’s check if we already have it installed:

$ vim --version

It should show the Vim version and a bunch of other information that we don’t need to care about right now, for example:

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jun 19 2019 19:08:44)
Included patches: 1-503, 505-680, 682-1283, 1365
Compiled by root@apple.com
...

If you haven’t it installed yet, you can install on OS X using Homebrew:

$ brew install vim

Or on Ubuntu/Debian using apt-get:

$ sudo apt-get install vim

For other operating systems please check out the official documentation.

Introduction

Vim has several editing modes which makes it more powerful and easier to use than vi. Each mode has a different purpose, so let’s talk about some of them:

  • Normal Mode: for navigation and text manipulation. This is the mode that Vim will usually start in (always use ESC to get back to this mode).
  • Insert Mode: for inserting new text. This is the second most used mode and will be familiar to most people.
  • Visual Mode: for navigation and manipulation of text selections. This mode allows you to perform most normal commands on the selected text.
  • Command Mode: for entering editor commands. This mode has a wide variety of commands and can do things that we can’t do in normal mode.

These definitions may seem confusing at first glance, but don’t worry, you will naturally understand and use these modes when using Vim.

Basic Vim Configuration

Before start using Vim we can configure it by editing the .vimrc file with Vim itself: sudo vim ~/.vimrc

Here are some basic configurations that we can use:

  • set number: show line numbers.
  • set ruler: show row and column ruler information.
  • set linebreak: break lines at word (requires wrap lines).
  • set textwidth=500: line wrap (number of cols).
  • set showmatch: highlight matching brace.
  • set visualbell: use visual bell (no beeping).
  • set hlsearch: highlight all search results.
  • set smartcase: enable the smart-case search.
  • set ignorecase: always case-insensitive.
  • set incsearch: searches for strings incrementally.
  • set autoindent: auto-indent new lines.
  • set shiftwidth=4: the number of auto-indent spaces.
  • set softtabstop=4: the number of spaces per tab.
  • set backspace=indent,eol,start: backspace behavior.
  • set encoding=utf8: set the default encoding type.

We can also use a tool like Vim Config to create the basic Vim configuration.

Basic Commands

In this section, we will learn some basic commands that will help us to start using Vim. Note that these commands must be used in the normal mode.

Move the Cursor

We can use either the keyboard arrows or the following keys to move the cursor around.

  • h: move one character left.
  • j: move one row down.
  • k: move one row up.
  • l: move one character right.

Navigate the File

  • gg: move the cursor to the beginning of the first line.
  • G: move the cursor to the beginning of the last line.
  • ctrl + d: scroll window downwards in the buffer. The number of lines comes from the ‘scroll’ option (default: half a screen).
  • ctrl + u: scroll window upwards in the buffer. The number of lines comes from the ‘scroll’ option (default: half a screen).

Insert Text

  • i: start insert mode in the current cursor position (insert).
  • a: start insert mode in the next cursor position (append).
  • o: create a new line below and start the insert mode.
  • I: move the cursor to the beginning of the line and start the insert mode.
  • A: move the cursor to the end of the line and start the insert mode.
  • O: create a new line above and start the insert mode.

Delete Text

  • x: delete the next character (del).
  • X: delete the previous character (backspace).

Copy and Paste

  • yy: copy the current line.
  • dd: cut the current line.
  • D: cut the content from the cursor position until the end of the line.
  • p: paste the content in the line below.
  • P: paste the content in the line above.

Undo/Redo

  • u: undo.
  • ctrl + r: redo.

Write and Quit

The commands starting with : will be executed in command mode but they must be typed in the normal mode.

  • :w: write/save the file.
  • :wq: write and quit.
  • :q: quit.
  • :q!: quit without saving.

Shortcuts to Write and Quit

  • :x: write and quit.
  • ZZ: write and quit.

More Specific Commands

In the section above we learned about commands that we will use every day when working with Vim. In this section, we will learn other commands that we can use in specific situations.

Enter Visual Mode

Remember that in the visual mode we can apply commands to selected parts of the text. The commands are similar to the normal mode but instead of double commands we usually use single commands, for example, instead of dd we use d and so on.

  • v: enter in the visual mode (to select characters).
  • V: enter in the visual line mode (to select lines).
  • ctrl + v: enter in the visual block mode (to select columns).

Useful Commands

  • de: cut the word from the current cursor position.
  • diw: cut the entire word, even if the cursor is not at the beginning.
  • 3dd: cut 3 lines. 3 is the number of lines to apply the command.
  • J: group lines. Can be used for selected text in the visual mode.
  • gUU: convert the current line to uppercase.
  • guu: convert the current line to lowercase.
  • ctrl + a: with the cursor on a number, it will increment the number.
  • ctrl + x: with the cursor on a number, it will decrement the number.

Split Screen

It is possible to open multiple files at once by splitting the screen horizontally or vertically.

  • split: split screen horizontally.
  • vsplit: split screen vertically.

Switch Between Screens

Use the following commands to switch between the split screens:

  • ctrl + ww: switch the screens from left to right.
  • ctrl + h: switch to the left screen.
  • ctrl + j: switch to the bottom screen.
  • ctrl + k: switch to the top screen.
  • ctrl + l: switch to the right screen.

Search and Replace

  • /content: use slash and type the content you want to search. Press Enter to apply the search and use n to navigate between the matches.
  • :%s/replace this/with this/g: search and replace the text in the current file.

Plugin Manager

There are a few plugin managers available out there, like volt or vim-pathogen, but the best knowns are vim-plug and Vundle.

Currently, Vundle is more popular on Github since it has about 20.215 stars against 15.800 stars from vim-plug. Even so, I’m personally using vim-plug, but you can use either one, I’m pretty sure the main plugins can be installed with any popular plugin manager.

On the plugin manager page, we can find an example or quick start explaining how to use them (don’t worry, they are really simple to use).

Plugins

In this section, we can find some well-known plugins that I personally use:

  • vim-polyglot: a collection of language packs for syntax highlight and indentation support.
  • ale: ALE (Asynchronous Lint Engine) is a plugin providing linting (syntax checking and semantic errors) in NeoVim 0.2.0+ and Vim 8 while you edit your text files, and acts as a Vim Language Server Protocol client.
  • completor.vim: is an asynchronous code completion framework for vim8. New features of vim8 are used to implement the fast completion engine with low overhead.
  • jedi-vim: is a VIM binding to the autocompletion library Jedi. The Jedi library understands most of Python’s core features. From decorators to generators, there is broad support.
  • ctrlp.vim: full path fuzzy file, buffer, mru, tag, … finder for Vim.
  • nerdtree: is a file system explorer for the Vim editor. Using this plugin, users can visually browse complex directory hierarchies, quickly open files for reading or editing, and perform basic file system operations.
  • nerdtree-git-plugin: a plugin of NERDTree showing git status flags. Works with the LATEST version of NERDTree. It is simple but very useful.
  • vim-test: a Vim wrapper for running tests on different granularities. This plugin consists of a core which provides an abstraction overrunning any kind of tests from the command-line.
  • vim-go: this plugin adds Go language support for Vim, with a bunch of amazing features like package compilation, file execution, syntax highlighting, code linters and so on.
  • vim-airline: lean & mean status/tabline for vim that’s light as air. When the plugin is correctly loaded, Vim will draw a nice status line at the bottom of each window.
  • vim-airline-themes: the official theme repository for vim-airline.
  • vim-colors-solarized: solarize your terminal. See the Solarized homepage for screenshots, details and color scheme versions for Vim.
  • golden-ratio: when working with many windows at the same time, each window has a size that is not convenient for editing. golden-ratio helps on this issue by resizing automatically the windows you are working on to the size specified in the “Golden Ratio”.
  • nerdcommenter: comment functions so powerful — no comment necessary. A tool that helps us to include and remove code comments.
  • auto-pairs: automatically insert or delete brackets, parens, quotes in pair.
  • vim-fugitive: use git directly inside Vim. fugitive.vim may very well be the best Git wrapper of all time.

Note that the plugins that I use are focused on Python and Go but there are a lot more plugins to almost any programming language. Check out the vim awesome list to find out more plugins.

Extra Tips

  • Vim has a lot of commands, don’t try to memorize them all at once, you can start with the basic ones and learn others as you need.
  • Vim is intended to make you use as few keys as possible, so when using an uppercase letter use shift + key instead of caps lock + key + caps lock.
  • You can combine numbers with almost any command, for example, to move the cursor 10 rows down you can use something like: 10j.
  • Try to use key combinations to perform operations quicker. For example, to delete (cut) all content of a file you can combine: ggVGd.
  • Vim Bootstrap: automatically generate configurations for Vim based on the selected programming languages. I believe this is a great tool to start configuring your .vimrc file but it is better to manually configure it so you have more control about what is happening under the hood.
  • Vim Awesome: a list with awesome Vim plugins from across the universe (I really recommend that you take a look at this one).

Endnotes

Congratulations, you finished reading this story!!! 🎉

You may find hard to learn Vim at the beginning but I assure you it worths the time invested and things got better when you learn some commands and shortcuts. So, don’t be nervous, you will naturally learn new commands and concepts as you go, and someday you may become a Vim master.

I hope you have enjoyed this reading. If you liked it, please give a clap. 👏

Now we’re ready to write some code using Vim!!! 😎

--

--