i am trying to write a vim abbreviation to create a template program in c

I have given the abbreviation as below in the .vimrc

abbr main #include<stdio.h>
\^Mint main() {
\^M int T, N;
\^M scanf("%d", &T);
\^M while(T-- > 0) {
\^M     scanf("%d", &N);
\^M }
\^M return 0;

When I type the word main, the abbreviation is coming into effect, but it's not indented properly. I tried with just spaces/just tabs/combination but nothing gets the code indented perfectly. Why?

abbr distorted

Any help?

Update: I tried with

 abbr main #include<stdio.h>
\<CR>int main() {
\<CR>int T, N;
\<CR>scanf("%d", &T);
\<CR>while(T-- > 0) {
\<CR>scanf("%d", &N);
\<CR>return 0;

It leads to the same result. Can anyone please show me how to add a multi-line abbreviation with proper indentation?

  • 1
    isn't ^M, window's newline character? is there a reason, you are having \^M instead of unix styled spaces?
    – user93868
    Aug 8, 2015 at 17:05
  • Hi @Madhavan, if I didn't type that, then all the lines are getting cramped up in single line. Hence i added a new line by pressing Ctrl + V followed by Enter key.
    – mtk
    Aug 8, 2015 at 17:50
  • why don't you try \n and for inner blocks, add an extra \t after newline char... in emacs, \n and \t is what people use
    – user93868
    Aug 8, 2015 at 17:55
  • @Madhavan Thanks for your reply. I tried with \n, but no success yet. All lines get cramped in single line.. : (
    – mtk
    Aug 8, 2015 at 18:03

1 Answer 1


Actually I think that your question is a XY problem: I think that your actual problem isn't How can I do a multiline abbreviation? but How can I easily add a sample function with a few keystrokes?. And if you want to do that, you shouldn't use abbreviations but snippets.

Why not an abbreviation?

First let's take a look at :h abbreviations:

[abbreviations] can be used to save typing for often used long words. And you can use it to automatically correct obvious spelling errors.

Abbreviations are made for changes smaller than a whole function, it is simply not there goal. What you're trying to do is typically to create a snippet, and some great tools already exists so you don't have to reinvent the wheel.

What is a snippet?

To keep it clear and concise I'll quote wikipedia:

Snippet is a programming term for a small region of re-usable source code, machine code, or text.

How do I get some snippets?

First you need a repository of snippets. Of course you can manually create them one by one, but Vim has a cool community which has already struggled with your problem and solved it.

So vim-snippets is what you are looking for: With hundreds of snippets for more than 60 languages (including a C main() function) it is a great base to begin with. Of course you can add your own snippets if you find one is missing.

How do I use these snippets?

Now that you have your snippets, you need a snippet engine which will allow you to add them in your buffers. The readme of vim-snippets has a section dedicated to the supported engines.

I would recommand ultisnips but that is only a personnal matter and you should try the different engines suggested to see which one is your favorite.

How do I get that?

If you're using a plugin manager it's pretty easy. For example with vim-plug you simlpy add this line to your .vimrc:

Plug 'SirVer/ultisnips' | Plug 'honza/vim-snippets'

Then you can follow the basic configuration recommanded in the ultisnips readme:

let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<c-b>"
let g:UltiSnipsJumpBackwardTrigger="<c-z>"

And after using :PluginInstall you can start to use your snippets with tab.

  • 1
    Also I think you might be interested in the SE site dedicated to Vim for your future questions about our beloved editor ;-)
    – statox
    Sep 11, 2015 at 16:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .