Minimal .vimrc for C/C++ developers

Update 2014-01-27: my vim configuration is now available on https://github.com/gergap/vim, so this blog post is now considered outdated.

VIM is a very powerful editor, and a standard developing tool for Linux users. Meanwhile VIM is also available for Windows. The good thing is, it is very customizable and can do almost everything. The bad thing is, by default most features are turned of, because it depends on your use case of the editor, what features should be turned on or off.

I’m a C/C++ developer, so I configured it for developing. Just store this file in your home directory or add it to your existig .vimrc. VIM will read this file at startup and configure it for C/C++ developing.

What this configuration offers:

  1. Visual: Syntax-highlighting and line numbers
  2. Editor Configuration: tab expanding to space, tabwidth 4, linewrap at 120 characters, C indentation, simple save file using [F2] (works in insert mode and normal mode)
  3. Navigation: Goto-Definition with [F12]. The word under the cursor is looked up using the ctags index file.
  4. Autocompletion: Therefore omnicppcomplete has to be installed additionally. See comments in .vimrc.
  5. [F5] automatically generates the tags file for the current folder.
  6. Intelligent Comments: Just type /* Text. The next * will be inserted automatically. / closes the comment automatically.
  7. Doxygen Comments: Creating doxygen comments for function under cursor using [F6]. Therefor DoxygenToolkit needs to be installed. See comments in .vimrc.
  8. Spell Checking using vim-spell: Use Alt-Down/up to jump from own spelling error to the next/previous. Use zg to add a good word, z= to get suggestion.

Another thing that works out of the box: Press SHIFT-K to lookup the function under cursor in the manpage – that’s the integrated help 🙂

Vim with OmniCppComplete

Vim with OmniCppComplete


 1 ” VIM Configuration File
 2  Description: Optimized for C/C++ development, but useful also for other things.
 3  Author: Gerhard Gappmeier
 4
 5
 6 ” set UTF-8 encoding
 7 set enc=utf-8
 8 set fenc=utf-8
 9 set termencoding=utf-8
10 ” disable vi compatibility (emulation of old bugs)
11 set nocompatible
12 ” use indentation of previous line
13 set autoindent
14 ” use intelligent indentation for C
15 set smartindent
16 ” configure tabwidth and insert spaces instead of tabs
17 set tabstop=4        ” tab width is 4 spaces
18 set shiftwidth=4     ” indent also with 4 spaces
19 set expandtab        ” expand tabs to spaces
20 ” wrap lines at 120 chars. 80 is somewaht antiquated with nowadays displays.
21 set textwidth=120
22 ” turn syntax highlighting on
23 set t_Co=256
24 syntax on
25 colorscheme wombat256
26 ” turn line numbers on
27 set number
28 ” highlight matching braces
29 set showmatch
30 ” intelligent comments
31 set comments=sl:/*,mb:\ *,elx:\ */
32
33 ” Install OmniCppComplete like described on http://vim.wikia.com/wiki/C++_code_completion
34 ” This offers intelligent C++ completion when typing ‘.’ ‘->’ or <C-o>
35 ” Load standard tag files
36 set tags+=~/.vim/tags/cpp
37 set tags+=~/.vim/tags/gl
38 set tags+=~/.vim/tags/sdl
39 set tags+=~/.vim/tags/qt4
40
41 ” Install DoxygenToolkit from http://www.vim.org/scripts/script.php?script_id=987
42 let g:DoxygenToolkit_authorName=“Gerhard Gappmeier <gerhard.gappmeier@ascolab.com>”
43
44 ” Enhanced keyboard mappings
45
46 ” in normal mode F2 will save the file
47 nmap <F2> :w<CR>
48 ” in insert mode F2 will exit insert, save, enters insert again
49 imap <F2> <ESC>:w<CR>i
50 ” switch between header/source with F4
51 map <F4> :e %:p:s,.h$,.X123X,:s,.cpp$,.h,:s,.X123X$,.cpp,<CR>
52 ” recreate tags file with F5
53 map <F5> :!ctags -R –c++-kinds=+p –fields=+iaS –extra=+q .<CR>
54 ” create doxygen comment
55 map <F6> :Dox<CR>
56 ” build using makeprg with <F7>
57 map <F7> :make<CR>
58 ” build using makeprg with <S-F7>
59 map <S-F7> :make clean all<CR>
60 ” goto definition with F12
61 map <F12> <C-]>
62 ” in diff mode we use the spell check keys for merging
63 if &diff
64   ” diff settings
65   map <M-Down> ]c
66   map <M-Up> [c
67   map <M-Left> do
68   map <M-Right> dp
69   map <F9> :new<CR>:read !svn diff<CR>:set syntax=diff buftype=nofile<CR>gg
70 else
71   ” spell settings
72   :setlocal spell spelllang=en
73   ” set the spellfile – folders must exist
74   set spellfile=~/.vim/spellfile.add
75   map <M-Down> ]s
76   map <M-Up> [s
77 endif
78
Edit 2012-02-06: Due to the great demand I uploaded vim-confg.tar.gz which contains .vimrc, the .vim folder with the wombat256 color scheme, DoxygenToolkit plugin and git plugin. This file contains also other stuff in comments which you can comment in again to activate it.
Advertisements

35 Responses to “Minimal .vimrc for C/C++ developers”


  1. 1 Piero Contezini October 3, 2009 at 4:48 am

    You should consider include the files itself for download, cutting every number is a pain!

  2. 3 Juri December 7, 2009 at 10:24 pm

    actually, if you’re using vim, the only thing you should know about is ctrl+v.
    one of the possible ways is:
    1. copy the whole vimrc posted here
    2. open your (g)vim
    3. type literally this -> “*p
    4. then -> gg
    5. ctrl+v
    6. use your navigation keys to select a rectangle and press “d”or simply do -> “Gld”

    😉

  3. 4 SuperJdynamite December 8, 2009 at 6:19 pm

    In VIM, %s/^\d\d //g will remove the leading line numbers.

  4. 5 Mike February 4, 2010 at 10:48 pm

    The above comment suggests:

    In VIM, %s/^\d\d //g will remove the leading line numbers.

    That will only remove the 2 digit line numbers.

    This will remove both:
    %s/^\d\d* //g

    • 6 gergap February 5, 2010 at 8:07 am

      That’s right. Also “%s/^\d? //g” will do it 😉

      • 7 Mike February 6, 2010 at 4:24 pm

        Yikes – I just tried this, and I made a mistake in my original post. It looks like a space gets copied over in front of the single digit numbers:

        Try:
        %s/^\s\=\d\+ //g

        \= (matches 0 or 1)
        \+ (matches 1 or more)

  5. 8 mymaydayya@Taiwan March 17, 2010 at 9:56 am

    thanks
    it works great

  6. 9 Patrik January 26, 2011 at 6:31 am

    Following up on the different ways to remove numbering. For tasks like this I usually make a quick mapping of the keystrokes required to change one line and then move to next.
    :map q 3xj
    then I just hold ‘q’ until I’ve fixed the numbering in this case.

  7. 10 Adrian October 25, 2011 at 11:19 am

    OMG. Why put the line numbers there in the first place? I already have something to do today.

    • 11 gergap November 8, 2011 at 8:54 am

      1.) It looks better
      2.) You can easily refer to line number when having a question
      3.) You can remove the line numbers just with one command in vim (see comments below)
      4.) Nobody says you have to use this configuration.

      Criticism is welcome and requested, but please stay polite and objective.

  8. 12 ram November 7, 2011 at 6:08 pm

    thanks, its good but when i installed, it says an error|:

    Error detected while processing /home/karthik/.vimrc:
    line 24:
    E185: Cannot find color scheme wombat256
    Press ENTER or type command to continue

    can the inventor or anyone help me,please ?

    Ram (on 07-Nov-2011)

    • 13 gergap November 8, 2011 at 8:49 am

      Either comment out this line, or use the wombat256 colorscheme like described in the next post: https://gergap.wordpress.com/2009/06/01/new-vim-color-scheme-wombat256

    • 14 Mike November 8, 2011 at 3:20 pm

      It’s nice to see new people trying out vi. I might be nostalgic, but it’s one of my favorite editors, and I pretty much can’t live without it.

      Gergap is right – you can comment out that line, you can download wombat256, or you can change the scheme to something currently loaded on your system.

      Color schemes are located in 2 places. Either in your ~/.vimrc/colors directory (if you’ve put them there), or in $VIMRUNTIME/colors (these are available to everybody on your system. You can find $VIMRUNTIME by typing in ‘vim –version’.

      If you want to play around with colors, I recommend checking out the Color Sample Pack (it has wombat256):
      http://www.vim.org/scripts/script.php?script_id=625

  9. 17 reza November 17, 2011 at 8:15 am

    any one knows how to comment a block in vim?

  10. 19 ki113d February 4, 2012 at 1:50 pm

    I am having issues installing DoxygenToolkit.
    I have copied it to ~/vim/autoload but issuing the command
    :DoxLic or :Dox return an error stating that the command
    does not exist.

    Any help would be greatly appreciated 🙂

  11. 21 Lukas April 2, 2012 at 1:25 pm

    Hey. The http://fyels.de/s4Y link seems to be down. Would you mind uploading a new one? Thank you 🙂

  12. 22 yan May 18, 2012 at 3:12 am

    Can you upload your resource file again? The link you posted seems to be broken. Thanks.

  13. 25 Stuart July 23, 2012 at 1:10 am

    Thanks much

  14. 26 Dmitry October 4, 2012 at 7:45 am

    Very good!!!!(beginer programmer from Russain) A lot of Thanks!!!!!

  15. 28 syntax August 14, 2013 at 3:44 pm

    how does shift K work and it won’t work for me?

    • 29 gergap August 14, 2013 at 4:33 pm

      Just move to cursor to the function where you need help e.g. “printf” and press SHIFT-K. Vim will open the manpage for it if one is found. This is the same as typing “man printf” on the shell.

      Note some function exists in man(1) and in man(3). You can force to open the desired manpage e.g. “man 3 printf” by typing “3” + “SHIFT-K”.

  16. 30 Vladimir Krivopalov October 28, 2014 at 5:15 pm

    Thank you a LOT for sharing your config and providing meaningful explanations!
    These helped me a lot in setting up my own one which now resembles yours quite a lot 🙂

  17. 32 nmars January 4, 2015 at 9:54 pm

    I’m having trouble with the clang-complete plugin. Every time I open a .cpp file I get the following message:

    Error detected while processing function 23_ClangCompleteInit:
    line 51:
    clang_complete: conceal feature not available but requested
    Loading libclang failed, completion won’t be available. Are you sure ‘/usr/lib64/llvm’ contains libclang?

    I figured I just needed to point the plugin to the right directory so I changed the path in the .vimrc file (line 86) to “~/vim/.vim/bundle/clang_complete/plugin”, which is where I found libclang.py. Problem is I still get the same error message but with the new directory.

    Is there something else I need to do to get this to work?

    Thanks for this by the way — the installation was smooth and everything else seems to work well.

    • 33 gergap January 4, 2015 at 10:09 pm

      Hi,

      You need Vim 7.3 or higher, compiled with python support and ideally, with
      the conceal feature.
      From your error message I would guess that your vim was not compiled with conceal feature turned on.
      Either recompile vim or disable the clang_complete plugin if you don’t need it.

  18. 34 Baloo March 4, 2015 at 6:10 am

    @nmars

    Follow the link, I found it beneficial for the same problem that I encountered.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: