

毕竟 VSCode 现在可是正统的 “宇宙第一 IDE”,Vim 或者 Nvim 在很多方面显然要甘拜下风,很多时候都不得不重新捡起 VSCode 写代码(甚至尤其是 Windows 上糟糕的终端体验)。所以作为 Vim 的第二期教程,就来讲讲在 VSCode 上如何优雅地使用 Vim。
此外这里也给出了对应的 Vim 插件名称,读者也可以在正常的 Vim 或者 Neovim 上使用这些插件,体验完全一致。
插件#
对于 VSCode,Vim 插件少不了!VSCodeVim ↗ 速速装完了再看下文。
Input Method#
在进入 Vim 编辑模式时自动切换输入法
Null
对于非英文用户,在退出 Insert 模式后需要切换回英文输入模式,这对于偶尔频繁用非英文输入内容时很烦人(比如我正在输入的这篇文章)。这个配置可以在退出 Insert 模式时自动切换到英文,重新回到 Insert 的时候切换回上次退出时候的输入法,大幅降低你的血压。
这可能需要 im-select
或者 fcitx
等工具的支持。不过对于 Windows 官方的微软输入法,推荐使用 AIMSwitcher ↗。它能在不切换输入法语言的请款下,调用微软拼音输入法的中英文切换。
配置参考:
{
"vim.autoSwitchInputMethod.enable": true,
"vim.autoSwitchInputMethod.switchIMCmd": "C:/Users/cworld/.local/bin/AIMSwitcher.exe --imm {im}",
"vim.autoSwitchInputMethod.defaultIM": "0",
"vim.autoSwitchInputMethod.obtainIMCmd": "C:/Users/cworld/.local/bin/AIMSwitcher.exe --imm"
}
jsonvim-surround#
快速帮你选择和操作外框字符
surround.vim
写代码经常碰到外框 <>
、""
、{}
等?surround.vim
插件可以帮你快速选择和操作这些外框字符。即使是 <p>hello world</p>
这样选择起来非常麻烦的内容,也可以通过 yit
来快速选择外框。当然,不只是选择,添加、替换、删除都是一应俱全的。
Surround Command | Description |
---|---|
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) |
具体玩法 VScode 内部给了快速跳转链接,这里就不展开了。
vim-easymotion#
快速移动光标
vim-easymotion
移动光标确实常常力不从心,从一头跳到另一头可能最快的方法还是鼠标。所以我们有了 easymotion
插件,快速输入你想要找的字母,然后快速跳转(或者快速跳转到后文的任意单词的开头/结尾)。
使用指令很多,这里贴官方表:
Motion Command | Description |
---|---|
<leader><leader> s <char> | Search character |
<leader><leader> f <char> | Find character forwards |
<leader><leader> F <char> | Find character backwards |
<leader><leader> t <char> | Til character forwards |
<leader><leader> T <char> | Til character backwards |
<leader><leader> w | Start of word forwards |
<leader><leader> b | Start of word backwards |
<leader><leader> l | Matches beginning & ending of word, camelCase, after _ , and after # forwards |
<leader><leader> h | Matches beginning & ending of word, camelCase, after _ , and after # backwards |
<leader><leader> e | End of word forwards |
<leader><leader> ge | End of word backwards |
<leader><leader> j | Start of line forwards |
<leader><leader> k | Start of line backwards |
<leader><leader> / <char>... <CR> | Search n-character |
<leader><leader><leader> bdt | Til character |
<leader><leader><leader> bdw | Start of word |
<leader><leader><leader> bde | End of word |
<leader><leader><leader> bdjk | Start of line |
<leader><leader><leader> j | JumpToAnywhere motion; default behavior matches beginning & ending of word, camelCase, after _ and after # |
缺点大概是要动脑子快速输入对应位置,很多时候其实快速行跳转 + f
就定位了。这个都是习惯问题,习惯了肯定好用,故更适用于高阶玩家。
vim-commentary#
快速注释/反注释你的代码行或块
vim-commentary
这玩意被默认启用!使用方法很简单:
gc
注释选中行(gcc
注释当前行,同理gc2j
注释当前两行、gcap
注释当前段落);gC
注释选中段落(同理gCC
、gC2j
)。
vim-sneak#
使用两个字符跳转并操作
vim-sneak
这样的描述会很奇怪,我们需要举例说明:
something.data.set(
'rounded-t-2xl',
inView &&
(this.target.index
? !this.headingProgress[this.tocLinks[i - 1]?.slug].inView
: 'rounded-b-2xl'),
true
)
js假设我想把代码中的 (this.target.index ? ...)
表达式替换为 (this.target. index && <new content>)
,使用传统的查找选择编辑就很是捉襟见肘。但是如果使用了这个插件,我们就可以简化为:
- 前往关键位置
?
- 输入
cz),
- 后续内容全部被清空,进入编辑模式。
指令构成其实是 <operator>z<char><char>
,z
换 Z
就是向前查找。
此外还可以使用 s<char><char>
或 S<char><char>
快速跳转!甚至可以使用 ;
或 ,
重复跳转,可谓是非常便捷了。Vim 还提供了 “把原生的 f
替换成这个插件的” 以及忽略大小写功能,可以按需开启。
vim-textobj-arguments#
快速选择/处理参数
targets.vim
众所周知,Vim 的 Text Object 是其相当强大的特性之一,如 w
代表 Word, L
代表 Line
。VSCodeVim 也提供了一些额外的 Text Object 支持,比如:函数作为现代编程的基本单元,参数经常涉及修改。这个插件就能快速选择对应参数:
foo(a.test.backwards, b, c, d)
js光标放在第一个参数上,daa
就能删除整个参数(Argument)(含分割逗号),cia
就能替换整个参数。这个插件还支持 a
、i
、r
、l
、c
、d
、y
等操作,非常方便。
后来似乎这个插件做了很多衍生的 Text Object:
(11(22))
2<scope>(
选择当前第二层括号内的内容,如在单词inside
位置执行d2i)
会将out(middle (inside))
清除为out()
。同理多层t
即 Tag,选择 HTML 标签。修改标签名称操作就应该为cstt
,意为 Change Surround from Tag to Tag,也就是 「修改围绕的标签为另一个标签」q
即 Quote,选择双引号b
即 Bracket,选择小括号B
即 Bigger Bracket,选择大括号
VSCodeVim tricks!#
这部分内容似乎有点进阶?没事看看就行。
zc
zo
可以开关折叠,zR
zM
可以折叠所有或者展开所有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 pressedaf
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!
资料#
Boost Your Coding Fu With VSCode and Vim ↗:你敢信,有人真的为 VSCode + Vim 写了本书。这抽象程度简直就跟这发布网址一样,抽象的域名配合抽象的路径,最后搭配抽象的内容量,绝了(值得一提,排版和标题字号也很抽象)。不过内容详实巨细无遗,值得一读。
Preview: