Skip to content

Better way for C and Cpp development in Vim 8

Linwei edited this page Apr 27, 2018 · 29 revisions

I mainly use vim to write C/C++ codes, and the plugin AsyncRun is originated for C/C++ development. Here is a quick glance for speeding up your work flow:

Quick setup

Plug 'skywind3000/asyncrun.vim'

" automatically open quickfix window when AsyncRun command is executed
" set the quickfix window 6 lines height.
let g:asyncrun_open = 6

" ring the bell to notify you job finished
let g:asyncrun_bell = 1

" F10 to toggle quickfix window
nnoremap <F10> :call asyncrun#quickfix_toggle(6)<cr>

When you input :AsyncRun echo hello in the command line:

You will see the realtime command output in the open quickfix window.

Compile and run a single C/C++ file

Let's take a look at single file compilation, just like the build system in sublime, we can setup F9 for this:

nnoremap <silent> <F9> :AsyncRun gcc -Wall -O2 "$(VIM_FILEPATH)" -o "$(VIM_FILEDIR)/$(VIM_FILENOEXT)" <cr>

The macros in $(..) form will be expanded as the real file name or directory, and then we will have F5 for run:

nnoremap <silent> <F5> :AsyncRun -raw -cwd=$(VIM_FILEDIR) "$(VIM_FILEDIR)/$(VIM_FILENOEXT)" <cr>

The double quotation mark is used to handle path names containing spaces. The option -cwd=$(VIM_FILEDIR) means running the file in the file's directory. The absolute path name $(VIM_FILEDIR)/$(VIM_FILENOEXT) is used because linux needs a ./ prefix to running executables in current directory, but windows doesn't . Using the absolute path name of the binary file can handle this crossing platform issue.

Another option -raw means the output will not be matched by vim's errorformat, and will be displayed in quickfix as what it is. Now you can compile your file with F9, check the compilation errors in quickfix window and press F5 to run the binary.

Build C/C++ project

No matter what build tool you are using, make or cmake, project building means acting to a group of files. It requires locating the project root directory. AsyncRun uses a simple method called root markers to identify the project root. The Project Root is identified as the nearest ancestor directory of the current file which contains one of these directories or files:

.svn .git .hg .root .project

If none of the parent directories contains these root markers, the directory of the current file is used as the project root. Now we can use either <root> or $(VIM_ROOT) to represent the project root. and F7 can be setup to build the current project:

nnoremap <silent> <F7> :AsyncRun -cwd=<root> make <cr>

and setup F8 to run the project:

nnoremap <silent> <F8> :AsyncRun -cwd=<root> -raw make run <cr>

The project will run in its root directory. Of course, you need define the run rule in your own makefile. then remap F6 to test:

nnoremap <silent> <F6> :AsyncRun -cwd=<root> -raw make test <cr>

If you are using cmake, F4 can be map to update your Makefile:

nnoremap <silent> <F4> :AsyncRun -cwd=<root> cmake . <cr>

Due to the implementation of c runtime, if the process is running is a non-tty environment, all the data in stdout will be buffered until process exits. So, there must be a fflush(stdout) after your printf statement if you want to see the real-time output. or you can close the stdout buffer at the beginning by

setbuf(stdout, NULL);

At the mean time, if you are writing C++ code, a std::endl can be appended to the end of std::cout. It can force flush the stdout buffer. If you are developing on windows, AsyncRun can open a new cmd window for the child process:

nnoremap <silent> <F5> :AsyncRun -cwd=$(VIM_FILEDIR) -mode=4 "$(VIM_FILEDIR)/$(VIM_FILENOEXT)" <cr>
nnoremap <silent> <F8> :AsyncRun -cwd=<root> -mode=4 make run <cr>

Using the option -mode=4 on windows will open a new prompt window to run the command, just like running command line programs in Visual Studio. Now we have these key mappings below:

  • F4: (project) update Makefile with cmake.
  • F5: (file) run the single file
  • F6: (project) run project test
  • F7: (project) build project
  • F8: (project) run project
  • F9: (file) compile the single file
  • F10: toggle quickfix window

F5:单文件:运行 F6:项目:测试 F7:项目:编译 F8:项目:运行 F9:单文件:编译 F10:打开/关闭底部的 quickfix 窗口 恩,编译和运行基本和 NotePad++ / GEdit 的体验差不多了。如果你重度使用 cmake 的话,你还可以写点小脚本,将 F4 和 F7 的功能合并,检测 CMakeLists.txt 文件改变的话先执行 cmake 更新一下 Makefile,然后再执行 make,否则直接执行 make,这样更自动化些。