Mouse-free

When I say I’m an IT consultant, what I really mean is that I’ve mastered Vim. All problems become so easy and quick to solve, because editing the files happens as if by magic.

And in an emergency, even without a graphical user interface. Here are some tips that should make life and work with Vim easier for all readers.

Version 9 of this best editor in the world has been released recently. Now is a good time to explore why Vim is great and consider what makes Vim 9 even better than previous versions.

Not too long ago I gave a short talk at the Munich Linux-Stammtisch about how I use Vim. Of course, one cannot talk about this often enough, hopefully something useful sticks. The slides from the Linux-Stammtisch are available here.

In this post, I pick up a few points from the talk and go into more depth about cursor navigation than the talk provided room for.

As few keystrokes as possible

Those who know Vim know that you can easily exit it by typing :wq. Alternatively you can use :x. Even a bit more efficient is :ZZ – that brings exactly the same result with one keystroke less. Two points to note here: Minimizing the number of keystrokes is the real goal of the Vim championship. The old Vim masters also count all types of key combinations as one keystroke each.

Where to find these old Vim masters? The best address is certainly vimgolf.com. There they compete and undercut each other in the keystrokes they need to solve complex tasks. But let’s take a step back.

Vim users typically switch between different modes, all of which have their strengths and weaknesses. When you open the editor, you are in “Normal Mode”. To enter text, you must first enter the “Insert Mode”. This can be done in many different ways. The best known is the print on :i for “insert”. (In fact, I don’t use the :i all that often for switching to insert mode.

I would especially like to recommend the :c and why that is so powerful is in the following sections).

You can get back by pressing the escape key. By entering : in Normal Mode you enter “Command Mode”, recognizable by the : icon at the very bottom left. In this mode you can enter instructions which will be executed only after pressing the Enter key (see :wq). For the sake of completeness, the “Visual Mode” should be mentioned here, in which you can mark and process strings or entire lines of any length.

Helpful mnemonics

If you like to work with mnemonic devices, remember now “append”, “change”, “substitute”, “replace” and “open new line”. In each case, the first letter (plus possibly a movement) puts us in Insert Mode. Different things happen to the character under the cursor as well as the current cursor position. With these commands there is also always a variant with the capital letter, which behaves somewhat differently.

The best thing to do is just to try it out.

But wait, there was also just talk of movement. Important: The change command expects a movement immediately after c is pressed. It then deletes all characters until the end of the movement and switches to insert mode.

What is this movement? What is meant is a cursor movement, i.e. a command that moves the pointer from the current position somewhere else in the document. Movement normally happens in Normal Mode and all commands allowed there are allowed after the c. In the simplest case, these are the cursor keys or the vimmier counterparts h, j, k, l. c deletes the text from the position directly before the cursor to the position directly after this character (one to the right of it) — or simply put, the character under the cursor — and causes a change to insert mode. c↓ removes the current line as well as the one below it and allows to replace it with new text.

Special movements

One of Vim’s great strengths, of course, is the variety of movements that make commands like change so powerful. For example, w and b move the cursor to the beginning of the next word or the previous one (“before”), e to the end of the current word. Round brackets lead to the previous or next sentence beginning, the curly brackets to paragraph endings. 0 and $ point to the beginning or end of the line. fy moves us to the next “y” (“following”), ty to the character directly before it (“till”). F and T do the same only backwards.

G takes the cursor to the top of the document, H to the first character on the screen, gg goes all the way to the bottom. If there is a bracket in the current line, % jumps to the counterpart. And then you can still jump to search results. n would be the next search result, N searches backwards. Speaking of which, you start searches with the forward slash /. The * looks for the next occurrence of the word under the cursor in the document, the hash # does likewise, only backwards.

If you want to jump to the beginning of a special line, you can do this with :N, where N corresponds to the line number. Thus, :42 jumps to the beginning of line 42. You can have the line numbers displayed with :set number.

Last but not least I can set marks and move between them. ma marks the current line as jump tag “a”, with 'a I can move there anytime. Jump marks that are labeled with capital letters are global, so they may open the file in which they are stored.

Seven paragraphs, please!

All movements or any commands can be multiplied.

A preceding number leads to a multiple movement: 7w jumps 7 words. And with this you can specify the c.

ct: lets you edit everything up to the next colon — very helpful in YAML files. c$ allows to modify all characters from the current position to the end of the line. And because this is so important and powerful, it is also available as a single keystroke: C equals c$.

Besides change, there are a number of other commands that expect movement. With a little practice, you can save yourself a lot of work here. And if you plan this far, from now on you write your documents in a way that you can use especially much from Vim commands. Sensibly chosen paragraphs are a good example. d} deletes everything from the current line to the end of the section (comes from delete). After that just navigate to the place where the just deleted passage fits better and paste it again with p (paste as in Copy & Paste).

One more: y3w copies the next three words into the (Vim-internal) cache. This comes from “yank”, c was already reserved for change. I can then insert this cache at any location using p. By the way, this memory is filled every time I delete something, even after a c2w I can insert the two deleted words somewhere else later. Example: cib""P encloses the contents of the brackets (within which the cursor must currently be) in quotes. The i here is a special movement, the exact meaning of which I’ll leave as homework, as will tracing this string.

New in Vim 9

We’ve come a long way and I hope there was at least one new insight for everyone. With Vim 9, not much has changed in what has been mentioned so far. The biggest new feature is its new scripting language: Vim9 script. For plugin developers this is a blessing, because the old Vimscript was – let’s say – unfamiliar, affects users probably only in the future. Vim9 script is also much faster. The rest of the improvements are more under the hood: better test coverage, more security and bug fixes. The new color schemes might be interesting and sort now takes into account the language settings.

Try the movements and look forward to Vim 9! When you are through, I recommend the topics Visual Mode, Macros and Registers for further progress. Zone-Folding I can also highly recommend. I also discuss a bit of this in SVA’s Focus On Linux podcast with Christian Stankowic and his colleagues.

And maybe we will meet sometime on vimgolf.com

The following two tabs change content below.

Jan Bundesmann

Latest posts by Jan Bundesmann (see all)