简体   繁体   中英

Vim generate text expansion from shortcut

The codebase I'm working on requires standard comments around any modifications that we make:

// -----------ABCD---------------

and then

// ----------\ABCD---------------

What's the best way to map a short combination of keys like -abcd and \\abcd to generate these longer strings?

The simplest, built-in :help abbreviations :

inoreab abcd // -----------ABCD---------------
inoreab abcD // ----------\ABCD---------------

Why did I choose different triggers? There are some rules (documented as "three types of abbreviations" under the above help link) for the allowed keys; the easiest is that it's all keyword characters.

If you insist on those exact triggers, you'd have to fiddle with the 'iskeyword' option (which can affect syntax highlighting and motions!), or switch to :inoremap . The downside of that is that you won't see the typed characters until any ambiguity has been resolved (try it; you'll see what I mean).

ABCD is just a dummy; I need dynamic text here

If multiple abbreviations won't do, you'll need snippets .

snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork . A modern alternative (that requires Python though) is UltiSnips . There are more, see this list on the Vim Tips Wiki and this comparison by Mark Weber .

A snippet would look like this (snipMate syntax):

snippet abcd
    // -----------${1:ABCD}---------------
    ${2:content}
    // ----------\$1---------------
    ${3}

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