19 minute read

Ah, Vim. You little beast of a text editor. Yes, I write blog articles about text editors now.

So what does a text editor do, exactly? Well, everyone uses it to write text into a file, typically a .txt file. But, if you work with programming languages, like me, then you use these editors to write your code, and some of them have nifty little features beyond simple text editing to help you fulfil that purpose. For instance, some editors colour different parts of your code in different colours to make it look more readable while you write it, something we call syntax highlighting. Other times, it may have an option to enable or disable line numbers, so you know where you are when you write your code or other text. Some more advance text editors may even allow you to enter a keyboard shortcut while you type, showing you a bunch of suggestions to help you complete that line of code, of which you select one to complete that line of code. Aptly enough, we call that code completion!

Of course, the most basic text editor most of us are familiar with on Windows is Notepad, which is basically a bare-bones text editor that lets you write text to text files ending in .txt… and that’s pretty much it. It is very fast and serviceable for what it does, but it has none of the features I mentioned in my previous paragraph. Linux Mint Cinnamon’s default text editor, xed, does have what Notepad has on Windows, as well as syntax highlighting and even line numbers, but no code completion (as far as I know) and few, if any, other advanced features that I didn’t mention before. However, there are other options out there that you can download for any OS, and the one I use is VSCode/VSCodium. I’ve been a long time user even before my switch to Linux (though I only went to VSCodium after I switched). It’s a text editor at its core, with many advanced features that separate it from most basic text editors, bringing it a bit more in line with what we would call an Integrated Development Environment. There are other midways between text editors and IDEs like VSCode/VSCodium. Atom and Brackets were popular choices before GitHub and Adobe respectively killed them off and lent them to their communities to maintain themselves, and Zed’s a new kid on the block that’s currently in active development but seeing constant progress, but for the most part, I pretty much stick with VSCodium for my day-to-day work. I enjoy its rich features and plugin ecosystem (the Open VSX Registry has come a long way since I first started using it). It allows me to work on this website, for instance!

Most of the time, I can give or take VSCodium’s long start time and reasonably moderate load on my system’s resources. For the recent project I did to do with software sustainability in MDE, however, it eventually started to get in the way of me obtaining the most accurate readings possible. For each test, I changed the value of a variable or two within the code itself using the editor I chose to use, then I had to run the code in the terminal, and leave a system resources program running in a separate window to simultaneously measure 3 different things in order to input their values in a formula to calculate the code’s estimated energy consumption in kilowatt-hours. Those 3 things were:

  1. The memory usage (in GB, rounded up/down).
  2. The CPU usage (as a percentage).
  3. How long the program actually took to run in milliseconds (this was done via the code itself).

So I needed a new approach for this project where I could quickly change the value of the variable(s) I needed to change (within the code itself) without having too much of an impact on my system’s resources, so as not to disrupt the tests themselves. An increase in speed would definitely help a lot too. That’s where a terminal-based text editor would come in, and while there are several ones out there, most notably GNU Nano and GNU Emacs’s TUI mode, the editor that reigns supreme in this niche… is Vim. And also Neovim.

Vim stands for “Vi improved”, as it was originally based spiritually on Bill Joy’s vi editor, originally released in 1976, without using any of vi’s code. It’s essentially a rewrite of vi, originally based on an earlier vi clone called STEVIE for the Atari ST, with the same functionality and added extensibility through the “Vimscript” domain-specific language, used to make plugins that are freely distributable and can be installed onto Vim to provide additional features to it. The original vi was quite popular among the nerdiest of computer users in its heyday, especially those who used UNIX or a UNIX-based operating system, but Vim eventually took over in popularity and is now far more widely used, even today. It was originally released to the public in 1991 by Bram Moolenaar, who sadly passed away in 2023, and may he rest in peace. Christian Brabandt is now Vim’s chief maintainer, carrying on the legacy Moolenaar planted with Vim.

Oh yeah, how could I have forgotten, the first thing many many Vi beginners know about Vim: You cannot escape Vim. You type in vim in a terminal, press Enter, and all of a sudden you’re greeted to this strange looking window with a 1 in the top left corner, followed in that same column by a bunch of tildes. You try to type in some words, and then fair enough, you’ve got some characters on your screen… but then how do you get out of Vim? Is it even possible? You wonder all that to yourself as you waste away countless hours of your life staring at the screen mashing any key combination you think will work… but nothing works! You’re now locked in a digital prison as a seemingly innocent text editor has entrapped you in its confines with no means of escape! You now pray for mercy desperately as Vim takes over your life! A menagerie of mad voices enter your head braying “You cannot escape Vim! You cannot escape Vim!” You have nowhere to go! There is nowhere to hide!

