1. 安装vimspector
使用vimspector 插件
vimrc文件中添加插件并安装
Plug 'puremourning/vimspector'
2. 安装vimspector C++调试适配器
打开vim
:VimspectorInstall --enable-c
会在 ~/.vim/plugged/vimspector/gadgets/linux/ 目录安装vscode-cpptools
3. 添加.vimspector.json模板
vim ~/.vim/.vimspectorjson/cpp.json
{
"configurations": {
"cpp:launch": {
"adapter": "vscode-cpptools",
"configuration": {
"name": "cpp:launch",
"stopAtEntry": true,
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/a.out",
"args": [],
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"breakpointers": {
"exception": {
"cpp_throw": "Y",
"cpp_catch": "N"
}
}
}
},
"cpp:attach": {
"adapter": "vscode-cpptools",
"configuration": {
"name": "cpp:attach",
"stopAtEntry": true,
"type": "cppdbg",
"request": "attach",
"program": "${workspaceRoot}/a.out",
"MIMode": "gdb",
"breakpointers": {
"exception": {
"cpp_throw": "Y",
"cpp_catch": "N"
}
}
}
}
}
}
4. 配置快速生成模板快捷键
vim ~/.vimrc
// 1. 安装fzf
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
// 2. 添加快捷键<leader>db快速生成.vimspector.json
"vimspector
let g:vimspector_enable_mappings = 'VISUAL_STUDIO'
"let g:vimspector_base_dir=expand('~/.vim/.vimspectorjson')
function! s:read_template_into_buffer(template)
" has to be a function to avoid the extra space fzf#run insers otherwise
execute '0r ~/.vim/.vimspectorjson/'.a:template
endfunction
command! -bang -nargs=* LoadVimSpectorJsonTemplate call fzf#run({
\ 'source': 'ls -1 ~/.vim/.vimspectorjson',
\ 'down': 20,
\ 'sink': function('<sid>read_template_into_buffer')
\ })
noremap <leader>db :tabe .vimspector.json<CR>:LoadVimSpectorJsonTemplate<CR>
sign define vimspectorBP text=☛ texthl=Normal
sign define vimspectorBPDisabled text=☞ texthl=Normal
"sign define vimspectorPC text=¶ texthl=SpellBad
5. 实例
5.1 添加调试模板
在项目中添加.vimspector.json配置
打开vim, 在normal模式下输入快捷键<leader>db,选择cpp.json显示如下:
生成模板
修改配置文件
.vimspector
5.2 按F5启动调试
选择2启动
调试界面
5.3 调试快捷键
可以直接是作者的GitHub(https://github.com/puremourning/vimspector)上查看文档,我用的是vscode风格的快捷键
快捷键