davidchambers
davidchambers

Reputation: 24806

Text expansion in Vim

In Vim, I'd like to type…

1x1<TAB>

and have it expand to…

(r 1)(d 1)(l 1)(u 1)

and for…

2x4<TAB>

to expand to…

(r 2)(d 4)(l 2)(u 4)

How would I go about making this so?

Upvotes: 0

Views: 1225

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172540

If you just need a small, fixed set of these, you can simply define :iabb (though they will expand with Space, not Tab). Alternatively, there are plugins like snipMate -- they even do the expansion with Tab by default.

However, if you need expansion of arbitrary numbers, a fixed scheme won't work and you have to implement a custom mapping for <Tab> that extracts the keyword before the cursor and replaces it with the expansion, like this:

:inoremap <Tab> <Tab>$<Esc>:substitute/\<\(\d\+\)x\(\d\+\)\s*\%#/(r \1)(d \2)(l \1)(u \2)/e<CR>0f$s

This temporarily inserts a $ placeholder to restore the original cursor position after the substitution; you could implement something better with getpos() / setpos().

Upvotes: 4

Related Questions