…I’m just jesting! It’s not as bad these days, now that you can just, well, close the darn window and open a new one! Though, even then, working with Vim for the first time can be considerably daunting and you can get lost quite easily, as with most powerful software (I’ll show you how to escape Vim at the end of this article). Vim really isn’t like most text editors! It’s got more than one “mode” of editing (two of which I will discuss in this article), and the way things are done is organised in such a way that it isn’t the most visible or intuitive at first, but once you get more acquainted with Vim, it’s meant to really enhance your speed and efficiency in writing and editing code and other text. I’m pretty sure it could be used for purposes other than programming, but more often than not, Vim’s mainly used by programmers like me, so please take that into consideration as you read the rest of this article, as I always try to take you into consideration whenever I write these articles!

Oh, wait, I mentioned Neovim earlier, didn’t I? Well, Neovim is essentially a fork of Vim created in 2014 to add additional functionality to Vim while still retaining the distinct Vim style (for the most part). While Vim and Neovim have diverged over the years, for the most part they’re still fairly similar, with Neovim providing more advanced functionality and aiming to keep Vim more maintainable in the long-term. Neovim also adds support for writing plugins in the general-purpose Lua programming language (commonly found embedded in games for modding purposes).

So I decided to use Neovim. My friend Kab, who now has a YouTube channel and a blog of his own (both of which I’ve linked to before in the past), has used both Vim and Neovim (he’s partly why I’m a Linux user nowadays, but that’s a tale for another time). He generally tends to prefer Vim these days, but I still decided to install Neovim anyway because I feel that, for what I’d want to do in the future, it has a few more features that I think would come in handy (but are outside the scope of this article). Plus, as a beginner, I don’t think I’ll be in immediate need of any Vim and/or Neovim plugins for now, but if the need for a Neovim plugin ever arises, then I could always either search for one online or, after I get better at and more acquainted with Lua, write my own!

So what did I use Neovim for? Well, you know how, every time you change the independent variable of your scientific experiment—the one thing to change in your experiment that doesn’t depend on the other factors you are measuring (because those factors, the dependent variables, depend on changes to the independent variable)—you have to make sure that every iteration of the experiment starts under the same conditions as before? It’s to make sure you can get the most accurate data possible, and it was becoming harder and harder with VSCodium to keep system resource usage constant across all my tests, as VSCodium would often be doing while I was running it idle. I’d always see some weird spikes in my CPU usage that got in the way. I actually got them a lot of the time, even with the OS by itself running idly, but it got more and more frequent with VSCodium running in the background. With Neovim, I could just open it with the same terminal I used to run my code (I don’t even need to open a new window), then scroll down to where my code was, go into insert mode (by typing the I key), then changing the value(s) I needed to change, then go back into normal mode (by typing Esc), then saving (:w!), escaping (won’t tell you that yet ;-)) and re-running my tests. And that was the first glimpse I saw of what Vim/Neovim could be used for: its efficiency. Even with the extra features, Neovim is incredibly lightweight, using very little system resources whenever I make the changes I want to make. As I said earlier, it’s not a consideration I have to make most of the time, but when I need to make that consideration, I’ll go with Neovim over basically anything else.

Only later on, however, did I start to take advantage of what a lot of users use Vim/Neovim for: its speed. See, Vim/Neovim has 7 modes when it comes to editing, but for this article I’ll only go over 2 modes:

  1. Insert mode, for typing in text.
  2. Normal mode, for typing in commands.

By default, Vim/Neovim opens in normal mode, but you can go into insert mode by pressing i and typing. You can then go back into insert mode by typing Esc and run certain commands. At first, I scrolled down every time I wanted to change the line of code I needed to change, and I lived with it, but at some point I wondered “Vim’s known for its speed. Surely there’s some way to go straight to the line I need.” Sure enough, a quick Google search later: :<line_number> (for instance, :50). So I could remember the line number I needed to change, and every time I opened the code file in Vim/Neovim, I could immediately type :<line_number> in normal mode, then go to that line and enter insert mode. That alone saved me a lot of editing time. I don’t think I could’ve done things nearly as quickly and efficiently without it. As should be implied, I’m still very new to Vim/Neovim, so I hadn’t achieved the most optimal usage of it at the time, and even now I’m still learning (more on that in a bit).

