Getting started with VIM (2021 Update)

Getting started with VIM (2021 Update)

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.

Is Vim the Best Text Editor?

Is Vim the Best Text EditorWhen searching for a good text editor, many people get confused as there are so many choices. However, Vim is one of the best text editors. When you compare it to modern graphical editors, such as Brackets and Atom, Vim only uses a sliver of the system’s total memory. Also, it loads instantly and is much faster than Emacs.

One of the best things about Vim is that getting started with the editor is straightforward. Did you know that Vim is highly configurable and boasts notable features? Some of these features are syntax highlighting, graphical versions, mouse support, and visual mode. You will also find many new editing commands, which is excellent.

When it comes to support, Vim has an energetic and enthusiastic community that offers remarkable support with regard to developing remarkable plugins and providing helpful Vim tips and tricks both for beginners and expert users.

With support for Linux, Windows, and Mac, Vim is a reliable and stable editor that integrates well with many popular tools on the market. You will be happy to know that Vim is always available. All Linux machines have it.

Vim also has a small footprint, fast startup, low latency, and allows for more screen space, which is remarkable. Vim is also very fast, responsive, and lightweight, even when editing huge source code files.

Vim Text Editor Commands

You may know that Vim is a little different from conventional Linux text editors. For example, Vim maintains the unique Unix philosophy of doing one thing at a time and doing it right.

Did you know that the fundamental idea behind this philosophy is that most programmers spend most of their time editing code, not writing it? If you would like to get the most out of Vim, you should be familiar with Vim commands.

In Vim, you may spend most of the time in “normal” mode and switch to “insert” mode only when you’ve to add or modify some text. This is important as it ensures that all edits become their own self-contained operations that you can repeat or chain with other operations.
Did you know that most editing commands might optionally be preceded by some number to apply it multiple times? For example, you can delete three lines by pressing 3dd. Also, it is worth noting that you’ve two options for how you can select text.

For instance, you can use commands such as dd, which deletes one line at a time, and yy, which copies one line. The other way is to highlight text and copy that text to the unnamed register.

Like movement commands, if you put a number in front of the command, it will increase the number of times a specific task is completed. For example, placing a number, such as 5, in front of yy increases the number of lines copied. This means that 5yy will copy 5 lines.

  • yy – Copies one line
  • yw – Copies one word
  • v – Highlights a single character at a time using various arrow buttons or the k, h, j, and l buttons
  • V – Highlights a single line, and you can use the movement keys to highlight additional lines
  • p – Pastes whatever you have copied to the unnamed register
  • dd – Deletes one line of text
  • dw – Deletes one word

Note that in command mode, the Vim editor processes various keyboard inputs as commands. On the other hand, in insert mode, the editor processes these inputs as their original values. You can use the command mode to control text editing.

How do I Open Text Editor in Vim?

You have to do a little preparation before jumping into Vim. You can easily open a console terminal right from your Linux OS (operating system). As soon as a terminal window is up, you can type the ls command in order to list the current directory.

After that, type mkdir Tutorial in order to create a new directory. It is called Tutorial. You can enter the directory by simply typing cd Tutorial. You can open a document and type: vsplit /path-to-document/document. Note that this will open the specified document, splitting your screen so that you can easily see both documents.

If the file does not currently exist, the editor will open a new file (blank). If it exists, Vim will open the file so you can edit it. Keep in mind that you can create a new file and edit an existing one using the vi.

Vim will start in the command mode – you cannot type anything in this mode. You can enter the insert mode by pressing i.

Vim Text Objects

Vim Text ObjectsThe best thing about text objects in Vim is that they allow you to make changes or modifications to words, phrases, and text sections based on multiple parameters.

You will like how Vim allows you to edit your text with high precision using Vim text objects. This is why Vim’s text objects are worth understanding.

To be an efficient editor in Vim, you’ve to edit beyond individual characters. Rather, edit by sentence, word, and paragraph. Did you know that in Vim, these higher-level contexts are known as text objects? For example, when you type daw, Vim follows your command and then deletes a word. If you type ci’, Vim helps you change or modify some text inside single quotes.

Vim is excellent as it gives you this precision by seamlessly combining its various useful operators (change, delete, yank, etc.) with powerful text objects. Leveraging operators in combination with these text objects, Vim offers you fine granularity and plenty of power in order to edit text-based documents as well as code.

Vim Text Highlighting

There is no doubt that no text editor is really complete without syntax or text highlighting, the coloring of special phrases and keywords within a program. Did you know that syntax highlighting emphasizes the structure of your document?

It also helps identify typographical errors and helps with debugging purposes, making text editing easier as well as more productive.
Vim text highlighting is a feature that displays text, particularly source code, in various colors and fonts, depending on the category of terms. Did you know that syntax or text highlighting is on in Vim by default?

Also, note that the highlighting mechanism uses various invisible tokens. These are inserted into a Txtfmt buffer using intuitive mappings offered by the filetype plugin.

Author Bio

Angelo has been involved in the creative IT world for over 20 years. He built his first website back in 1998 using Dreamweaver, Flash and Photoshop. He expanded his knowledge and expertise by learning a wider range of programming skills, such as HTML/CSS, Flash ActionScript and XML.

Angelo completed formal training with the CIW (Certified Internet Webmasters) program in Sydney Australia, learning the core fundamentals of computer networking and how it relates to the infrastructure of the world wide web.

Apart from running Sunlight Media, Angelo enjoys writing informative content related to web & app development, digital marketing and other tech related topics.

Post a Comment

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

© 2021 Sunlight Media LLC | 811 W 7th St. Level 12, Los Angeles CA, 90017 | 323.868.3581