Getting started with vim (2018 Update)

Getting started vim

Getting started vim: The choice of one’s text editor can be a hot point of debate amongst developers. There is even a Wikipedia entry on this common rivalry. To be sure, it’s not what you use but how you use it. However, a professional text editor can make a massive difference in your development workflow and productivity. One such text editor is vim.

There are countless text editors and IDEs to choose from. However, often when you ask seasoned developers the choice invariably comes down to two different editors: vim and Emacs. Both vim and Emacs are command-line based editors that have been around for 25+ years or longer. While they both have their strengths and weaknesses, one could argue that vim has a slightly less steep learning curve. Additionally, vim offers a stronger and very active community surrounding it.

This post will take a look at why vim is an ideal text editor for developers looking to improve their workflow. We’ll go through steps on how to get started, as well as available configuration options.

Why use vim?

vim (standing for “vi improved”) is a clone of the classic vi editor for Unix-based systems. As the name suggests, vim is an updated version of vi, the latter first appearing in 1976. vim was first released in 1991 by Dutch programmer Bram Moolenaar. Using a text editor with this long of a history ensures a certain level of stability and maturity. Additionally, it has the major benefit of ubiquity across any Unix-based operating system — including Linux servers.

One of the major principles of vim utilizing as little hand movement as possible. Keeping your hands on the “home row” (i.e. the middle row on your keyboard starting with a, s, d, f), reduces movement and thus inefficiency. As an example, instead of using the arrow keys for navigation, in vim you use the keys h (left), j (down), k (up), and l (right). While arrow keys still work in vim, getting used to this different workflow can speed up your typing considerably.

Another major feature of vim is the use of modes. Most text editors just have a single mode — what you would call the insert mode in vim. Insert mode functions just like a regular text editor. Typing certain keys will always input the corresponding characters.

In vim though, the mode that you start in is normal mode. In this mode, vim assigns just about every key a command or “hotkey” for common editing functions and workflows. For example, typing w in normal mode will bring the cursor to the beginning of the next word. Typing 0 will bring the cursor to the beginning of the cursor line. Below is an overview of some of the most common commands in vim. This should give an idea how much vim can cut down on repetitive editing tasks.

Common commands

$ – move the cursor to the end of the current line

gg – go to the first line of the document

G – go to the last line of the document

dd – delete the current line

2dd – delete the current and following line

d$ – delete from the current cursor point to the end of the line

r – replace a single character (follow this command with the character you would like to replace with)

This is just a small sampling of the 100+ vim commands in existence. The number of commands can be somewhat daunting to someone considering the vim editor. However, learning even a few commands at a time can drastically change the way you work with text.

Getting started vim

To start up vim for the first time, open up your terminal. I highly recommend iTerm2, although the standard Terminal app works fine too. While you can start using vim simply by typing vim, I recommend naming the file beforehand:

vim my-first-text-file.txt

 

This will open up a new instance of vim:

Getting started vim

When you first open up vim, it will be in what what vim refers to as normal mode. Normal mode is the primary mode of vim. In this mode just about every key on the keyboard is a hotkey for commonly-used editing commands. Examples include “delete word”, “move the cursor to end of the line”, “replace text under cursor”, etc.

In order to actually type text in vim, you will need to enter insert mode. To do this, simply type the letter i. Once you hit i, vim will function like a regular text editor. In insert mode all keys input their regular characters.

Getting started vim

Type in some text, and then hit the esc key to return to normal mode. If you are ever unsure of what mode you are in, just hit esc to return to normal mode and start over.

While our new file has a name, it still exists only in a temporary cache and needs to be properly saved to the system. To save a file, type :w then hit enter.

Getting started vim

If you’d like to save the file and exit vim at the same time, type :wq then enter.

To exit vim without saving your changes, type :q! then enter.

Getting comfortable with vim

Above is most of a typical editor’s functionality. However, this is barely scratching the surface of what vim is capable of. To take full control, it is necessary to get comfortable with normal mode and its major functions.

One way to improve typing efficiency is to use the h j k l keys in normal mode. These take the place of the normal arrow keys. This cuts down on hand movement, removing the need for the right hand to move in order to access the arrow keys.

h is for moving left.

j is for moving down.

k is for moving up.

l is for moving right.

This can take a little bit of time to get used to. Once you’ve adopted this new workflow, though, you will likely want to always use it for navigation.

Getting started vim

Cursor Movement

Let’s extend these cursor movement commands further. Typing H will move the cursor to the top of the screen. M moves the cursor to the middle line of the screen, and L moves the cursor to the bottom of the screen. You can remember these easily for standing as “High”, “Middle” and “Low”.

