Configuring Vim for C++

Ideal C Setup for Vim

You should drop the : it's not needed in your ~/.vimrc, the correct line should be:

filetype plugin indent on

The plugin part loads additional filetype-specific plugins that often provide useful commands/mappings/settings.


8 characters is actually the universal and historical default width for a <Tab>. If you want it to be displayed shorter (which I can understand), you'll have to add these lines to your ~/.vimrc:

set softtabstop=4              " see :h 'softtabstop'
set shiftwidth=4 " see :h 'shiftwidth'

There's a comprehensive explanation in :h 'tabstop'.

Note that it doesn't change anything to the actual content of the file.


I use DelimitMate but there are many "auto closing" plugins. If you are satisfied with AutoClose and need that feature, there's no reason to change, I guess. But you should know that a naive but working implementation of that concept can be achieved with a handful of custom mappings like:

inoremap ( ()<Left>

This is the minimal ~/.vimrc that I put on every server I work on. It is small but it sets a number of super useful options like hidden or incsearch.

set nocompatible
filetype plugin indent on
syntax on
silent! runtime macros/matchit.vim
set autochdir
set backspace=indent,eol,start
set foldenable
set hidden
set incsearch
set laststatus=2
set ruler
set switchbuf=useopen,usetab
set tags=./tags,tags;/
set wildmenu
nnoremap gb :buffers<CR>:sb<Space>

To know what each option does, just do :h 'option (with the single quote) and add it to your ~/.vimrc only if you understand what it does and you actually need it.

Generally, learning how to use the documentation is key.

.vimrc for C developers

how about this?

https://mislav.net/2011/12/vim-revisited/

set nocompatible                " choose no compatibility with legacy vi
syntax enable
set encoding=utf-8
set showcmd " display incomplete commands
filetype plugin indent on " load file type plugins + indentation

"" Whitespace
set nowrap " don't wrap lines
set tabstop=2 shiftwidth=2 " a tab is two spaces (or set this to 4)
set expandtab " use spaces, not tabs (optional)
set backspace=indent,eol,start " backspace through everything in insert mode

"" Searching
set hlsearch " highlight matches
set incsearch " incremental searching
set ignorecase " searches are case insensitive...
set smartcase " ... unless they contain at least one capital letter

Configuring Vim for C++

  • Code complete: Omni completion or Clang autocomplete or YouCompleteMe
  • Real time syntax checking: Syntastic
  • Switching between source and header file: A plugin
  • Snippets: Snipmate or UltiSnip
  • Search for reference of variables, functions, classes, etc.: Cscope
  • Go to definition: Ctags or part of YouCompleteMe subcommands mentioned above
  • Refactoring tools: Refactor, lh-refactor
  • Useful text objects: Arg text object and Class text object
  • C++ category in Vim Tips wiki
  • Luc Hermitte's C/C++ plugin
  • Not C++ specific but I also recommend either FuzzyFinder or Command-T or Unite for file navigation. With either of these, you don't even need tabs (which does not scale for 10+ files) to manage your project.
  • Class navigation: Taglist or Tagbar

Edit: Updated as of July 2013

How to configure Vim for C++ development?

For your first problem: I suspect that you didn't extract all the files in the archive (that c.vim came in). The c.vim documentation (README.csupport) says:

The subdirectories in the zip archive
cvim.zip mirror the directory
structure which is needed below the
local installation directory
$HOME/.vim/ for LINUX/UNIX
($VIM/vimfiles/ for Windows)

This means that you need to uncompress the entire archive as it is into your vimfiles directory.

There are some other steps to follow, detailed in the documentation.

As for your second issue: you need a Makefile to do that. If you have never done this before, I suggest using cmake to generate a Makefile. You will also need GNU tools for Windows; Cygwin or MinGW are the most popular choices. I haven't use them, it is easier to do all this on some *nix OS :).

When done, use :cd (if you are not in your working directory), and :make. Use :cl to list the compiler output, :cn to jump to the next error. There are some other useful commands for compiling. You might find these resources useful:

  • StackOverflow: Recommended plugins for C coding
  • Compiling from Vim
  • C editing with Vim

Also, I found the Nerd Commenter a very useful companion.

I found that Vim acts somewhat like alien on Windows; it is designed for an *nix-like operating system. I think it is possible to craft a similar environment for it, and use it mostly successfully, but it is so much easier to do on some linux, as it is "instantly home" there.

Anyway, if you wish to stick with Windows, I think you can find a way to accomplish what you want. Good luck.

How can I configure Vim to compile C code using Borland's compiler on Windows?

I've not used the bcc32 compiler, but I'm assuming it uses a different make tool than make. You'll need to put:

set makeprg=<make command>

Somewhere in your .vimrc file.

How to configure vim for embedded programming?

I was trying to compile main.c with gcc, but then I compiled it with avr-gcc instead. Warnings of "file not found" didn't go away, but it compiled successfully.

using vim as a c++11 IDE

I think you will need Ultisnips, Tagbar and tcomment plugin, especially Ultisnips.

More information about configuration, please refer:https://github.com/xautjzd/dotvim

set Vim as a basic C++ Editor

Some plugins that might help you and I tried in the past when I was trying to get started with vim long ago:

IDE: http://www.vim.org/scripts/script.php?script_id=213

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

Debugging: http://www.vim.org/scripts/script.php?script_id=3039

Completion: http://ctags.sourceforge.net/ and http://www.vim.org/scripts/script.php?script_id=1520

Statusbar: http://www.vim.org/scripts/script.php?script_id=3881 and its successor http://usevim.com/2013/01/23/vim-powerline/

You can search for further plugins at http://www.vim.org/scripts/index.php

That being said, I use vim just fine without any plugin for daily C++ development. It is also handy because I can use the same workflow when ssh'ing onto a server or someone else's machine without the consideration of major differences.

Also C++ syntax highlight works by default as such plugins for languages are usually included into the distributed vim, already.

vim c++ configuration

Some of the most important IDE's features are:

  • Editing
  • Searching
  • Tags ("Goto Definition" in Visual Studio)
  • Source Control Integration
  • Building
  • Debugging

Emacs has typically tried to accomplish all of these things and more (including running a shell inside Emacs). Vi has typically been more lightweight but has gotten more functionality since the advent of Vim. I have not seriously tried to use Vim for building or debugging -- I find it difficult when I go to new projects that are using different build and debug environments.

However, there are many things that I do to integrate Vim with various IDE's and stay in Vim as much as possible.

See https://stackoverflow.com/a/8897164/1113528 for Tags, Source Control Integration, Clipboard, IDE integration (ability to jump to build errors). I also have a custom search plugin -- I think you would be better off looking for a more standard search vim plugin.

In order to switch between Visual Studio, Eclipse and Vim, I setup external spawning of Vim for Ctrl+Shift+V. To set this up, do the following:

Visual Studio

  1. Tools | External Tools | Add
  2. Title:Vim | Command:C:\Vim\vim73\gvim.exe | Arguments:--servername ext --remote-silent +$(CurLine) $(ItemPath)
  3. Move up to first position
  4. Tools | Options | Environment | Keyboard
  5. Show commands containing: Tools.ExternalCommand1
  6. Press shortcut keys: Ctrl+Shift+V
  7. Apply, OK

Eclipse

  1. Run | External Tools | External Tool Configurations
  2. Location: C:\Vim\vim73\gvim.exe
  3. Arguments: --servername ext --remote-silent ${resource_loc}
  4. Window | Preferences | General | Keys
  5. Filter Run Last
  6. Binding Ctrl+Shift+V
  7. Apply, OK


Related Topics



Leave a reply



Submit