Skip to content

Instantly share code, notes, and snippets.

@sgtrusty
Forked from dogfuntom/Vim Ideas.md
Created October 2, 2020 04:11
Show Gist options
  • Save sgtrusty/f03de1c186ed0d2633a154b0f25f66c8 to your computer and use it in GitHub Desktop.
Save sgtrusty/f03de1c186ed0d2633a154b0f25f66c8 to your computer and use it in GitHub Desktop.
VsVim Registers and Marks Cheat Sheet #vs #vim #vsvim

Workarounds

Filling empty line between { and }

When you type a new method, VS aids you and you end up with this:

private void Foo()
{

}

If you don't write anything there immediately, but return to it later, VsVim forces you to type from the hard BoL of the empty line. I don't know any good way to deal with it, but at least there's certainly a nice assortment of acceptable ways:

  • ddO
  • i, End
  • If the expression is simple, just do i and type it (ignoring intendation), it'll auto-format automatically when you type “;” or other trigger.

Registers

Most useful

"0 — Last yank. (Very useful when you yanked something, then deleted something in target location, then want to paste yanked text instead of just deleted: "0p.)

"+ — System clipboard.

Special registers

:echo @r — Print register r.

"" — Unnamed register used automatically on dd, yy, etc.

"/ — Last search pattern.

"_ — Black hole. (E.g. "_dd will delete a line without remembering it. Undo will still work as usual though.)

"1 — Big delete (deleted line or lines).

"2"9 — Previous big deletes.

"- — Small delete (deleted text within a single line).

Named registers

"a"z — Named registers.

"rd — Delete into register r.

"ry — Yank into register r.

"Rd, "Ry — Delete or yank appending into register r.

Macros

Macros mostly reuse the same register system described above. You can't execute @+ in VsVim though.

qr — Record into register r.

@r — Playback from register r.

@@ — Repeat last playback.

Marks

Most useful

`. — Jump to last change.

` ` — Jump back (in other words, unjump, i.e. navigate back or undo/reverse last jump).

Special registers

'' — Jump back to line (instead of exact position).

Named registers

mm — Place mark m.

'm — Jump to line containing mark m.

`m — Jump to mark m exact position.

Don't forget that all jump operations above, similarly to most (all?) other kinds of jump operations, can be used together with d, c or y.

With vim it's easy to figure out how to do something clever: eigher it's clear immedietly, or it requires only a bit of googling, otherwise it's likely not reasonable (as any tool, it should be used for what it's good for, not for everything).
What is hard is to recognize vim-automation-worthy task in the first place! Therefore, the goal of this file is to give a real-life inspiration regarding where to apply your vim knowledge.
#### Log method call
Useful for understanding sloppily/poorly written legacy code. Given a bunch of methods like this:
private void ConfusinglyNamedAndConfusinglyUsedMethod()
{
// confusing code here...
}
Record a macro with a following algorithm: starting at line with name, go to name, yank name, go down, open new line, type logging method, paste name, finish calling logging name, done. Apply this macro to every method with unclear purpose.
Result:
private void ConfusinglyNamedAndConfusinglyUsedMethod()
{
Debug.Log(nameof(ConfusinglyNamedAndConfusinglyUsedMethod), this);
// confusing code here...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment