Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

183 lines (138 sloc) 4.462 kb

Advancer Abbreviation

Advancer Abbreviation is a Vim plugin to create code snippet quickly

Features

  • Support any character except space as abbreviation name.
  • Quick jump to next placeholder.
  • Easy to extend.

Installation

copy plugin/advancer-abbreviation.vim to ~/.vim/plugin

Usage

Use Shortcut <C-CR> or <S-CR> to expand the abbreviation or jump to next placeholder.

<S-CR> : Smart Expand or jump to next placeholder.
<C-CR> : Force Expand or jump to next placeholder.
<ESC>  : If the line contain placeholder but now it is blank, 
         then delete whole line and leave insert mode.

Options

g:AbbrShortcutSmartExpand 
    Default: ['<S-CR>']

    A list of expand keys. 
    for std.main will try expand std.main only.
    for std(main will try expand std(main and main.

g:AbbrShortcutForceExpand
    Default: ['<C-CR>'] 

    A list of force expand keys.
    for std.main will try expand std.main and main.
    for std(main will try expand std(main and main.

g:AbbrShortcutNoExpand
    Default: []

    Skip expand, jump to next placeholder.

g:AbbrShortcutEscape
    Default: ['<ESC>']

    A list of Escape keys

g:AbbrPlaceholders
    Default: ['\/\*TODO\*\/','#TODO#', "'TODO'", '<!--TODO-->']

    The placeholder regex pattern list.

g:AbbrAutoInit
    Default: 1

    Autocmd for AbbrInitSyntax and AbbrInitMapKeys
    au Syntax,WinEnter * AbbrInitSyntax
    au BufRead,BufNewFile * AbbrInitMapKeys

    AbbrInitMapKeys MUST be invoked, or the no shortcut will work.

g:AbbrSplitPattern         
    Default: '[()\[\]{}]'

    Use for SmartExpand, bracket is not consider as a word.

Tutorial

Preparation

copy ftplugin-examples/* to ~/.vim/ftplugin

There are 5 examples include c, cpp, javascript, php, ruby
The abbreviation supported in example are

    c          : for std main #i #ii
    cpp        : for std main
    javascript : for fu
    ruby       : for def
    php        : for

You could test the script with your favourite language.

example for edit C file

<C-CR> = Control+Return
|      = Cursor postion
$ gvim foo.c

1

input: #i<C-CR>foo<C-CR>#ii<C-CR>bar<C-CR>
output:
#include <foo>
#include "bar"
|

2

input: std<C-CR>
output:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

|

3

input: main<C-CR>
output:
int main(int argc, char *argv[])
{
    |
    return 0;
}

4

input: for<C-CR>
output:
for (|; /*TODO*/; /*TODO*/) {
  /*TODO*/
}
/*TODO*/

5

input: for<C-CR>int i=0<C-CR>
output:
for (int i=0; |; /*TODO*/) {
  /*TODO*/
}
/*TODO*/

Write Your Own Abbreviation

Use Command Abbr
Usage: Abbr [name] [code]
Example:
Abbr for for (/*TODO*/; /*TODO*/; /*TODO*/) {<CR>/*TODO*/<CR>}<CR>/*TODO*/
Abbr #ii #include "/*TODO*/"<CR>

Abbreviate format is same as vim abbr. see help :abbr
but AdvAbbr has looser limition on name, it support any character as name except space.
eg: f.f is invalid name in vim abbr but valid in Advancer Abbreviation.

TODO has four different formats
/*TODO*/, #TODO#, 'TODO', <!--TODO-->
choose them according to which language is using. 
for example, /*TODO*/ use for c, cpp, 'TODO' use for ruby

Map the expand and jump key

If <C-CR> or <S-CR> is used in other way, set
g:AbbrShortcutSmartExpand
g:AbbrShortcutForceExpand
g:AbbrShortcutEscape
in .vimrc
eg:
let g:AbbrShortcutSmartExpand = '<M-m>'
let g:AbbrShortcutSmartExpand = ['<M-m>', '<S-CR>']
bind SmartExpand to M-m. value type should be string or list.

TroubleShooting

The plugin cannot work.

1. Check if the key mapping is correct
use :imap <S-CR> to check
The correct output should be
i   <S-CR>    *@<ESC>a<C-R>=AbbrSmartExpand()<CR>
if incorrect, invoke command :AbbrInitMapKeys<CR> to remap the keys.

2. Check if terminal support the key
Some terminal doesn't support <C-R>, Try remap the key to other.

Others

Use pathogen manage the abbreviation script easily
see http://www.vim.org/scripts/script.php?script_id=2332
Jump to Line
Something went wrong with that request. Please try again.