There is one area where vim really shines over typical text editors. In vim you can freely move the cursor around just about anywhere on a given line. You don’t need a mouse at all to reposition the cursor. Regardless of where you are on a line, you can jump to the beginning of the line by typing 0. Typing $ brings you to the very end of the current line.

What if you want to skip ahead a few words? Typing w jumps to the beginning of the next word (i.e. the first character of the next word). If you need to make an edit or addition to the end of the word, use e. This will bring the cursor to the last character of the following word.

You can also expand these commands by prefixing them with a number. If you type 2w, the cursor will jump ahead 2 words. 4w jumps 4 words ahead, 3e jumps to the last character of the 3 words ahead, etc.

If you ever need to go back to the very beginning of a document (i.e. the first line), type gg. Typing G will bring you to the very last line of the document.

Once you get the hang of this, making edits to your writing becomes all so simple.

Expanding your vim knowledge

An oft-cited method for learning and improving your vim skills is to go through the built-in vimtutor. This comes pre-installed with vim. You can access it by typing vimtutor into your command line:

Getting started vim

The vimtutor walks you through some of the most common uses of vim. The included exercises are set up to get you started on editing text right away. As the tutor suggests, it’s not prudent to try to memorize the commands, but rather to learn by doing. It can take anywhere from 30 to 45 minutes to complete vimtutor on the first go. I’d recommend to go through the tutor regularly as you are starting. You’ll gradually decrease your completion time as well as commit the commands to muscle memory.

Migrating from Vim to NeoVim

The official version of Vim is an excellent text editor. However, there has long been disappointment with how updates are welcomed or integrated into the project. The original and primary maintainer, Bram Moolenaar, has somewhat a reputation for denial of otherwise reasonable pull requests. In response to issues with inclusivity in the project, there have been several forks and re-distributions of Vim. This has been in an effort to re-focus the maintenance of the Vim project towards the needs of the community.

One of the primary of these projects is NeoVim. A fork of the original distribution, NeoVim intends to realize many of the features long discussed in the community. Below we’ll go over getting started with NeoVim. Whether migrating from Vim or new to the world of Vim entirely, NeoVim is an excellent distribution.

Benefits of NeoVim

One of the primary benefits of NeoVim over the original distribution is the ability to run tasks asynchronously. This is particularly beneficial and long-awaited. The main advantage here is asynchronous linting in the programming language you’re currently writing in. As a consequence of this, there’s a wide array of NeoVim plugins taking advantage of this feature.

It’s worth mentioning that some of the features of NeoVim, such as asynchronous I/O, have since been added to Vim 8. Vim 8 added added these features in large part due to the success and reception of NeoVim. With its focus on community-driven development, NeoVim has shown itself to be frequently updated and maintained. A large team of developers actively contribute to it.

Download & Install

To download and install NeoVim for OS X, the easiest way is via Homebrew in the terminal:

brew install neovim 

 

Alternatively, you can install the latest stable release from here. Additional distributions are also available for Linux and Windows.

Once installed, you should be able to run nvim in the terminal to start up NeoVim:

Getting started vim

Migrating configuration from Vim to NeoVim

If you’ve been using vim for a while, you likely have configurations you’d like to carry over to NeoVim. The .vimrc file stores all configuration settings. You can typically find this in your home directory (~/.vimrc).

NeoVim uses an entirely different configuration file by default. Just starting out, you’ll need to be that set up first in order to add or import any settings. The default name and location for NeoVim’s configuration file is ~/.config/nvim/init.vim. init.vim is the NeoVim equivalent of .vimrc.

There are at least a couple options for dealing with configuration settings in NeoVim:

  1. Making an exact copy of .vimrc to ~/.config/nvim/init.vim (one of the easiest solutions)

  2. Create a symlink between ~/nvim/init.vim and ~/.vimrc

    This can be done by running the following 3 commands:

    mkdir -p ${XDG_CONFIG_HOME:=$HOME/.config}
    ln -s ~/.vim $XDG_CONFIG_HOME/nvim
    ln -s ~/.vimrc $XDG_CONFIG_HOME/nvim/init.vim

     

If compatibility and synchronization between vim and NeoVim are not a priority, the first choice is the easiest. You only need to maintain init.vim after importing any settings from ~/.vimrc.

Post a Comment

Comments are moderated. Your email is kept private. Required fields are marked *

© 2018 Sunlight Media LLC - Web Design Company