VSCode Vim插件:实现EasyMotion移动后自动屏幕居中
解决VSCode Vim扩展EasyMotion跳转后自动居中的问题
方法:利用Vim自动命令监听EasyMotion完成事件
VSCode的Vim扩展兼容大部分Vimscript语法,可通过添加自动命令,在EasyMotion跳转完成后自动执行zz命令实现居中。
- 打开VSCode设置(快捷键
Ctrl+,或Cmd+,),搜索vim.vimrc,将其设置为本地Vimrc文件路径,比如~/.vscode.vimrc(Windows用户可使用%USERPROFILE%\.vscode.vimrc)。 - 创建并打开该Vimrc文件,添加以下内容:
augroup EasyMotionAutoCenter autocmd! autocmd User EasyMotionPostMotion zz augroup END
- 重启VSCode,或在Vim命令模式下执行
:source ~/.vscode.vimrc使配置生效。
此后每次使用EasyMotion跳转,光标都会自动触发zz,让屏幕居中显示光标位置。
备选方案:自定义映射(针对特定EasyMotion命令)
若上述自动命令不生效(部分版本扩展可能未支持EasyMotionPostMotion事件),可针对常用EasyMotion命令添加自定义映射,确保跳转后执行zz:
在VSCode的settings.json中添加以下配置:
"vim.normalModeKeyBindingsNonRecursive": [ // 单词跳转(leader + s) { "before": ["<leader>", "s"], "commands": [ { "command": "extension.vim-easymotion", "args": "motion" }, { "command": "vim.wait", "args": 100 }, { "command": "vim.normal", "args": "zz" } ] }, // 行内向前找字符(leader + f) { "before": ["<leader>", "f"], "commands": [ { "command": "extension.vim-easymotion", "args": "f" }, { "command": "vim.wait", "args": 100 }, { "command": "vim.normal", "args": "zz" } ] }, // 按需添加其他EasyMotion映射,比如F/t/T等 ]
其中vim.wait用于等待EasyMotion交互式选择完成后再执行zz,可根据操作速度调整等待时间(单位毫秒)。
内容的提问来源于stack exchange,提问作者accoumar
相关产品推荐
相关产品推荐

