简体   繁体   中英

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?

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM