I created this plugin in order to improve integration CMake to the Vim editor. I tried different plugins for vim which allow to work with cmake but I didn't find the plugin which was satisfied my requrements.
- Written in pure Vimscript
- The plugin supports next CMake Generators:
- Unix Makefiles
- Visual Studio
- Ninja
- The plugin shows cmake results using quickfix list. If you have installed vim-dispatch plugin, plugin will use it, this means that if you are using vim with tmux, cmake output will be printed in a separate window. In other case plugin will use
jobsto async run if your Vim editor supports it. - The plugin shows cmake results using quickfix list. If you have installed vim-dispatch plugin, plugin will use it, this means that if you are using vim with tmux, cmake output will be printed in a separate window. In other case plugin will use
jobsto async run if your Vim editor supports it. * The plugin allows to specify cmake targets in order to avoid building of all project. - The plugin has an integration with next fuzzy finder plugins:
- The plugin allows to specify make arguments for native build system (for example -jN and something else for Unix Make).
- The plugin parses the output of cmake command and supports jump to warnings or errors.
- Supports work with multiple build types
- For CMake newer than 3.13 the plugin uses the CMake file API
- Plugin allows to find and run executable file for selected target.
- Plugin supports Vimspector plugin. Plugin can generate and modify Vimspector configuration file in order to save command line arguments and allows to debug application.
- SantinoKeupp/telescope-cmake4vim.nvim - Telescope integration.
- SantinoKeupp/lualine-cmake4vim.nvim - Integration with Lualine. Plugin adds information about elected kit, build type and build target.
- ilyachur/gtest-vim - Integrates vim with gtest. Plugin has a dependency on cmake4vim and can use cmake4vim capabilities to detect gtest executables.
You can use VimPlug for installation:
Plug 'ilyachur/cmake4vim'Or Pathogen:
cd ~/.vim/bundle
git clone https://github.com/ilyachur/cmake4vimThe current version of the plugin supports next commands:
:CMakecreates a build directory (if it is necessary) and generates cmake project.:CMakeResetAndReloadremoves cmake cache and re-generates cmake project.:CMakeResetremoves cmake cache (this command removes the cmake build directory).:CMakeBuildbuilds current cmake project. The command allows to specify cmake target.:CMakeSelectTargetselects a target for project. You should put target name as a command line argument.:CMakeSelectBuildTypechanges the cmake build type with argument passed and call:CMake.:CMakeInfocreates a window with CMake information.:CMakeCleancleans the project (it is equal of the executionmake clean).:CMakeCompileSourcecompiles source file in current buffer.
:CMakeRunRun the current the binary of currently selected target. Allows to automatically change the Vimspector config file.:CMakeRun!Run the current the binary of currently selected target. Command allows to reset previous arguments if plugin reads arguments from Vimspector config.:CTestrunctest. The command allows to specify CTest arguments and default arguments can be set ing:cmake_ctest_args:CTest!same as:CTestbut ignoresg:cmake_ctest_args.:CTestCurrentsame as:CTestbut run tests with-R current_cmake_target.:CTestCurrent!same as:CTest!but run tests with-R current_cmake_target.
:CCMakeallow to use ccmake command inside vim. The command supports next open modes: 'vsplit' - vertical mode, 'split' - horizontal mode, 'tab' - open ccmake in the new tab (by default the executor window split mode is used).
:CtrlPCMakeTargetyou can use CtrlP in order to select a target for project.:CtrlPCMakeBuildTypeallows to use CtrlP in order to select a project build type.:CtrlPCMakeKituses CtrlP to select a CMake kit for project.:FZFCMakeSelectTargetyou can use FZF in order to select a target for project.:FZFCMakeSelectBuildTypeallows to use FZF in order to select a project build type.:FZFCMakeSelectKituses FZF to select a CMake kit for project.
<Plug> mapping |
Command |
|---|---|
(CMake) |
:CMake |
(CMakeResetAndReload) |
:CMakeResetAndReload |
(CMakeReset) |
:CMakeReset |
(CMakeBuild) |
:CMakeBuild |
(CMakeClean) |
:CMakeClean |
(CMakeInfo) |
:CMakeInfo |
(CMakeRun) |
:CMakeRun |
(CTest) |
:CTest |
(CTestCurrent) |
:CTestCurrent |
(CCMake) |
:CCMake |
(CMakeCompileSource) |
:CMakeCompileSource |
Plugin supports special global variables which are allow to change behaviour of commands (you can change them in your .vimrc):
The options below allow to change plugin behavior.
g:cmake_executablethe path to CMake. Default is 'cmake'.g:cmake_reload_after_saveif this variable is not equal 0, plugin will reload CMake project after saving CMake files. Default is 0.g:cmake_change_build_commandif this variable is not equal 0, plugin will change the make command. Default is 1.g:cmake_compile_commandsif this variable is not equal 0, plugin will generate compile commands data base. Default is 0.g:cmake_compile_commands_linkset the path for a link on compile_commands.json. Default is empty.g:cmake_vimspector_supportenables generation and modification of Vimspector config file. Default is 0.g:cmake_vimspector_default_configurationis a default configuration for new vimspector target. Default is:
let g:cmake_vimspector_default_configuration = {
\ 'adapter': '',
\ 'configuration': {
\ 'request': 'launch',
\ 'cwd': '${workspaceRoot}',
\ 'Mimode': '',
\ 'args': [],
\ 'program': ''
\ }
\ }
g:cmake_build_executorallows to force set the build executor. Default is empty. Available values are:- 'job' uses job to asynchronous build
- 'term' uses terminal to asynchronous build Experimental
- 'dispatch' uses vim-dispatch plugin to asynchronous build
- 'system' uses synchronous build
- '' uses automatic detection of supported modes (the priority is
dispatch,job,term,system)
g:cmake_build_executor_window_sizedefines the size of build window and quickfixlist. Default is 10.g:cmake_build_executor_split_modeAllows to configure split mode for build window and quickfixlist. Avaulable values are:- 'sp' enables horizontal mode. It is the default value.
- 'vsp' enables vertical mode.
g:cmake_build_executor_heightdefines the height (in rows) of the build window and quickfixlist window showing the results. The option was deprecated, please uselet g:cmake_build_executor_window_size=<size>instead. Default is 10.
Below the list of options which allow to customize the path to CMake build directory. The list order is from higher to lower priority (it means if you initialize several options plugin will use the first initialized option from the list):
g:cmake_build_dirallows to set cmake build directory. Default is ''. If variable is empty the plugin will use the prefix plus build type.g:cmake_build_path_patternpattern for build dir, two strings that will be evaluated in aprintf. e.g.:let g:cmake_build_path_pattern = [ "%s/workspace/build/%s/%s/%s", "$HOME, fnamemodify( getcwd(), ':t' ), g:cmake_selected_kit, g:cmake_build_type" ]g:cmake_build_dir_prefixallows to set cmake build directory prefix, in this case the plugin uses the next rule to generate build directory name:g:cmake_build_dir_prefix+g:cmake_build_type. This option is used by default, the default prefix is 'cmake-build-'.
The list contains variables which allow to configure CMake build.
g:cmake_src_dirallows to set cmake source directory. Default is '' which evaluates to the current working directory.g:cmake_build_typeallows to change-DCMAKE_BUILD_TYPE. Default is empty. If variable is empty, plugin tries to detect cached build type. And selects 'Release' type if cmake cache doesn't exist.g:cmake_variantsenables predefined cmake build variants in the form of a dictionary, e.g.{ 'Debug' : { 'cmake_build_type' : 'Debug', 'cmake_usr_args' : { 'CONAN_PATH' : '~/.conan' } }g:cmake_build_targetset the target name for build. Default is empty and default value depends on CMake Generatorg:cmake_usr_argsallows to set user arguments for cmake. Default is empty. It can be either a string or a dictionary.g:cmake_build_argsallows to set custom cmake build arguments (for example--parallel). Default is empty.g:make_argumentsallows to set custom parameters for make command. Default is empty. If variable is empty, plugin launchesmakewithout arguments.g:cmake_ctest_argsenables arguments forctest, e.g.'-j8 --output-on-failure --verbose'. Default is empty. If the user calls:CTest <some arguments>, theg:cmake_ctest_argsare inserted directly afterctest, before the<some arguments>parameter.g:cmake_kitsenables predefined cmake kits in the form of a dictionary of dictionaries that specify a toolchain file, environment variables, cmake variables among other thingsg:cmake_kits_global_pathspecifies a path to the JSON containing cmake kits. Default is empty.g:cmake_selected_kitcurrently selected cmake kit. Default is empty.
CMake kits can be given in the following ways:
- as a variable
g:cmake_kits - as a JSON file in the current folder named
.cmake-kits.json - as a JSON file specified by
g:cmake_kits_global_pathvariable
The variable g:cmake_kits has the lowest, and the local .cmake-kits.json file has the highest priority.
Example of supported functions in CMake kits:
let g:cmake_kits = {
\ "android-ndk-r22": {
\ "toolchain_file": "~/toolchains/android.cmake",
\ "environment_variables": {
\ "PATH_TO_CACHE": "/Users/vimmer/cache",
\ "PATH_TO_CONFIG": "/Users/vimmer/config"
\ },
\ "cmake_usr_args": {
\ "USE_BROWSER": "chrome",
\ "TIMEOUT_IN_SECONDS": "300"
\ },
\ "generator": "Ninja"
\ } }
let g:cmake_kits = {
\ "gcc": {
\ "compilers": {
\ "C": "/usr/bin/gcc",
\ "CXX": "/usr/bin/g++"
\ } }
If you specify both toolchain_file and compilers, the toolchain_file takes precedence and compilers are ignored.
Example of a .cmake-kits.json file:
{ "gcc": {
"compilers" : {
"C": "/usr/bin/gcc",
"CXX": "/usr/bin/g++"
} } }Plugin is closely integrated with quickfix list and allows to use quickfix features to jump between error or warning messages.
- :cp[revious] command jumps to previous error/warning message
- :cn[ext] command jumps to next error/warning message
Bug reports, feedback, feature and other pull requests are appreciated. Check the Contributing Guidelines for how to create a feature request, submit a pull request or post an issue.
nnoremap <expr> <leader>db printf(":bo new\<CR>:lcd %s\<CR>:res 15\<CR>:term ++curwin\<CR>", utils#cmake#getBuildDir() )
If you're using YouCompleteMe and want clangd-completer to work with different compilers, you could pass clangd a -query-driver argument.
E.g. for developing emscripten, you could pass em++ to clangd via g:ycm_clangd_args.
You could add such entries to your g:cmake_kits and override CMakeSelectKit and FZFCMakeSelectKit.
Add this to the after directory, e.g. ~/.vim/after/plugin/cmake.vim:
function! s:customSelectKit(name) abort
let l:loaded_kits = utils#cmake#kits#getCMakeKits()
if !has_key( l:loaded_kits, a:name )
call utils#common#Warning(printf("CMake kit '%s' not found", a:name))
return
endif
call cmake4vim#SelectKit(a:name)
let l:cmake_kit = l:loaded_kits[ g:cmake_selected_kit ]
let g:ycm_clangd_args = filter( g:ycm_clangd_args, "v:val !~# 'query-driver'" )
if has_key( l:cmake_kit, 'query_driver' )
let g:ycm_clangd_args += [ printf( '-query-driver=%s', l:cmake_kit[ 'query_driver' ] ) ]
YcmRestartServer
endif
endfunction
function! s:FZFSelectKit() abort
if exists(':FZF')
return fzf#run({
\ 'source': sort( keys( utils#cmake#kits#getCMakeKits() ), 'i' ),
\ 'options': '+m -n 1 --prompt CMakeKit\>\ ',
\ 'down': '30%',
\ 'sink': function('s:customSelectKit')})
endif
endfunction
command! -nargs=1 -complete=custom,cmake4vim#CompleteKit CMakeSelectKit call s:customSelectKit(<f-args>)
command! FZFCMakeSelectKit call s:FZFSelectKit()
The plugin supports all CMake versions since 2.8.
Since the CMake 3.14 version the plugin uses file API, this feature helps to have more information about CMake project and implement a smart
detection of executable files for :CMakeRun command.
Ilya Churaev ilyachur@gmail.com
MIT




