VS Code Vim 速记

https://marketplace.visualstudio.com/items?itemName=vscodevim.vim

VS Code 的 Vim 扩展,除了复现标准的 Vim 键位之外,默认启用了几个插件,在此简单罗列平时常用的几个。

vim-surround

Based on surround.vim, the plugin is used to work with surrounding characters like parentheses, brackets, quotes, and XML tags.

t or < as <desired> or <existing> will enter tag entry mode. Using <CR> instead of > to finish changing a tag will preserve any existing attributes.

Surround CommandDescription

y s <motion> <desired>

Add desired surround around text defined by <motion>

d s <existing>

Delete existing surround

c s <existing> <desired>

Change existing surround to desired

S <desired>

Surround when in visual modes (surrounds full selection)

Some examples:

  • "test" with cursor inside quotes type cs"' to end up with 'test'

  • "test" with cursor inside quotes type ds" to end up with test

  • "test" with cursor inside quotes type cs"t and enter 123> to end up with <123>test</123>

vim-commentary

Similar to vim-commentary, but uses the VS Code native Toggle Line Comment and Toggle Block Comment features.

Usage examples:

  • gc - toggles line comment. For example gcc to toggle line comment for current line and gc2j to toggle line comments for the current line and the next two lines.

  • gC - toggles block comment. For example gCi) to comment out everything within parentheses.

vim-indent-object

Based on vim-indent-object, it allows for treating blocks of code at the current indentation level as text objects. Useful in languages that don't use braces around statements (e.g. Python).

Provided there is a new line between the opening and closing braces / tag, it can be considered an agnostic cib/ci{/ci[/cit.

CommandDescription

<operator>ii

This indentation level

<operator>ai

This indentation level and the line above (think if statements in Python)

<operator>aI

This indentation level, the line above, and the line after (think if statements in C/C++/Java/etc)

VSCodeVim tricks!

  • gd - jump to definition.

  • gq - on a visual selection reflow and wordwrap blocks of text, preserving commenting style. Great for formatting documentation comments.

  • gb - adds another cursor on the next word it finds which is the same as the word under the cursor.

  • af - visual mode command which selects increasingly large blocks of text. For example, if you had "blah (foo [bar 'ba|z'])" then it would select 'baz' first. If you pressed af again, it'd then select [bar 'baz'], and if you did it a third time it would select "(foo [bar 'baz'])".

  • gh - equivalent to hovering your mouse over wherever the cursor is. Handy for seeing types and error messages without reaching for the mouse!

最后更新于