Vim C++ – There Is Such a Thing! (Tricks to Use Vim in C++)

Dori Exterman
Dori Exterman reading time: 9 minutes
October 4, 2021

What is Vim?

Vim is a well-known and mature text editor that is highly efficient at editing code. It is often referred to as a “Programmer’s editor” but in practice, it can be used for all kinds of text editing. Whether it is being used to edit scripts, configuration files, emails, or C++ files, it is highly customizable and can be set up to work in a very simple way. For the scope of this article, we will refer to our flavor as Vim C++.

Vim is not a feature-rich word processor like Microsoft Word and it is not a full-fledged IDE, such as the ones that we have listed as the top C++ IDEs for 2021. That said, it is a very powerful and widely-used editor and well worth discussing its capability for coding in C++. 

Why Use Vim C++?

Vim C++ is a powerful and lightweight editor that consumes few resources compared to an IDE like Visual Studio. One of the distinct advantages it has over the typical IDE is speed. After somebody becomes accustomed to the hotkeys and keyboard-based interaction in Vim C++, it is highly efficient at manipulating text, on-par or better than current-day text editors. There are keyboard combinations for every action and an adept user can perform functions like search and replace very quickly. Users can further extend this by creating key mappings to execute complex commands with minimal input.

Vim is popular among programmers because it provides syntax highlighting when writing code or editing scripts. Furthermore, it supports multiple programming languages and can be customized using an extensive plugin system. For example, there are a variety of plugins for C++ that offer support for syntax checking, code completion, and debugging. Hence, Vim C++! 

Why Not Use Vim C++?

With the undisputed speed advantage and a plethora of features available by way of plugins, Vim is still not for everybody. One of the main complaints about Vim is that it has a steep learning curve. Even the GUI version in Windows, gVim, feels foreign to some Windows users because of the different editing modes. A common joke is that beginners need to view the tutorial just to exit the editor!

Amateur hint: if you’re in the Normal editing mode, “:wq” will save your file and exit, whereas “:q!” will exit immediately and without saving. These are not the only commands you’ll ever need but at least you can exit the application. Why the colon? Vim commands all start with the colon. 

Vim C++ Look and Feel

Vim is operated by entering commands and even if you are running gVim, keyboard commands can still be used. In fact, those familiar with Vim will still enter commands even when using the GUI because, in some instances, it is faster.

In the following screen, a user has entered the “:q” command to quit the editor.

gVim introduction screen

Figure 1: gVim introduction screen with “:q” entered by the user 

New users to Vim will often ask, “What’s with the ~ character on the left of each line?” In Vim, a tilde (~) represents a blank line. If the tilde is absent and the line is blank then there is either a newline, space, tab, or another non-printable symbol or symbols present. Essentially, the tilde represents a non-existing line, as opposed to an empty line. 

Tutorials and Learning

While Vim C++ does indeed have a learning curve, most users agree that it is easy to get started with it. There is a built-in tutorial called vimtutor, and it is normally installed with Vim.

vimtutor

Figure 2: vimtutor is a separate command that is a good starting point for beginners

In addition to vimtutor, myriad tutorials and how-to guides are available online in written and video format. Anybody with a keen interest in learning Vim C++ will have no trouble finding places to learn the tips and tricks.

Tricks to Use Vim C++

With such a highly customizable editor, there are a multitude of ways to accomplish things. To get you started, we have selected a few tips and tricks to steer you and your fellow C++ gurus in the right direction.                 

Themes

A theme in Vim C++ is a color scheme, which is the set of colors that are used to distinguish controls, differentiate sections in your source code, and highlight other aspects of the interface. Vim comes preinstalled with several themes and there are many more available on GitHub. A list of the most popular Vim color schemes can be found here.

To try out a new color scheme, it can be done by entering a simple command into Vim. For example, the default color scheme for a section of C++ code looks like this:

Default color scheme in Vim C++

Figure 3: Default color scheme in Vim C++

By applying the :colorsceme command, you can change the color scheme to something that makes C++ source code look clear to you. For example, to change to the built-in “Desert” theme:

:colorscheme desert

Desert color scheme in Vim C++

Figure 4: Desert color scheme in Vim C++

There are two ways  to get a list of all of the installed color schemes, as follows:

:colorscheme <space> <tab> will bring up a list that you can select from
:colorscheme <space> <ctrl-d> will display a text listing of them

Selecting a comfortable color scheme is something that most editors support, but it’s common because it’s a feature that most people want. Having good readability will improve code efficiency but will also help with the all-important code reviews. When your code is easy to follow, it’s easy to review.

Code completion

