Kontakt

"Hands off your mouse!" - Exploring Vim in detail (Part 1)

by Theo on 31.01.2019

DISCLAIMER: Throughout this blog post you will find some illustrational example gifs. These gifs do not visually represent the vanilla Vim experience. The appearance of the editor has been heavily modified and tailored to my personal needs. However apart from that everything shown in these gifs can be done with a vanilla Vim installation.

If your are already familar with Vims configuration and are interested in what mine looks like, feel free to have a look: https://github.com/on3iro/dotfiles/blob/osx/.config/nvim/init.vim

What's in it for you?

TL;DR: Quit VIM via ':q' ... ;)

Ah good old friend Vim - worshiped by some, feared by many.

There seems to exist an endless amount of variations of "how to exit"-Vim jokes out there and
many people rarely get to know Vim deeper than this (apart from a few secret tête-à-têtes in git commits).
With the following series of blog posts I would like to move beyond these jokes and dig a little bit deeper.
What is Vim? Why does it do things the way it does? How can I profit from it as a programmer or writer?
Is Vim superior to all the other editors out there? (spoiler: No. We don't think in absolutes here.)

Before I start this series off with a quick overview of the topics I am going to address, I would like to put a bit more emphasis on the former point:
There are many many great editors and IDEs out there and in the end the only thing that matters is what works for you. Generally speaking: Don't obsess over tooling - be curious and never stop learning.
Vscode, Emacs, Sublime Text, IntelliJ (just to name a few) are all great tools, but this series is about Vim,
because its the one I fell in love with and because it does a lot of things very very differently.
So should you learn Vim? - Yes and no! Some basic understanding of a modal editor and Vim in particular
will definitely help you out in your career as a developer. Deep diving into all its intricacies however
is definitely exciting, but can be a bit of a bottomless pit. The only one to decide when to stop digging is you ;)

So by now you are probably wondering what you are going to learn from this series.
Here's a small overview of the topics I am going to address:

  • Vim basics (this is what the remaining part of this post will all be about)
  • Advanced Vim 1: Buffers, setting and jumping to marks, spell checking
  • Advanced Vim 2: relative line numbers, the dot and semicolon operators, visual mode
  • Advanced Vim 3: macros
  • Advanced Vim 3: Plugins and configuration, leader key
  • Bonus Entry 1: Vimwiki
  • Bonus Entry 2: copying and pasting into and out of Vim,
  • Bonus Entry 3: Dotbot + Git - an easy way to manage dotfiles
  • Bonus Entry 4: fzf

"Don't obsess over tooling - be curious and never stop learning."

The basics

Now with all that introductory stuff out of the way lets dive right in by answering who and what Vim might actually be good for. Generally speaking Vim caters to people who write, read and edit lots and lots of text, e.g. software developers, writers and students. While many tools do exactly that, Vim does it a bit differently (and even claims to do some parts better or at least more efficiently).

The main difference between Vim and most other common text editors is, that Vim is a modal editor. This means that Vim can be used in different modes with different functionality. The mode most resembling the traditional editor experience is called "insert mode". This does exactly what you would expect from a text editor. You can basically write and delete text like you are used to. You might expect this to be Vims default behavior when you start up the
program. But as some of you might already have guessed it isn't. Instead you will find yourself inside "normal mode" whenever you first open Vim.

"Normal mode" is what I would call the "bread & butter"-mode of Vim. You will spend most of your time in there when reading, editing and even writing text/code. The main things you do in "normal mode" comprises of moving and traversing your text file and editing parts of it whenever necessary. You do this by using combinations of "operators" and "motions". Both are basically keyboard shortcuts that can be chained and composed into more complex
operations - a bit like a small programming language on its own. If you ever find yourself outside of "normal mode" simply press 'Esc' to enter it again. Users of modern macbooks might find this pretty uncomfortable because of the lack of a physical button. Luckily there is another key combination you could use instead without additional configuration necessary. Just press 'Ctrl + [' and you will return to "normal mode" as well (assuming you are using the QWERTY keyboard layout - othewise you might be out of luck).

