Today I Learned

4 posts about #vim

Repeat last substitution in VIM

Use the & motion to repeat the last substitution in VIM. IE, the most recent :s command.

Very handy in a git interactive rebase that requires every commit message to have a ticket reference changed.

VIM: Move a numbered line to current cursor

Let's say I'm on line 20 and I want to move line 10 to my current line. Useful for code refactoring.

How I used to do it

10G
dd
19G
p

Or:

:10d
p

The new way I'll do it

Thanks to my learnings here:

:10m .

Range prefix to VIM command (from current line)

When I want to execute a command in VIM over a range of lines (for example, search-replace with :s) starting from the current line, I could execute a command like so:

:.,.+10s/foo/bar/g

This reads as search-replace from the current line (.) until 10 lines after. While useful functionality, it does require quite a bit of typing to execute.

Thankfully, there is a neat little shortcut that can be used to reduce some of this type. Simply enter the number of lines that you want to change from the current line and VIM will fill in the rest in the command prompt.

For example, I need to type 11: and VIM will insert :.,.+10 into the command prompt. You can now finish off this command like needed. Do note that the line count includes the current line itself (think of this count as current line plus number of lines after).

Jumping/deleting by matching tags

Let's say you have the following:

def a_method
  "a return value"
end

If you put your cursor on def and type %, it goes to end.

If you put your cursor on def and type d%, it deletes the entire method!