Code completion is a feature that is available in some programming environments, such as the Visual Studio IDE, that is used to speed up the process of coding and reduce the incidence of errors. Intelligent code completion is context-sensitive and will provide an easy way to access class member functions and variables. The suggestions are made in real-time and in many cases, speed up programming because details like specific class functions no longer need to be memorized.

The following example is from Visual Studio, where as soon as the “>” is entered to complete the pointer reference, the list of accessible class variables is displayed.

code completion from Visual Studio vs Vim C++ with YouCompleteMe

 Figure 5: Example of code completion from Visual Studio vs Vim C++ with YouCompleteMe

Several extensions are available for Vim to provide code completion functionality for C++. Three of the more popular ones are YouCompleteMe, Clang complete, and OmniCppComplete. These extensions are complete with documentation on installation and usage and have been utilized by many thousands of people to improve their coding efficiency.

Real-time syntax checking

The Syntastic Vim C++ extension is a real-time syntax checker that scans files and presents errors to the user. A syntax checker is used by programmers to look for obvious errors in advance of compiling the code. This saves time and generally makes programming more efficient. In some cases, a syntax checker will even indicate problems that would compile successfully but would not work correctly at runtime.

Syntastic example

Figure 6: Syntastic example (https://github.com/vim-syntastic/syntastic/raw/master/_assets/screenshot_1.png ) 

Switching between source and header files

C++ programmers generally work in multiple files for a single project but even when the codebase is quite small, there are virtually always going to be C++ (.cpp) files and header (.h, .hpp) files. In Vim C++, moving between the main source file and a header file is as simple as changing windows if they are both available. However, depending on how many different text or code windows are open, it can be painful.

Two extensions that can be used to facilitate this switch are Alternate Files quickly, sometimes referred to as A, and FSwitch.

Other interesting and relevant extensions

In addition to the aforementioned extensions, myriad others are available to fine-tune or improve your Vim C++ coding experience. Some examples are:

  • Snippets: These are small blocks of code that are used often enough to warrant having a quick key combination set up to include them. Two for Vim C++ are UltiSnips and snipMate. In the example below, the user types “if” and then presses the TAB key, which is recognized by UltiSnips and it has reacted by completing the if() block and positioning the cursor inside of the parenthesis, leaving the user in the correct place to continue entering the “if” logic.
    UltiSnips example
    Figure 7: UltiSnips example; type “if” then press tab to build the if() block and position the cursor
  • Refactoring tools: When you want to standardize without changing functionality, try vim-refactor or Refactor.
  • Jumping to specific definitions can be accomplished using Ctags.
  • Quote, parenthesis, and bracket matching meet other helpful features in delimitMate.
  • If you want to try a graphical debugger, install the vimspector

For a more comprehensive set of features in a single extension, try lh-cpp.  This extension includes smart snippets for bracket pairs and control statements, some templates, syntax highlighting, and an API to build wizards and more complex features.

These are but a mere sampling of the many extensions that are available and ready to use, and many of them have a lot of good contributors that want to make your life as a programmer easier. The best thing to do is take a look, try one out, and see if it fits your style! 

Where Do I Get Vim?

Vim is freely available for a variety of platforms on the product’s download page, where the current version and several historic versions can be downloaded. Windows users that do not want to install the application can run a portable version from the PortableApps gVim page. For Unix systems, the developers suggest compiling directly from the source. The most recent version is available for Git users on GitHub, and there is a repository for Mercurial if you prefer (for info regarding Mercurial vs Git click here).

The terminal version of Vim comes preinstalled on Mac, although it is normally behind the current release in terms of features. A GUI-based, actively developed version that is popular among Mac users is MacVim. 

Vim and Visual Studio

For those developers who are already comfortable with Vim and are working with a team that is using Visual Studio, there is an extension available in the Visual Studio Marketplace called VsVim. 

In conclusion…

Vim C++ is a powerful text editor that is popular among system administrators and programmers. While it is not a full-featured word processor, nor a complete IDE for code development, it makes up for this with improved speed and performance. Users who are skilled with keyboard-based input are highly efficient at manipulating text, and there are special features available for programming languages such as C++ that are utilized via the extensive plugin system. It might seem a little daunting to start but there are plenty of tutorials, and once you get started, it’s easy to learn.

speed up c++

Dori Exterman
Dori Exterman reading time: 9 minutes minutes October 4, 2021
October 4, 2021

Table of Contents

Related Posts

9 minutes Rust vs C++: Is It Good for Enterprise?

Read More  

9 minutes Visual Studio vs Visual Studio Code: A Comprehensive Comparison

Read More  

9 minutes A Comprehensive Guide to E2E Testing

Read More