Now let us return back to "motions" and "operators". "Motions" represent movements on different scales and can be combined with a count. While you could move by single characters with the arrow keys or 'h', 'j', 'k' and 'l', some Vim users frown upon that. That is because it is often much more effecient to use Vims other motions doing bigger and more precise steps (remember: Vim users usually don't use a mouse). For example to move a single word forward, you would press 'w'. To move two words you would instead first press '2' and then 'w'. Moving backwards you would simply use the letter 'b' instead of 'w'. You could also directly jump to the start, middle or bottom of a file, or to the next occurence of a specific character on the line your curser is currently on. I wont go into further detail on each basic motion, as this would go beyond the scope of this introductory post. Instead I will point you to a very valuable source for learning these "motions" (and "operators" as well) by the end of this article, so stay tuned.

"Normal mode is [...] the bread & butter-mode of Vim"

Contrary to "motions", "operators" will actually alter the contents of your text file. Most of the time when you would like to edit something you will use an "operator" to do so. Most "operators" should be combined with a "motion". Lets say for example you want to delete a single word under your cursor. You simply use the 'd' "operator" in conjunction with the 'iw' motion. You might wonder what the 'i' stands for: 'diw' reads as "delete in word". So no matter where your cursor is at inside the word, it will fully delete it.
 Of course you can specify a count here as well. So 'd2iw' would read as "delete the two words".

Additionally you can even specify separate counts for your "operator" and your "motion" which will basically multiply the resulting command. For example '2d2iw' would translate to "delete the next two words, twice". Instead of combining an "operator" with a motion you can also double tap it, which tells Vim that you would like to operate on line level. Pressing 'dd' would therefore delete the line under your cursor (and save it to Vims internal clipboard). Adding a count to this "operator" works just as expected deleting multiple lines.

As with "motions" I wont go into further detail and show you each single one that exists. Instead I will now lift the curtain and point you to a very valuable resource to learn the basics of Vim: When you install any flavor of terminal Vim like Vim itself or NeoVim you will get an additional command line tool called "vimtutor". By executing this program from your terminal you will enter an interactive tutorial teaching you all the most important basics in about 30 minutes of your precious time. You will learn how to traverse, edit and save a file, how to search and replace text, how to undo changes aaand - drumroll - even how to exit Vim (there I did it...).

Have a look at vimtutor - it is more than worth it!

Which Vim to choose?

Speaking of Vim flavors: There is a huge range of possible options how you can use Vims basic features.
The most popular one probably is your common Vi(m) installation, which comes with most unix-like operating systems and can be started from inside your terminal. Another very popular option (which I am using myself) is NeoVim, a fork of the original. While NeoVim is still close to its ancestor there sometimes seems to be somewhat of a split in the community and both programs diverge a little bit with NeoVim being a lot more forward-looking, while Bram Moolenaar the creator of the original Vim tends to be rather conservative only adding new features every now and then to it.

Those who prefer to use an editor with an actual user interface instead of a terminal application might be glad to hear, that there also exists a GUI-version of Vim called GVim. While being a bit more convenient in some cases you should be aware that GVim is not only slower than its terminal counter part, but that you also loose a lot of Vims original power that comes with direct access to a terminal.If you don't want to part with your beloved IDE or editor you can still profit from many of Vims features.
Most of todays editors and IDEs either come with integrated Vim modes or at least provide them as an installable plugin. While many of these Vim integrations are not at all comparable to full fledged Vim, they still provide a lot of
useful features. Jetbrains products like IntelliJ for example provide a pretty good Vim integration, which even supports
some of the more complex Vim modes as well as macros (more about these in another post ;D).

Pros and Cons

Before I close this (already quite long article) I would like to summarize the good and the bad of Vim to provide a more concise overview: 

Good:

  • runs in a terminal and can therefore also be used on a server without desktop environment
  • has great performance and is very fast & responsive
  • extremely customizable to a point where it almost feels like an actual IDE
  • the "Vim mindset" can be transfered to many applications once learned and mastered (e.g. with some browser plugins it can be used to navigate your browser without mouse)

Bad:

  • Learning to configure Vim for more complex use cases can be difficult at first and might take a lot of time depending on your needs
  • Some IDE-like features are not as powerful like those of actual IDEs (on the other hand some are even more powerful)
  • The first time user experience can be a bit daunting
  • Developing your own plugins with vimscript can be a bit of a mess (although in NeoVim you could at least use Lua instead)

Vim is fast, responsive and extremely customizable

What's next?

One final question that might arise, is if Vim is useful for people that are not able to touch type. As you might already have guessed, Vim is a tool for keyboard power users and if you aren't using GVim or any form of IDE/editor-integration your mouse wont help you much in interacting with Vim. Therefore you need to be quite proficient with the keyboard. If you are unable (and unwilling to learn) to type without looking at your keyboard you will be rather unproductive with Vim. That said learning to touch type isn't nearly as hard as it might seem at first. I learned it by practicing about 20 minutes a day for about two weeks. So the time investment needed is probably rather small.

With these words I would like to close this article. Next time we will have a closer look at some of the more advanced features of Vim, like splits and tabs. I will also show you another very important mode you will probably use quite often.