Hi, Habrahabr. My name is Alexey and I am committed to developing embedded operating systems.
Today I want to tell you how I use Vim as an IDE for C / C ++ projects.
Although I use Vim for C / C ++, many of the recommendations are quite general and can be used in any project.
First step
For a start, we want to have a place where you can save settings for your project. Global
.vimrc does not suit us, because there are many projects and they are different, which means that each of them has its own specific settings.
')
Vim has a suitable option for us -
exrc . She tells Vim to look for an additional
.vimrc in the current working directory. Thus, we can have one file in the root of each project with its specific settings.
Since vim will connect
.vimrc from any directory from which you run it, it is advisable to take some security measures and set the
secure option. Basically, it prohibits external
.vimrc from writing something to files, executing shell commands, and displaying mappings that are created.
In general, we need to add the following two lines to our main
.vimrc :
set exrc set secure
Settings for a specific project
After we got the opportunity to save options for each project separately, why don't we do it.
All projects have their own rules regarding the same indents, so just place the necessary settings in
.vimrc in the root of your project:
set tabstop=4 set softtabstop=4 set shiftwidth=4 set noexpandtab
In addition, I try to keep my lines within 110 characters. But since it is very specific and strongly dependent on the context, I cannot trust such an important matter as splitting lines to an editor (even vim). But vim can help me just highlighting the desired column.
set colorcolumn=110 highlight ColorColumn ctermbg=darkgray
File Type Definitions
By default, Vim assumes that all .h files are C ++. However, some of my projects are written in pure C. Therefore, I want the file type to be C. In addition, my projects use doxygen, so I don’t mind including very cool doxygen highlighting in vim.
In order to add a couple of lines to the local
.vimrc :
augroup project autocmd! autocmd BufRead,BufNewFile *.h,*.c set filetype=c.doxygen augroup END
Setting the path variable
In vim there is a wonderful
gf command (and related to it), which opens a file whose name is under the cursor. This is incredibly useful when navigating through header files.
By default, vim looks for files in the current folder, as well as in
/ usr / include . However, almost all projects have header files that are stored in other folders. Therefore, you need to set the
path variable, which contains a list of folders to search, separated by commas.
let &path.="src/include,/usr/include/AL,"
The note
Javista should pay attention to the
includeexpr option. It contains an expression that will be used to convert the string to a file name. The next line replaces all the dots for slashes for
gf . Thus, you can jump on import'am.
set includeexpr=substitute(v:fname,'\\.','/','g')
Autocompletion
Best (Obsolete. Now I use YouCompleteMe. Refer to
my post for details) A good plugin for autocompleting C / C ++ code that I found is
clang_complete (see the plugin page for installation instructions).
It uses
clang to generate a list of add-ons. And it works fine for C and C ++ (as well as a couple of languages).
In order for clang to know about your folders with header files, you need to add the
-I and
-D flags to the
.clang_complete file in the root of your project. After that, clang_complete will be automatically called when you click "." or "->".
hint
If the
path option is already filled, you can easily insert a list of I-flags using the following simple key combination
"='-I'.substitute(&path, ',', '\n-I', 'g')<CR>p
Other clang plugins
Clang is not just a compiler, but also a set of useful libraries for analyzing and manipulating code. Thus, it is a very good basis for a variety of plug-ins. So in the near future we can expect a lot of plug-ins based on it.
Navigator
In order to display the project tree, you can use either individual plugins (
NERD Tree for example) or the built-in netrw.
I do not use any of the methods, since it is quite comfortable for me to select files directly from the vim command mode. (if you set
wildmode to
longest:list,full
, then vim shows matching files as a normal shell)
Build system setup
After we have dealt with editing and navigating through files, you can build a project. In vim there is a
make command . By default, it runs make in the current directory and parses the output for errors.
The actual command to execute is stored in
makeprg . If the project is built in a different folder, uses different make options, or uses a different build system at all, you just need to change the
makeprg to display this:
set makeprg=make\ -C\ ../build\ -j9
After that, you can build the project by typing
:make
.
But since this is not enough for us, you can hang this command on one of the unnecessary buttons. For example:
nnoremap <F4> :make!<cr>
("!" prevents from jumping to the first error)
Startup system settings
After the project is assembled, it is quite logical to start it. Vim can run any program, for this you just need to add an exclamation mark in front of her name in command mode. So, to run my super-duper program, you need to dial
:!./my_great_program
.
Of course, this also needs to be simplified!
nnoremap :!./my_great_program<cr>
Version Control System
Since vim provides direct access to the command line, I do not use any special plugin for these tasks and just manage my git-tree directly from the shell.
But I have to note that there are many excellent vim plugins for working with different version control systems.
For example,
fugitive . If you are working with git, you absolutely must look
at fugitive wimcasts .
Forgotten item: debugging
Since I feel comfortable in the command-line gdb, and the specificity of the operating systems makes it almost impossible to debug them with debuggers, I only ran gdb once a year. Basically, I’ll debug all my projects with regular print'ami and a bit of brains. So in my vim there is nothing to remind a debugger.
However, I must say that there are several plugins for integrating gdb and vim. For example,
clewn ,
gdbvim .
Total
After a series of simple operations, vim turned into quite a tolerable IDE. I would be glad to hear about the additional settings that made your life easier in vim and allowed to better integrate it with the surrounding tools.