Vimspector配置文件路径指定及预启动构建功能咨询
Background
I'm using Vim as a C++ IDE with puremourning/vimspector for debugging and ycm-core/YouCompleteMe for error checking and completion. My project structure looks like this:
myproject\ ├─ src\ │ └─ main.cpp (depends on include/maininclude.h) ├─ include\ │ └─ maininclude.h ├─ .vscode\ (VSCode config files) │ ├─ tasks.json │ ├─ launch.json │ ├─ Makefile │ ├─ Makefile-Debug │ ├─ Makefile-impl │ └─ Makefile-variables └─ .vim\ (intended to hold all Vim IDE-related files) ├─ .ycm_extra_conf.py ├─ Makefile ├─ Makefile-Debug ├─ Makefile-impl ├─ Makefile-variables └─ .vimspector.json
I have two questions:
Question 1: Can Vimspector read .vimspector.json from the .vim folder instead of the project root? Is there a config option similar to YCM's g:ycm_global_ycm_extra_conf to change the default config path?
I want all Vim IDE-related files to stay in the .vim folder, not scattered in the project root.
Answer
Absolutely! Vimspector supports customizing the config file path via a global variable in your .vimrc. Just add this line:
let g:vimspector_config_path = './.vim/.vimspector.json'
This tells Vimspector to load the config from your .vim directory relative to the project root. If you ever need to override it temporarily for a single session, you can also run :VimspectorLoadConfig ./.vim/.vimspector.json manually, but setting the global variable is the best way to enforce your centralized setup.
Question 2: VSCode's launch.json has a preLaunchTask that runs make CONF=Debug -C ./.vscode, plus it auto-saves modified files before building. Can Vimspector replicate this workflow (auto-save, build from .vim folder's Makefile, then start debugging to breakpoints)? Right now I have to manually run make CONF=Debug in myproject/.vim/ then go back to Vim and press F5.
Answer
Vimspector doesn't have a built-in preLaunchTask option, but you can build this workflow using Vim's autocommands and custom commands—it's actually pretty straightforward:
Auto-save modified files before debugging
Add this autocmd to your.vimrc; it will save all changed files right before Vimspector launches:autocmd User VimspectorPreLaunch * silent waThe
silentflag prevents unnecessary output, andwasaves all modified buffers.Custom command to build + launch debugging
Create a custom command that runs your Makefile build first, then starts Vimspector. Add this to.vimrc:command! -nargs=0 DebugBuildAndRun :silent !make CONF=Debug -C ./.vim | call vimspector#Launch()silent !make ...runs the build command without cluttering your Vim session.- If
makereturns a non-zero exit code (meaning build failed), the pipe|won't execute the next command—so Vimspector won't launch, just like VSCode stops on build errors.
Bind to F5 for one-click workflow
Replace your existing F5 binding with the custom command to mimic VSCode's behavior:nnoremap <F5> :DebugBuildAndRun<CR>Now pressing F5 will auto-save your files, run the Debug build from
.vim, and launch debugging to your breakpoints—all in one step.
内容的提问来源于stack exchange,提问作者Tryer

