Neovim: the best code editor for many, potentially for you too
Posted: August 27, 2024
Introduction
Getting started with Neovim can be somewhat daunting, vim’s modal editing and motions are notoriously difficult to learn. Why do people still go through this process and advocate it for others? (Like I’m doing now.)
Even though Neovim can feel a bit hostile in the beginning, the editor gets friendlier as you get more familiar with it. As you learn to use, extend, and configure Neovim, it will change and grow alongside you, and you will end up with something right for you, with as much simplicity or complexity as you want. In turn, this will also make your text editing much more efficient and enjoyable.
In this blog post, I will outline some of my reasons for and struggles with learning to use Neovim. I hope that this will also help you decide whether it could be the text editor for you.
This blog post is the first of a two-part series on Neovim. The second post will explain how to configure Neovim from scratch and provide a minimal working example of a functional editor for coding.
The universal experience with IDEs
IDEs excel at making programming accessible by offering familiar interfaces, and click-to-play buttons to write and execute code.
This is especially important when you are just starting to learn to code: writing and running your first "Hello world!"
should be simple, and IDEs should assist you with this by being intuitive and unobtrusive.
Once you become more proficient, learning more of the features and shortcuts of your IDE can also become a logical secondary goal — mastering your tool of choice will make you a faster and more well-rounded coder.
IDEs come with an abundance of features, each of those are probably useful for at least one person. On the other hand, no single person utilizes every feature; most people only rely on a small subset of them. Hence, features considered bloat by one person will be useful to another, so IDEs need to include them to be a fit for a wide user base. Even if this is an imperfect fit for many.
An alternative
Neovim’s approach is different: even though it packs several features itself, its core is much more bare-bones. It is more of a text editor rather than an IDE on its own.
That said, thanks to the active plugin development community, there are countless extensions available to turn it into your personal development environment. It’s fairly easy to find and install widely used plugins that are often more complete and provide a better alternative to corresponding features in any IDE. More often than not, these also follow the Unix philosophy of “doing one thing well”.
While plugins can enhance Neovim to match the features of IDEs, Neovim’s unique advantage lies in its different approach to editing text.
Optimizing editing
Unless you are a large language model, writing code is rarely just one continuous linear flow of thought followed by typing out the exact code that solves your problem. A large part of writing code really is editing code: lots of iterations with jumping back and forth between parts and repeatedly making smaller or larger modifications.
Vim’s modal text editing with its operators and motions was designed to make this type of work efficient. They offer a new perspective on what navigating and editing code can be like. The advantage of using this approach might not be immediately apparent for newcomers, it is a vague promise after all: “How will this make me more efficient?” — one could ask.
What is efficiency?
A clear example of an objective metric for efficiency is the number of key presses needed for specific editing tasks: achieving the same result with fewer key presses is more efficient. While Neovim does offer an advantage by this standard, its true impact goes beyond this in less quantifiable ways.
This impact comes from the feeling of being more engaged, and in general having more fun while editing code, making it easier to stay in the flow. Editing code will feel nearly effortless, more mentally engaging, enjoyable, less repetitive, and less fatiguing.
This will boost your efficiency — by expending less mental energy on your editing tasks, you will have more capacity to focus on the more challenging elements of your work.
In hindsight, my old way of editing code was taking so much more mental effort. I never really realized this though, until I became somewhat proficient with Vim’s way, and made the switch to always using modal editing.
An editing progression example
This example tries to illustrate what editing can feel like with a few different methods ranging from beginner to advanced, when it comes to a simple editing task.
Let’s say you want to change the function name in this block of code:
|def foo():
print("hi Mom, hi Dad!")
To do this, at the very beginning of a coder’s journey, one might naively press this series of keys:
(Or even worse:
Then after a while, one might realize that using Ctrl
and Shift
can speed this up a bit:
On the same note, but with even fewer key presses (although these combinations still feel quite cumbersome):
With Vim, the same result can be achieved with just these (while thinking about the mnemonics: “Let’s go to the next word and change that word”.):
This was a simple example, and for more complex tasks vim will allow you to be even more creative and come up with some intuitive and clever ways of doing them efficiently.
Mouselessness
Vim also encourages you not to use the mouse at all. Everything is only a few key presses away, and there’s no need to interrupt your flow by switching between your keyboard and mouse.
The mouse pointer is an okay way to navigate and select between the fixed set of things on your screen, but can we do better?
Imagine being able to rename a file just by changing a word in a special text file (technically called a buffer in Vim.) Or navigating your file system with the same set of keys that you already use for navigating text in Vim. Or accessing all of the debugging features with much nicer key combinations.
With the magic of modal editing and the help of some Neovim plugins, you can do all these and much more, without the need to use your mouse.
Learning Vim motions
To get familiar with Vim motions, a good starting point is Vimtutor, an interactive tutorial that will help you get familiar with the concepts of editing text in Neovim. You can launch Vimtutor from inside Vim by typing this:
:Tutor<Enter>
The tutorial will walk you through the basics of editing with neovim, and you can learn from examples in this by doing them.
Changing perspective
Making the switch from another IDE takes quite a bit of mental effort — you are learning a new way of editing code/text after all, and essentially changing how you think about this process. But as with many skills, this will become easier and automatic after a while. Until that happens though, you are going to have to pay the cost upfront: a dip in productivity, feeling frustrated, getting more exhausted sooner, and you won’t be sure whether it will pay off in the end.
A sincere form of flattery
All mainstream IDEs offer a way to emulate Vim motions either natively or with an external plugin. By intermittently enabling this feature, you can spread the cost in productivity over time and get used to the new editing model gradually.
By practicing the motions in a more familiar environment, you can keep most of your habits of using your IDE. And when Vim emulation feels too much, you can take a break by turning it off for the day. This allows you to control how smooth or challenging you make this shift.
Fully switching to Neovim
You can experience the power of Vim’s unique editing model, even when using your IDE’s emulation.
But you can go even further in reinventing your workflow by fully switching to neovim. With neovim, you can have complete control over your features and customizations.
You will have the freedom to select and install your specific set of plugins with the features you want and create your own custom keybindings to make the most out of these plugins.
This process will also deepen your understanding of how your tool works under the hood. You will know all of its features by actively participating in installing and configuring the plugins. Plus, because you’ve set up the features and keybindings yourself, they will be intuitive and aligned with your workflow.
It’s also easy to revise and adjust your setup once every while — adding new features or removing those that you no longer use.
Your tool can be a precise reflection of your preferences.
Next steps
In the second post in this series, you can learn more about the configuration and plugin ecosystem of neovim by creating a minimal but functional Neovim configuration from scratch for coding.