This page contains notes on ways to use Vim or
Neovim with Drake. Wherever there are differences
between the two the Neovim value will be given in curly braces after the Vim
sample_vimrc (download) provides a sample
configuration file that sets up some plugins and key-mappings that users may
find useful for working with Drake. It should work out of the box if copied
~/.config/nvim/init.vim}. Be sure to back up any existing
init.vim} before copying! Users with existing Vim
configurations are, of course, free to cherry-pick whatever portions of
sample_vimrc they’d like.
If you do copy
init.vim}, your next step
is to install the plugins that it specifies.
- Open a terminal and launch Vim. The plugin manager (vim-plug) will be automatically installed (to disable this behavior, delete the four lines following the comment “Remove these lines …”).
- In Vim, type
:PlugInstalland press “Enter”. vim-plug will download the specified plugins from GitHub. If you later decide to stop using vim-plug, you can remove it along with the plugins it installs by deleting the following
Setting up Code-Completion/Linting/Jump To Declaration
YouCompleteMe is a plugin that provides real-time linting + jump-to-declaration functionality. It has a compiled component, which needs to be built before the plugin can be used.
<YouCompleteMe-location>be the directory in which the plugin is located. If you’re using the provided
sample_vimrc, you can determine
:echo g:plug_homein Vim and appending
/YouCompleteMeto the result.
- In a terminal, run
cd <YouCompleteMe-location> git submodule update --init --recursive ./install.py --clang-completer
In order for YouCompleteMe to provide code-completion and linting for Drake’s C++ files, it needs to know what flags to use when compiling them. Fortunately, bazel-compilation-database provides a way for YouCompleteMe to get this information from Bazel. The following steps should get this working in Drake:
- Download the bazel-compilation-database 0.2.2 tarball.
- Extract the contents of the tarball to some directory. We will refer to
this directory as
- From the root of your Drake repository run:
mkdir bazel ln -s <bcd-location> bazel/compilation_database
- Add the following line to your
let g:ycm_global_ycm_extra_conf = '<bcd-location>/.ycm_extra_conf.py'
Now, when you open a Drake C++ file, you should get real-time linting and
completion, as well as jump-to functionality (through the
command). If it appears not to be working, try running
:YcmDiags to force
YouCompleteMe to compile the current file. Note that for YouCompleteMe to see
a given Drake header file, the corresponding Bazel target must have been built.
See also, Linting and Jump-to-Declaration Mappings.
sample_vimrc file pulls in several plugins and defines
mappings for working with them. It also defines some general-purpose mappings.
Here are some highlights. A lot of these start with
<leader>, which in the
attached file is mapped to
;gv). You can, of course,
<leader> to whatever you prefer in your
<leader>gdshows the git diff for the current file. You can move hunks into the index using Vim’s diff commands.
<leader>gsshows the current git status using fugitive.git.You can stage files by moving the cursor to the line and pressing “-“. Press cc to start editing the commit message. Save and exit in the commit message to commit.
<leader>gvlaunches gitv, a Gitk clone for Vim. You can use this to inspect commits, switch branches, cherry-pick, and more
Linting and Jump-to-Declaration Mappings
<leader>ggjumps to the declaration of the class/function under the cursor (mnemonic “go go!”). Use the standard jump command Ctrl+o to go back.
<leader>fiapplies the “fix-it” action listed at the bottom of the buffer (mnemonic “fix it!”).
<leader>lotoggles the Location List on and off (mnemonic “LOcation”). All errors found by YouCompleteMe are listed in the location list, which can make it easy to go from one to the next.
File Tree Explorer Mappings
These mappings use NERDTree to provide a file tree explorer in Vim.
<leader>ntlaunches NERDTree for the current working directory
<leader>nflaunches NERDTree with the current file selected. This is particularly useful for getting to the appropriate
BUILD.bazelfile for a given source file.
Fuzzy Finder Mappings
These mappings use ctrlp.vim to provide fuzzy file lookup in Vim.
<leader>fflaunches CtrlP in file mode (mnemonic “find file”). Start typing any portions of the file path (they don’t have to be contiguous!) and select the file you want once the choices are narrowed down sufficiently.