配置 Vim 作为 C++ 开发环境初步

一般来说,如果你使用 Vim 开发完整的项目,使用 Makefile 并直接 :make 就可以了。

将下面的代码写入到 vimrc 中, Vim 会捕获编译过程中出现的消息并显示在 Quickfix 窗口。

autocmd FileType cpp,c nmap <F9> :make<CR> :copen<CR> <C-W>10_
1

但是如果你是 C/C++ 新手,目前较多书写单个的 C/C++ 代码文件,或者只是想快速测试 某段代码的话,使用 gcc/g++ 直接编译或许是更简单的方式。你可以直接在 Vim 中执行 外部 gcc/g++ 命令,也可以想这样映射快捷键:

autocmd FileType cpp nmap <F10> :w<cr>:exe "!gcc -o ".expand("%:r").".exe ".expand("%")<cr>
1

不过这样使用外部命令, Vim 并不能捕获编译过程的输出消息,就无法在 Vim 中根据 错误消息有效的定位到错误行了。

有更好的方法:

autocmd FileType cpp nmap <F10> :w<cr>:exe ":set makeprg=g++\\\ -Wall\\\ -o\\\ ".expand("%:r").".exe\\\ ".expand("%")<cr>:make<cr><cr>:cw<cr>
autocmd FileType c nmap <F10> :w<cr>:exe ":set makeprg=gcc\\\ -Wall\\\ -o\\\ ".expand("%:r").".exe\\\ ".expand("%")<cr>:make<cr><cr>:cw<cr>
autocmd FileType cpp,c nmap <F5> :exe "!".expand("%:r").".exe"<Left>
autocmd FileType javascript nmap <F9> :call JsonLint()<cr>
1
2
3
4

再补充映射一些 Quickfix 模式中快速跳转的快捷键:

map <F4> :cclose<CR>
map <F8> :cn<CR>
map <F7> :cp<CR>
1
2
3

完整的设置可以参考 我的 vimrc

更多讨论 看这里

参考

Help
[count]gg 跳转到第 [count] 行,默认第 1 行。
[count]G 跳转到第 [count] 行,默认最后一行。
[count]j 向下跳转 [count] 行,默认跳转一行。
[count]k 向上跳转 [count] 行,默认跳转一行。
/ 开始搜索。按 <Esc> 退出。
gh 跳转到首页。
gb 跳转到博客首页。
gw 跳转到 Wiki 首页。
gt 跳转到我的 Twitter Profile 页。
gp 跳转到我的 Github Profile 页。
? 打开帮助。按 <Esc> 退出。