Reputation: 1324
For example, when I type 'inclu', is there a method which can complete it to 'include'? Thank you.
Upvotes: 5
Views: 4069
Reputation: 7515
You could set up some Vim abbreviations to do this for you.
iab inc #include ""
iab Inc #include <>
Then by typing inc<SPACE>
Vim will automatically expand this to #include ""
. You could add all kinds of abbreviations to reduce the number of keystrokes required.
If you want to push this further, I'll share a little ongoing experiment of mine to create a C++ 'shorthand'. Though it certainly reduces the number of keystrokes required to input code, I haven't used this long enough to determine if the added mental complexity of remembering the shorthand is worth it. (Probably not!)
You will need to install UltiSnips for this to work. You could probably do most of this using Vim's abbreviation feature, but I've found it works best with a dedicated snippet completer.
Once UltiSnips is installed you will already have some C and C++ snippets ready to use, including the #include ""
example mentioned above.
I've added the following (plus many, many more!) to my cpp.snippets file.
snippet sptr "std::shared_ptr< type >"
std::shared_ptr< ${1:type} >
endsnippet
When I want a shared_ptr I type sptr
and press the snippet completion key. UltiSnips completes this as std::shared_ptr< type >
with the word type
selected so I can continue entering the template type without pausing.
This contrived example shows some more of the shorthand. This is what I could type:
// <C> is where I would press the UltiSnip complete/next key.
fun<C>sptr<C><C>load_widget<C>cu32<C>offset<C>, cc<C>name<C>ret<C>msptr<C>widget<C>offset<C>name<C>;
Which expands to:
std::shared_ptr< widget > load_widget( const std::uint32_t offset, const char* name )
{
return std::make_shared< widget >( offset, name );
}
A rough count shows this reduces approximately 136 keystrokes down to 100. With Omnicomplete or YouCompleteMe plugins, this is reduced further as variable and function names can often be completed after the first 2 or 3 characters have been entered.
I try to stick with this pattern when creating a shorthand for types: [c]type[r|p]
. [c]
is optional const, [r]
or [p]
for optional reference or pointer. (r
and p
and easier for my to type than &
and *
). For example, std::string
has the following shorthand:
str = std::string
strp = std::string*
strr = std::string&
cstr = const std::string
cstrp = const std::string*
cstrr = const std::string&
So cstrr
, the most common of these in my code, reduces 19 keystrokes (there's a trailing space) down to just 6.
Upvotes: 0
Reputation: 1581
Snipmate does this by default. type 'inc' and press Tab, it will convert into
#include <stdio.h>
Upvotes: 1
Reputation: 77
You may want to try the Vim plugin Supertab. If you do, I suggest installing it using Pathogen.
Upvotes: 2
Reputation: 3629
Maybe, any plugin like Snipmate or UltiSnips will be helpful for you.
Upvotes: 2
Reputation: 104050
Most of vim
's (complex) auto-completion is done via the ^X
key mapping. ^X^]
will autocomplete based on tags generated by ctags(1)
. ^X^P
looks for previous keywords in the file that can be used for completion. ^X^K
looks into a configurable dictionary
for completion words. ^X^I
looks into included files and pops up a menu for completing keywords from within those files. ^X^D
completes from #define
.
Perhaps the simplest way to get what you're after is to fully type #include <...>
once in your file. The second file to be included could then be handled via #incl^X^P
and then keep going.
If you want to put slightly more effort into it, create a ~/.vim/dict
file with the keywords you want to autocomplete, add the file to the dictionary
variable (:help dictionary
), and use ^X^K
to insert it.
Upvotes: 3