Advent Calendar 2020 - Vim Tips

In the spirit of [using unix as an IDE](https://sanctum.geek.nz/arabesque/series/unix-as-ide/) I’ve been using Vim as my editor of choice for the last 10+ years or so. In this document I’d like to showcase some of my favorite Vim features and other useful Vim tips I’ve come to know.

## 1. Use ctrl-z to go to shell

This might be obvious to some, but for me, it was a whole paradigm shift when I first realized I could use ctrl-z to temporarily get out of Vim and back into my shell.

Whenever I finish editing a file and want to compile or stage/commit some changes, all I need is to hit ctrl-z. No need to look for the right terminal that might be hidden in a stack of other terminals. A single key stroke and I’m right where I need to be!

![ctrl-z.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/924037/05211bc7-70ff-e8fa-2336-1338ead1b823.gif)

## 2. Use ctrl-[ to exit insert mode

Most Vim users will eventually want to change their way of exiting insert mode to something more accessible than the escape key. This can be done by mapping the `jk` key combination or something similarly uncommon to escape. However Vim actually provides, in my opinion, a much better option: the ctrl-[ combination! Easily accessible and available by default!

## 3. Edit and navigate code by paragraphs

One of the key strengths of Vim is that you can edit code structurally rather than textually. What I mean by this is that instead of editing and navigating character by character, I can edit by components of the code!

Vim calls these components [“text objects”](http://vimdoc.sourceforge.net/htmldoc/motion.html#object-select). Most users know of the “word” objects, but in my opinion, the paragraph object is much more useful for coding.

I use paragraph objects in primarily two ways. The first use is to navigate code using `}` and `{` to move forward and backward respectively one paragraph. Paragraphs in code roughly translates to functions, or if not functions, chunks of code that achieve one thing. This means I can easily move across the file by stepping by the functions in it.

The second use is to move functions or blocks of code around. To cut a block (including the trailing newline), use the `dap` (read "delete a paragraph") keystroke, use `}` or `{` to go to the new position and simply `p` to paste it. In Particular this is very efficient for reordering functions.

![paragraph_fast.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/924037/54207481-09c8-d544-5dca-e77f1119d56f.gif)

## 4. The “pairs” text objects

Another very useful set of text objects are those that work on pairs, that is quotations (`“`, `‘`) and brackets (`()`, `{}`, `[]`, `<>`). These come in two flavors, inner (e.g. `i”`) and outer (e.g. `a[`, read “a bracket”) where inner selects excludes the delimiter and outer includes the delimiter.

![pair_fast.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/924037/56a66bb2-c30d-6a06-8796-01970503a391.gif)

## 5. The % movement

Although technically not a text object, but a “movement”, it’s very much in the same spirit. It finds the next bracket or the bracket under the curser and jumps to the matching bracket. For example, if the cursor is at the beginning of a function call, it will jump to the closing bracket of the call! This is probably my favorite "text object" and extremely useful for refactoring code, for example when replacing an inline function call with a helper variable.

![func_fast.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/924037/c728e8a1-a872-3119-a200-f0cb21c54053.gif)

## 6. The power of dot!

Arguably the most powerful feature in Vim is the `.`! My favorite command. In short, it’s a “mini macro”. It re-does your last “text change”, i.e. roughly the last delete, change or insert. This can be extremely powerful in combination with other Vim operations. Technically less powerful than normal Vim macros but because there’s no “record step” to it, it’s so much easier to use.

Let’s say for example you want to chain another method call to some arguments of a function call, do `A<backspace to remove comma>.some_method(),<escape>` and move to the next line to reapply.

![dot_fast.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/924037/182f71b2-2845-48ba-3a3d-3a8c234abbac.gif)

Another combination I use is with the `gn` (next search match) text object. I combine this with `c` to change the next search match to something and repeat using `.`. It’s basically the same as the `:%s//replacement/c` command but it is slightly more interactive in that you can still navigate and edit text as you go and replace matches.

## Conclusion

These features might not be the most obscure or hidden features of Vim but I think they are among the most useful and part of what makes Vim stand out from other text editors. If you have other suggestions for Vim features that are fundamental, please share in the comments!