To use Vim as an editor for C/C++ (and other programming languages as well), we need to extend Vim with some handy plugins. Vim is a powerful editor, it really is, but you need some plugins to make it more powerful to your needs. Vim is not a code editor out of the box, so you can not expect to have cool stuff like Visual Studio's intellisense, or jumping to definition of a function and so on. So to make our coding life easier, we can install Vim plugins to have more fun while coding.
First, here's a list of Vim plugins below I use it for writing code:
Matchit
This is a nice plugin too, especially when dealing with html files. It searches for matching tags using the existing % key. In a C file, if you're on one of the brackets like {}, [], () and so on, you'll see those brackets highlighting in pairs. When hitting % the cursor jumps to the matching pair, but normally this won't work for a html tag. Without this plugin this won't work for a html tag.
Link: http://www.vim.org/scripts/script.php?script_id=39
SnipMate
Snipmate is really one of my favourite, it's simple but yet so powerful, it's brilliant. You simply download the plugin, copy it to your .vim folder and you're ready to go, just that simple! No, extra additions to .vimrc, no long docs reading, just download, copy and start playing. When you've done that, open a C file and type as follows:
for and than hit the tab key, voilĂ ! A complete for loop snippet is placed and we're not finished yet! You can replace the default words by hitting tab again, change the word in the selected field, than hit tab again, edit the selected field until you're done. This is fantastic!
Link to download snipmate plugin: https://github.com/garbas/vim-snipmate
Surround
This plugin is a must-have when writing html code, it surrounds a selected block of text/code with div tags (in case you're writing html code) or brackets (in case you're writing C code). It's also very easy to use.
For example, type a simple text and than go back to normal mode and type: yss"
When using visual selection (v or V) than use capital S for surrounding, i.e. veS' is the same as ysw'.
The first one is in visual mode and the second one is in normal mode.
The key combinations can be seen as follows:
ysmn surround word m with tag n
csmn change surround with tag m to tag n
dsn delete surround with tag m
For more detailed information go to link: https://github.com/tpope/vim-surround
NERDTree
This plugin works as a file browser, you open it, search the file you want to edit and than hit enter. The file will be displayed and you can edit all along. So, you don't have to get out of Vim to look for files to edit, you simply stay in Vim, open NERDTree and look for the files. But I tell you this, a real good experienced Vim user doesn't even need NERDTree to do such a magic.
I've seen die-hard Vim users loading a whole bunch of files at once and switch buffers as they drink coffee and do crazy stuff! Vim is so powerful, you can't call it just an editor, it's actually an editor that you have a conversation with. When you say, 'dd', it responds with "listen baby, if you want I can also delete multiple lines", than you move 4 lines down and Vim says "baby, just say 4j and I do what you want, but anyway sweety, you want me to delete this line too? Just press . and I'll do it for you". This is Vim and I'm also a beginner, Vim is just incredibly good, you just need to be patient with her and once you have control over her, you can let her do whatever you want ;-)
By adding this line .vimrc, you can toggle NERDTree on or off by pressing on the F4 key:
:noremap :NERDTreeToggle
Link: https://github.com/scrooloose/nerdtree
TagList
TagList plugin is a sourcecode browser, it shows you a list of classes, defines, global variables, functions, methods etc. for the currently displayed file. This a very useful feature, this way you have an overview of all the functions and you can navigate to a function or a method easily. If you want to jump to a function and edit right away, you simply move to the function name in the taglist and hit enter. If you just want to view the function without jumping to it, you simply press 'p' key (which means preview), this way you stay in the taglist window.
It supports most programming languages, so it's a simple and useful plugin without putting a bunch of tweaks in the vimrc file.
I added these lines in the .vimrc script, to enable and disable TagList with F5 key:
:noremap :TlistToggle
:let Tlist_Show_One_File = 1
:let Tlist_Exit_OnlyWindow = 1
:let Tlist_Use_Right_Window = 1
The last line tells TagList to show on the right side of the window.
Clang_complete
This plugin is important when developing C/C++ code. Clang_complete together with clang and libclang-dev is the best replacement for omnicppcompletion, the old completion plugin which doesn't work well. omnicppcompletion uses ctags to perform completion of code, but it's not perfect like Visual Studio's intellisense or Eclipse. But clang is here to save us, developed by llvm (www.llvm.org). First you need to install clang, I did the following:
#sudo apt-get install clang
#sudo apt-get install libclang-dev
Than download clang_complete from:
https://github.com/Rip-Rip/clang_complete
Copy the files to your .vim folder and you're done. That's actuall all I did.
I tested with BOOST C++ and it works without tweaking or adding creepy scripts, it just works! See below for an example:
First, here's a list of Vim plugins below I use it for writing code:
- Surround
- Matchit
- Snipmate
- Clang_complete
- NERDTree
- TagList
Matchit
This is a nice plugin too, especially when dealing with html files. It searches for matching tags using the existing % key. In a C file, if you're on one of the brackets like {}, [], () and so on, you'll see those brackets highlighting in pairs. When hitting % the cursor jumps to the matching pair, but normally this won't work for a html tag. Without this plugin this won't work for a html tag.
Put cursor on head tag and enable V mode |
Hit % to jump to the matching tag |
Link: http://www.vim.org/scripts/script.php?script_id=39
SnipMate
Snipmate is really one of my favourite, it's simple but yet so powerful, it's brilliant. You simply download the plugin, copy it to your .vim folder and you're ready to go, just that simple! No, extra additions to .vimrc, no long docs reading, just download, copy and start playing. When you've done that, open a C file and type as follows:
for and than hit the tab key, voilĂ ! A complete for loop snippet is placed and we're not finished yet! You can replace the default words by hitting tab again, change the word in the selected field, than hit tab again, edit the selected field until you're done. This is fantastic!
Typing 'for' before hitting TAB |
Result after hitting TAB |
Link to download snipmate plugin: https://github.com/garbas/vim-snipmate
Surround
This plugin is a must-have when writing html code, it surrounds a selected block of text/code with div tags (in case you're writing html code) or brackets (in case you're writing C code). It's also very easy to use.
For example, type a simple text and than go back to normal mode and type: yss"
Before typing yss" in normal mode |
After typing yss" in normal mode |
After typing yss} in normal mode |
After typing cs}) in normal mode |
The first one is in visual mode and the second one is in normal mode.
The key combinations can be seen as follows:
ysmn surround word m with tag n
csmn change surround with tag m to tag n
dsn delete surround with tag m
For more detailed information go to link: https://github.com/tpope/vim-surround
NERDTree
This plugin works as a file browser, you open it, search the file you want to edit and than hit enter. The file will be displayed and you can edit all along. So, you don't have to get out of Vim to look for files to edit, you simply stay in Vim, open NERDTree and look for the files. But I tell you this, a real good experienced Vim user doesn't even need NERDTree to do such a magic.
I've seen die-hard Vim users loading a whole bunch of files at once and switch buffers as they drink coffee and do crazy stuff! Vim is so powerful, you can't call it just an editor, it's actually an editor that you have a conversation with. When you say, 'dd', it responds with "listen baby, if you want I can also delete multiple lines", than you move 4 lines down and Vim says "baby, just say 4j and I do what you want, but anyway sweety, you want me to delete this line too? Just press . and I'll do it for you". This is Vim and I'm also a beginner, Vim is just incredibly good, you just need to be patient with her and once you have control over her, you can let her do whatever you want ;-)
By adding this line .vimrc, you can toggle NERDTree on or off by pressing on the F4 key:
:noremap
Example of NERDTree |
Link: https://github.com/scrooloose/nerdtree
TagList
TagList plugin is a sourcecode browser, it shows you a list of classes, defines, global variables, functions, methods etc. for the currently displayed file. This a very useful feature, this way you have an overview of all the functions and you can navigate to a function or a method easily. If you want to jump to a function and edit right away, you simply move to the function name in the taglist and hit enter. If you just want to view the function without jumping to it, you simply press 'p' key (which means preview), this way you stay in the taglist window.
It supports most programming languages, so it's a simple and useful plugin without putting a bunch of tweaks in the vimrc file.
I added these lines in the .vimrc script, to enable and disable TagList with F5 key:
:noremap
:let Tlist_Show_One_File = 1
:let Tlist_Exit_OnlyWindow = 1
:let Tlist_Use_Right_Window = 1
The last line tells TagList to show on the right side of the window.
TagList example on the right side |
Clang_complete
This plugin is important when developing C/C++ code. Clang_complete together with clang and libclang-dev is the best replacement for omnicppcompletion, the old completion plugin which doesn't work well. omnicppcompletion uses ctags to perform completion of code, but it's not perfect like Visual Studio's intellisense or Eclipse. But clang is here to save us, developed by llvm (www.llvm.org). First you need to install clang, I did the following:
#sudo apt-get install clang
#sudo apt-get install libclang-dev
Than download clang_complete from:
https://github.com/Rip-Rip/clang_complete
Copy the files to your .vim folder and you're done. That's actuall all I did.
I tested with BOOST C++ and it works without tweaking or adding creepy scripts, it just works! See below for an example:
Hey,
ReplyDeleteThanks a lot for the post, was very helpful. Just, maybe you should correct the :norename to include after :noremap and at the end.
Thanks a lot!!!
ReplyDeleteYou're welcome, glad it works for you. I'll correct the mistype.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete