7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Advent Calendar 2020 - Vim Tips

Last updated at Posted at 2020-12-03

In the spirit of using unix as an 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

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”. 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

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

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

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

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!

7
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?