146

I would like to make vim my C++ editor. I have very little experience working with it and need help in configuring vim to work with C++. I need such features as

  • code-complete (for stl and for my classes)
  • switching between .cc and .h files
  • may be some more tricks you, C++ and vim gurus, have.

May be you could provide some configs (with explanations), or links to tutorials, plugins I could make use of?

sandris
  • 1,363
  • 13
  • 27
Draco Ater
  • 20,820
  • 8
  • 62
  • 86
  • 11
    +1 for the sheer masochism of wanting to use vim as your IDE :) vim has its uses (especially for editing files remotely in my experience), but anyone who actually *wants* to use it for code editing has chutzpah in my view. – Stuart Golodetz Nov 21 '10 at 15:35
  • 49
    @sgolodetz: Then there are quite a lot of people with chutzpah out there. – Cascabel Nov 21 '10 at 16:35
  • 3
    @Jefromi: Yup I do realise that :) It has always seemed like masochism to me though...in a sort of slightly admirable way. – Stuart Golodetz Nov 21 '10 at 16:40
  • @wilhelmtell: I guess you could read it that way if you wanted -- actually I was just bored whilst doing some work on my doctorate and saw a post I felt like commenting on. Not really interested in starting a big debate on the merits of vim or otherwise, just stating a point of view. Personally I find vim helpful when editing the odd text file using Putty, but using it as my IDE doesn't seem in any way appealing. At the same time, I have a certain amount of respect for anyone who does use it for that -- they obviously have a higher pain threshold than I do. – Stuart Golodetz Nov 21 '10 at 21:32
  • 4
    In retrospect, I guess posting the equivalent of "I'm a bit dubious about vim" on a post likely to be frequented by a lot of vim enthusiasts might have been a little provocative -- it was meant to be a tongue-in-cheek comment rather than the start of an argument though. – Stuart Golodetz Nov 21 '10 at 21:44
  • 1
    I did give [this answer](http://stackoverflow.com/questions/149558/recommended-vim-plugins-for-c-coding/152448#152448) which was initially restricted to C development. However every thing I said is still valid for C++. – Luc Hermitte Nov 21 '10 at 22:23
  • `sudo apt install eclipse` :-) – Ciro Santilli OurBigBook.com Oct 28 '19 at 08:34

3 Answers3

169

Edit: Updated as of July 2013

Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
Thanh DK
  • 4,257
  • 3
  • 23
  • 18
  • I've fixed the link to my C&C++ ftplugins suite. Thanks for the "advertizing" :) – Luc Hermitte Nov 21 '10 at 21:25
  • Also I may recomend FSwitch plugin instead of A plugin. – Sergey Miryanov Feb 26 '13 at 04:13
  • 1
    Luc's lhCpp is incompatible with vundle (I think due to svn repo) and no install process I like personally - recommend checking out https://github.com/Raimondi/delimitMate – netpoetica Sep 15 '13 at 14:28
  • Because of all dependencies in my plugins suite, I highly recommend to install it with VimAddonManager (type `:InstallAddon lh-cpp`, or `call vam#ActivateAddons(['lh-cpp'])` in your .vimrc, and you should be done with it) . This way I can keep my (viml) code base DRY and focus on advanced features. – Luc Hermitte Jan 23 '14 at 16:35
  • 2
    +1 a very complete (and up to date) answer deserves it, it's nice to see some of the more reclusive members posting these great answers. How thorough your answer is, is quite helpful, while at the same time not overwhelming, even to a new vim user. – osirisgothra Jul 21 '14 at 10:58
  • 1
    How about vim-rtags? It's good for c++ code navigation, has a rename functionality. – P4C Jul 28 '16 at 09:57
  • I know this is an old thread, however useful for people stumbling here; here is a vimrc which I use for C/C++ and python development with autocompletion of code https://github.com/tbhaskar78/vimrc/blob/master/vimrc – Bhaskar Tallamraju Jul 28 '20 at 04:02
10

I'm using vim as my C++ editor, however I'm not using many 'exotic' stuff.

  • Regarding completion, I'm using the non-contextual ^P and ^N.
  • I have a bunch of user defined abbreviations for my C++ use, for example :

    abbreviate bptr boost::shared_ptr
    abbreviate cstr const std::string &
    
  • I have several functions for "code snippets" like things, for example :

    function! IncludeGuard()
      let basename = expand("%:t:r")
      let includeGuard = '__' . basename . '_h__'
      call append(0, "#ifndef " . includeGuard)
      call append(1, "#define " . includeGuard)
      call append(line("$"), "#endif /* !" . includeGuard . " */")
    endfunction
    
  • The only plugin I really couldn't live without is Command-T (which requires ruby support)

  • For easy .cc to .h switching, you can try this plugin
icecrime
  • 74,451
  • 13
  • 99
  • 111
4

NERDTree http://www.vim.org/scripts/script.php?script_id=1658

Exuberant ctags (vim already supports the hotkeys natively) http://ctags.sourceforge.net/

taglist: http://vim-taglist.sourceforge.net/

snipmate: http://www.vim.org/scripts/script.php?script_id=2540

I don't do omnicompletion just the usual ^n ^p stuff but there are plenty of resources to google for.

ThePosey
  • 2,734
  • 2
  • 19
  • 20