Vim Workflows

When I started learning Vim 6-7 years ago, I found all the micro-movements, such as 4j or d3w, to be completely insane. They didn’t seem to enhance productivity at all. The time it would take for me to count the number of characters was probably longer than just grabbing the mouse and clicking. I didn’t really understand why people would use Vim.

Now, I still barely use those micro-movements, but I have come to love how Vim enables me to craft my own workflows. I don’t know whether these workflows are the best ways to do things in Vim. I only know that they are good enough for me, and I can navigate/edit code quickly and accurately with Vim. Here are just some workflows that I used today at work… countless times.

Quickly getting back to the editing context

Problem

I’m writing code but forget the name of a symbol or a constant. I want to look it up and quickly go back to where I was.

Solution

If I know the symbol is in the same file, I will do the following:

  1. /search_pattern to search for the symbol name
  2. Yank the symbol name, for example, using yaw
  3. `^ to go back to where I was editing
  4. p to paste the symbol name

If the symbol is probably not in the same file, I will do the following:

  1. mM to create a global mark M
  2. Grep string interactively using fzf+rg
  3. Yank the symbol name
  4. `M to go back to where I was
  5. p to paste the symbol name

That may seem like a lot of steps, but it’s extremely efficient when dealing with a large codebase.

Delete or yank a containing block

Problem

I’m in the middle of a deeply nested code block. I want to delete or yank the root.

Solution

  1. Keep typing [{ to get to the beginning of the desired containing block.
  2. To delete, d%. To yank, y%

Visually select a long function

Problem

I want to visually select a long function easily without the need to carefully identify where the last closing bracket is.

Solution

  1. Start with the first line where the function is defined, press V to visually select the entire line.
  2. Keep pressing j until the line that contains the opening bracket.
  3. $% to move to the closing bracket. Note: $ because most functions in my codebase have the body on a new line after the opening bracket.