Like so many other programmers, I’ve come to love Vim. I’ve always had a deep respect for the efficiency of Vim, but it wasn’t until I combined the power of Vim text editing with the agility of CLion’s code navigation engine, that I really feel in love. Vim integrates so seemlessly with CLion that I’ve completely forgetten that it’s a plugin and not just a basic part of the tool.
The purpose of this post is to demonstrate how Vim code editing works in conjunction with CLion code navigation, not to act as a complete Vim primer. If you’re new to Vim and want to learn more, please see the links at the end of this post for some great resources on the web.
Let’s begin by editing some STM32F4 driver source code as an example. HAL_Delay
is a pretty useful function that’s weakly defined in stm32f4xx_hal.c
.
Suppose we’re not entirely happy with the default definition of this function and we want to modify it in some way. To do this, we’ll need to copy this function to another file (let say the bottom of main.c), remove the __weak
attribute, and then make our modifications. The new strongly defined definition will override the weakly defined version so that we don’t have to modify the library code directly. One traditional way of accomplishing this task might be as follows.
-
Use the mouse to highlight all 15 lines of this function.
-
Press
⌘c
to copy the lines. -
Use the mouse to navigate the directory tree to find our destination file and double click on it to open it.
-
Use the scroll wheel to navigate to the bottom of main.c or click on the slider at the right edge of the file and pull to the end.
-
Press
⌘v
to paste the lines. -
Use the mouse to put the cursor at the end of the
__weak
attribute. -
Press the backspace key 6 times to delete the attribute
__weak
.
For any Vim user, this would be like nails on a chalkboard. Using CLion and Vim, the same result could be accomplished with the following keystrokes.
15yy ⌘e main ENTER G p dw
For a non-Vim user, this might seem like utter nonsense, but the thing is, when read like a sentence it’s very easy to digest. Let’s break it down.
-
15yy
: First we want to yank the 15 line functionHAL_Delay
, we accomplish this with the command15yy
. The commandyy
just means yank line (or copy line) and the preceeding number specifies the number of lines to yank. y means yank, which is easy enough to remember. -
⌘e main ENTER
: Now we invoke CLion to take us to main.c.⌘e
opens the recent file list. This command is used so frequently that it will be engrained after a day’s use. Typing main automatically filters the recent file list andENTER
select main.c. -
G
: In Vim, capitol G moves the cursor to the bottom of the current file. Another frequently used command. -
p
: Paste the 15 lines that were yanked. A little more intuitive than, say,⌘v
. -
dw
: Delete the word__weak
, d for delete, w for word.
By reducing the mechanics of code editing and navigation to basic keyboard commands, CLion + Vim allows you to focus on what really matters, feature development.
Installing IdeaVim
Vim is easily installed as a CLion plugin. Go to CLion > Preferences (or press ⌘,) and find the Plugins tab. Search for IdeaVim.
Like Vim, IdeaVim uses a resource file, which is stored in the user’s home folder. This file is only necessary if you want to change IdeaVim default settings. Enabling relative line numbers is the only change that I would highly recommend. With the relative line option, each line in your file is numbered relative to the cursor’s current position. We used this feature in the example above when we noted that the function HAL_Delay
was 15 lines long.
To enable relative line numbers, create a file called .ideavimrc in your home directory (usually ~/). Add the following line to the file.
set number relativenumber
Distraction Free Mode
For those wanting to take it one step further there is Distraction Free Mode. This is not necessarily related to Vim, however if you’re already used to running Vim from the terminal, you might find the file explorer and tool bars rather unnecessary. I would agree. When I’m working with a project that I’m very familiar with, I will typically run CLion in Full Screen & Distraction Free Mode. All of the benefits of the terminal plus all of the benefits of CLion.