For that matter, that wasn’t even the last thing I learnt about Vim/Neovim at the time. By default, at least for me, the editor does not show line numbers in the margin on the left, and it was getting a bit hard for me to have to guess which line I had to edit every time (at least until I could remember the specific line number after successive edits), so I was wondering if I could enable such a thing in Vim. A quick Google search later: :set number or :set nu. To enable them permanently in Vim, one can edit ~/.vimrc and add that line to it, or, if you’re using Neovim (like me), you could edit ~/.config/nvim/init.lua and add the following line:

vim.wo.number = true

I did the latter, since I used Neovim, and sure enough, line numbers at every Neovim session, which made it easier to search through code and remember which lines I needed. I always found that quite hard without visible line numbers.

Oh, by the way, if you’re wondering what that tilde sign ~ refers to, it’s basically shorthand for your home directory /home/<username>/, where your Documents, Downloads, Music and other relevant folders are stored (similar to how Windows does things: C:/Users/<username>/My Documents, C:/Users/<username>/My Videos et cetera).

With those two things, the :<line_number> command and line numbers in the editor by default, I could quickly and effortlessly make the changes I needed to make before I ran each successive iterations of my tests. I know it’s a very small purpose to be using Neovim for, but sometimes it’s really the little things that matter. In what would’ve taken me about a minute or two in VSCodium for that purpose I did with Neovim in seconds. It was then that, for the time being at least, Neovim’s supposed speed and efficiency both became less supposed and more concrete to me. Despite knowing very few commands and shortcuts, of which there are so many, I was seeing the benefits of Vim for developer productivity by actually practising them. And then not only was I proud of myself, but I was having fun!

Yes, when I used Vim, I ended up quite enjoying it. I enjoyed the fact that the commands I knew at the time were more than enough to get me through preparing each test, and at some point I just enjoyed entering those commands! As I said earlier, Vim/Neovim is effectively a completely different environment for handling text of any sort, and because it’s so different to writing any text in almost any other editor out there, be it Notepad, Notepad++, xed (Linux Mint’s built-in editor), VSCode/VSCodium, Sublime Text or even Emacs (it took me this long to bring up Emacs in an article about Vim 😅), it does seem weird at first. People edit their code this way? Well, some people do, believe it or not! And once you get used to it, it becomes part of your life! I did not feel so productive while editing any kind of text until I started using Vim to do so, and I started enjoying this different editing environment that I was getting productivity gains out of. I was enjoying being productive!

Heck, I’m still learning things even now! Until I started writing this article, I didn’t really use Neovim much after my funded project, but I decided to start writing bits of this very blog post in Neovim. Yes, you can do that! And it should be well suited to writing blog posts (once you know how to use it, of course), but while I was trying it out, I was wondering where to find a good spell checker plugin for Vim and/or Neovim, as I recently started using a LanguageTool plugin on VSCodium. A Google search later, I found that you don’t even need a plugin! Vim/Neovim comes with its own spell checker to be used within Vim/Neovim! Just type the following command in normal mode and you’re good to go:

:set spell spelllang=eng_gb

I may still search for a LanguageTool or other more extensive spell checker plugin for Vim and/or Neovim later, but I’m not in immediate need for such a plugin, since Vim’s built-in spell checking features cover most of my needs. The spell checker highlights text (instead of underlining it) that doesn’t match, and I don’t know how to change that (at present), but I don’t really mind that either. In a way, it kind of works for Neovim and what I used it for, especially since I usually like to have translucent (partly transparent) terminal windows, so the opaque highlighting makes the text detected by Vim’s spell checker a bit more visible, especially when the terminal window’s over a white background of sorts (i.e. a webpage opened on Firefox with a white background) that makes the rest of the text harder to read than it normally is.

And that wasn’t all I learnt! When I wanted to go to the end of a long paragraph in my blog post, I learnt to type $ in normal mode to get me there, which saved me minutes (maybe even hours) of pressing and holding down the right arrow key for prolonged periods of time just to get to the end of the paragraph I wanted to go to the end to. Again, just one of many shortcuts designed to optimised the way you write code and other text in Vim!

