If you use bracketing macros (I do), you can do the following:

imap <buffer> <leader>switch switch ( <>
)<cr>{<cr><><cr>}<><esc>^%k<del>
imap <buffer> <leader>case case <>:<cr><>;<cr>break;<><esc>k$"_xk^<del>
imap <buffer> <leader>default default:<cr><><esc>0<del>

This allows me to hit ,switch (my leader is the comma) to get:

    switch ( | )
    {
        <>
    }<>

And the cursor ends up where the | is.  Type the switch variable and hit
the hot key to move to the next placeholder.

Then, I hit ,case to get:

    switch ( something )
    {
        case |:
            <>
            break;<>
    }<>

Again, the cursor is on the |.  Type the case value, hit the hot key,
type the case statements, hit the hot key to go to the end of the break.
If you're done, hit the hot key to get out; if not, press enter and
either do another ,case or ,default.  ,default gets you:

    switch ( something )
    {
        case 'a':
            somethingElse();
            break;
        default:
            |
    }<>

(You know where the cursor is by now, I'm sure.)

I don't use this very often, but I have it around.  I have similar ones
for if, while, else, else if, try and catch.

Note that if you don't like spaces around parentheses or after keywords
(like switch) or prefer your opening brace on the same line, etc.,
change the mapping slightly.  It should work with your indentation style
as is, however.

Hope this helps.

Enjoy!

Salman.

> -----Original Message-----
> From: Tom Purl [mailto:[EMAIL PROTECTED] 
> Sent: Friday, June 23, 2006 10:17 AM
> To: Max Dyckhoff
> Cc: vim@vim.org
> Subject: Re: Script to create automatic case statements
> 
> > The number of times that I write something like this in a day is
> > ridiculous:
> >
> > switch (some_variable)
> > {
> >     case _some_root_wibble:
> >     case _some_root_wobble:
> >     case _some_root_jelly:
> >     case _some_root_on_a_stick:
> > }
> >
> ...
> >
> > Has anyone seen something to do this before, or does anyone 
> have any 
> > ideas about how to implement this?
> 
> The "Perl Support" vim plugin has some shortcuts for creating "if"
> statements and such.  You may want to check out how the 
> author of that script did it.
> 
> Hope that helps!
> 
> Tom Purl
> 
> 

Reply via email to