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:
/search_pattern
to search for the symbol name- Yank the symbol name, for example, using
yaw
`^
to go back to where I was editingp
to paste the symbol name
If the symbol is probably not in the same file, I will do the following:
mM
to create a global mark M- Grep string interactively using fzf+rg
- Yank the symbol name
`M
to go back to where I wasp
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
- Keep typing
[{
to get to the beginning of the desired containing block. - 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
- Start with the first line where the function is defined, press
V
to visually select the entire line. - Keep pressing
j
until the line that contains the opening bracket. $%
to move to the closing bracket. Note:$
because most functions in my codebase have the body on a new line after the opening bracket.