I’m still using the tried-and-true method of “search up specific things when you need them”, as demonstrated in other parts of this article, but at some point I plan to learn Vim/Neovim in a more extensive manner, such that I, at the very least, have all the basics covered and can use the most common commands for most of my writing and editing. I know there’s a :Tutor command you can run on Neovim (in normal mode, of course) once you start a Neovim session to open up a written tutorial to read and practise through, and that’s one way I am considering learning Vim/Neovim more extensively and with more focus. With Vim you can just type vimtutor in the terminal to open the Vim tutorial straight away, but I had to set up an alias in ~/.bashrc to have Neovim mimic this behaviour:

alias vimtutor='nvim -c Tutor'

For the uninitiated, ~/.bashrc basically gets run every time a Bash terminal in Linux is opened up (I already went over what the tilde ~ refers to earlier). An alias, in this case, refers to a shorthand command that can be typed into a terminal session to run a different command, as I have demonstrated above. The command I set the alias to basically runs the :Tutor command after Neovim opens.

I’m sure there are uses of Vim/Neovim for the non-developers who read this post. After all, I wrote part of my blog post with it (I alternated between VSCodium and Neovim)! I guess it’s just finding them. After all, there’s Vimwiki, a Vim plugin for building wikis and/or taking notes (I’m sure it should work with Neovim too), and I’m sure there are other things too, for writing and other non-programming purposes. I did a quick Google search (just a very quick one) and found that some people did, indeed, use Vim/Neovim for things other than programming and/or software development.

For people like me, though, finally getting used to Vim/Neovim feels like finally pulling the sword from the stone it was stuck in. I have a newfound appreciation for the productivity gains I get. Somehow changing between the two modes I knew at the time helped me write faster than writing in one single mode using VSCodium and its own shortcuts. It’s far more efficient not just with editing but also with my system resources. VSCodium’s requirements are fairly small, but I could probably run Vim, and also Neovim, on a potato. I could probably get VSCodium to run on a potato as well but (1) I haven’t looked up how to do it and (2) I’m not really sure if it would run as smoothly and effectively as Vim/Neovim would.

And yes, I know there are other Vim forks and derivatives out there, such as Kakoune and Helix Code, but Neovim was the first of them that I found and, for what I want to do, it does the job and does it perfectly.

And yes, I’m also aware of Vim and Neovim emulation plugins for VSCode, and yes, they’re installable in VSCodium too. I haven’t got around to getting either of them to work properly yet. That’s definitely a future undertaking for me, though it’s worth noting that I would lose the lower system resource usage benefit this way, and that’s partly why I used Neovim for that funded project.

For now, though, I’m happy to cycle between xed (Linux Mint’s default editor), VSCodium and Neovim as my language-agnostic editors of choice (yes, I use more than one tool for reading and writing code; that’s a tale for another time, though). I like xed for its simplicity, speed and built-in syntax highlighting, and the fact that it comes with Linux Mint is also a bonus (it’s way better as a “built-in” editor on Mint than Notepad is on Windows). I like VSCodium for its syntax highlighting, other built-in features (lots of them, such as a built-in terminal) and extensibility beyond built-in features using its rich plugin ecosystem (even if a VSCode plugin I want isn’t in the Open VSX Registry, I can always download the VSIX file from the Visual Studio Marketplace website and extract it into ~/.vscode-oss/extensions). Finally, I like Neovim for its editing speed, extremely light system resource usage and the features it has despite its small size (of course, Neovim has extensive syntax highlighting support, but I did not know it had its own spell checker). It’s got a learning curve, sure, but with what I know about Vim/Neovim already, I’m able to do quite a bit, more than I thought I’d be able to do.

Even so, I’m still on the path of learning the commands I need to work with Neovim, as well as its various editing modes. And I still have yet to explore Vim and Neovim’s own plugins and plugin ecosystems! I’m sure if I need something Neovim cannot innately provide (yet; it’s still being actively developed), I’m sure I’ll be able to find a plugin that will do what I need, much like VSCode’s and VSCodium’s plugin ecosystems do for me at the moment. Truth be told, I do mean to learn a lot of other things too, but, even with all those things still in my backlog, I’m hopeful that my journey with Neovim won’t stop here!

Oh, and you’re probably still wondering how to escape Vim. The generic command (in normal mode) is :q, but the following 2 are more commonly used:

  • :q! to exit without saving.
  • wq to save and exit at the same time.

There, you now know how to exit Vim. Take that information and keep it with you!