34

I really like clang_complete for this. It does require clang, and you need to tell it where libclang resides in your system. After that, it works wonderfully. People might suggest YouCompleteMe, but to be honest, that plugin is hugely bloated for what it says it does, and it requires way too many steps to install. I also had it segfault Vim on multiple ...


19

Exuberant ctags is the simplest way to achieve this. Under GNU/Linux (e.g. Ubuntu or Debian) you should be able to just do sudo apt-get install exuberant-ctags (For OSX "$ brew install ctags" should suffice; for Windows you might want to visit http://ctags.sourceforge.net/ and download the standalone executable) Then navigate to your project's root ...


11

clang-format is a command line tool that formats c++ code. One of the default formatters is google. clang-format -style=google There are plugins that would allow you to run clang-format in vim. One such plugin is https://github.com/rhysd/vim-clang-format (I have not used it, it was the first I found)


10

Here's how I would do it. Add this to your .vimrc vnoremap if [[O][ Explanation: vnoremap means map the left hand side if to the right hand side [[mO][ while you're in visual mode. if means In Function, although you can rename this if you like. [[ jumps to the beginning of the function. O moves to the other end of your visually selected text, and then ][ ...


10

set nocp is not actually needed if your vimrc is located at ~/.vimrc or ~/.vim/vimrc. Many of your commands can be abbreviated even further sy on colo torte se rnu nu nowrap hi=1000 cb=unnamed si ts=4 sts=4 sw=4 sr is hls nn <CR> :noh<CR><CR> ino {<CR> {<CR>}<C-o>O


9

Finally found it! Completing parameters is not a part of the YCM behaviour. See this YCM issue. There's an old fork from oblitum which could grant this feature. That's what misled me. Unfortunately, this part of the fork seems unmaintained and after a few tries I couldn't make it work. So, I tried out clang_completer which has the parameter completion ...


8

YouCompleteMe (Link) plugin has been work great for me. It uses libclang to generate the autocomplete feature, providing accurate completion. It has a lot of customization, specially when working with compilation flags. You can edit the "flag generator" editing a python script per project (Example). But, to me the main advantage is that it supports Clang ...


8

DJ McMayhem's solution inspired me to write my own that relies on ctags and on matchit to do a proper analysis of function boundaries. The difficult part has been already done by lh-dev and lh-tags for several years: the current file is parsed through ctags with the right options we look for all function definitions in the tags database which is restricted ...


7

As we cannot expect C++ code to have their function-scope brackets on the first column, we cannot rely on the default behaviour of ]] and ][. A few weeks ago I've added this feature into lh-cpp. I won't describe here how it works as I've done it in answer to another similar question on vi.SE: Quick search, limited to a C++ function (the idea is that I rely ...


7

The first line really should be runtime defaults.vim. This is a standard file and it must be present in all Vim installations. Among other things, it saves from typing syntax on filetype plugin indent on set hi=200 set is Also set cb=unnamed is evil (just prepend "+ before normal mode command when needed) and set hls IMO is more harmful (every time ...


6

Skeleton text from an existing file can be added to a new file by inserting the line autocmd BufNewFile *.cpp r C:\(full file path of skeleton text file) into the vimrc. The BufNewFile event does not work if the new file is created in two steps using :enew then :sav filename. Instead, creating a new file using :e filename triggers the BufNewFile event's ...


6

The g:ycm_show_diagnostics_ui variable controls this: The *g:ycm_show_diagnostics_ui* option When set, this option turns on YCM's diagnostic display features. See the _Diagnostic display_ section in the _User Manual_ for more details. So, do: let g:ycm_show_diagnostics_ui = 0


6

I cannot provide an answer different to the one I gave in other similar questions. While tag-based plugins may present a complete list of existing overloads in a way much more ergonomic than :tselect (they may even provide a filter option like my lh-tags plugin), they cannot help restrict the list of overloads to the one matching the types of the objects ...


5

YouCompleteMe isn't that hard to install, and it has been quite stable for me, I've migrated from clang_complete to YouCompleteMe years ago. One thing I agree, it's indeed bloated with a huge commit history, many internal dependencies and support for many languages you may not care for but still will end up cloning its associated submodules. That, that's an ...


5

It can be done by testing the syntax name behind the variable to replace: :%s/variable/\=(match(map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")'), '\cstring')==-1 ? 'NEw' : submatch(0))/g But nobody will want to use that. Instead, we'd better define a new command. :command! -bang -nargs=1 -range SubstituteUnlesString \ <line1>,<...


5

You can force a motion to be linewise; then, the % would include the full last line, including the trailing ;, for example yV%. Unfortunately, if you use the matchit plugin, that doesn't work any more, but you can instead use visual mode: V%y (interestingly, this is the reverse of the above!) Alternatively, you could define a custom motion for that.


5

Indeed, template expander plugins will help you in this task. :read is the step 0 of template expander plugins. For instance, mu-template will try to include foo.h from foo.cpp if foo.h is detected. It'll also permit to expand headers with dynamic data within them (date, author, license/copyright, etc).


4

Finding the beginning and end of a function can be difficult, especially in a language without a function keyword… and many conflicting indent styles. If your function ends with a closing brace alone on its own line (as in 10 out of the 13 styles listed here), you could visually select it with something like this: xnoremap if /^\s*}<CR><Esc>V% ...


4

The documentation (:help ctrl-]) says: When there are several matching tags for {ident}, jump to the [count] one. When no [count] is given the first one is jumped to. g] may not be what you are looking for but that's the best you can get in Vim. Basically, you can't expect ctags and Vim to understand your code, so you'll have to look for a smarter ...


4

The vim-jp/vim-cpp repo included this change in July 2016: syn match cNumber display contained "\d\('\=\d\+\)*\(u\=l\{0,2}\|ll\=u\)\>" syn match cNumber display contained "0x\x\('\=\x\+\)*\(u\=l\{0,2}\|ll\=u\)\>" syn match cNumber display contained "0b[01]\('\=[01]\+\)*\(u\=l\{0,2}\|ll\=u\)\>" If you don't want to use this repo, ...


4

" tell which cinoption take effect for current line com! WhichCinoption call s:which_cinoption() function s:which_cinoption() abort " test if cindent take effect if !empty(&indentexpr) echo "'indentexpr' exists, it overrides 'cindent'." return endif if !&cindent echo "'cindent' is currently disabled." return endif " ...


4

A variety of methods are at your disposal There are some very useful commands listed here, most notably [I, which will display all lines in the current and all included files containing the keyword under the cursor. See :h [I and :h include-search. You can :grep or :vimgrep to search for occurrences of a particular symbol (or any pattern for that matter), ...


4

So far I can name two solutions to the problem of finding either the declaration or the definition of a function. I know there is another well know tags based solution, but as I don't use it, I'll let others give it to you. The more trendy one first: YouCompleteMe has a :GoToDefinition and a :GoToDeclaration pair of commands. The one I use (it's hard to ...


4

I'm not aware of anything direct and foolproof. It may very well exist in a plugin somewhere, of course. ][ will take you to the next closing curly brace in column one. If your code is properly formatted, that's almost certainly the end of the function but the movement doesn't pay any attention to the syntax of any other characters at all. It just walks ...


4

You could put setlocal iskeyword-=: inside ~/.vim/after/ftplugin/cpp.vim Loads automatically when you open files where the filetype is cpp. If it doesn't work, try setting filetype plugin on. Update: Or you could do autocmd FileType cpp :setlocal iskeyword-=: In your .vimrc, but I recommend you put it in a ftplugin - it keeps your .vimrc clutter-free.


4

:make doesn't have to actually use a Makefile. Try something like: :let &makeprg = "g++ *.cpp -o [Filename]" You can then run the command with :make and if desired you can set up a normal mode mapping for this with something like: :nnoremap <leader>m :make<CR> See :help 'makeprg' for more details. You'll probably also want to ensure '...


4

It indeed looks like cppOperator links to Operator which links to Statement (-> :hi cppOperator), but I've never seen these operators displayed differently... It's just for the keywords. See syn list cppOperator. I guess you'd have to add these symbols to the cppOperator list. You may need a syn match that takes care of not overlapping with cFloat and some ...


4

Indentation of C++ (and also C, Objective-C and even Java) is typically handled by enabling the 'cindent' option, which uses Vim's built-in C-indenting implementation, which recognizes elements of these languages (such as curly braces, semi-colons, etc.) and uses them as cues to decide whether to indent or unindent a specific line. This indentation engine ...


4

(I've answered this question so many times that I've lost the count, and impossible to find a complete answer in the lot... I'll bookmark this answer this time...) Everything starts with Vim quickfix feature which permits to integrate compilation, and to navigate error messages. The starting point is :make. To navigate error messages, you'll need , :copen, :...


Only top voted, non community-wiki answers of a minimum length are eligible