Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I wanted a simple abbreviation to work as a quick page divider like

typing:

'>>>'

becomes:

'==============='

2 other requirements: 1) I want to be able to perform serial abbreviations (more than one in a row) and 2) to be able to type text following the divider, then be able to add another divider. So a final divider may look like this:

==============================TEXT HEADER==============================

I tried the following line:

iabbr <silent> >>> ===============<c-r>=Eatchar('\m\s\<bar>/')<cr>

...but it can't perform the above requirements due to subtleties involving the class of preceding characters (see :h full-id, etc). I ended up with the following then:

inoremap >>> ===============<c-r>=Eatchar('\m\s\<bar>/')<cr>

It works, but my feeling was a remap might be overkill when all I really want is a plain string substitution. Is there a better way to do this?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

The easiest way to deal with this is probably to call a user defined function. I came up with the following proof of concept. A couple of drawbacks though:

If you use abbreviations instead of mappings, the character entered after the abbreviation that triggers it will be included as part of the text captured from the user by the input() call, so I used a mapping.

The problem with a mapping is that when you type a > it won't show up in the file until you type something that tells Vim you're not trying to trigger the mapping (or you wait for long enough for the mapping detection to time out).

imap <expr> >>> MyHeader('=')

function! MyHeader(fillchar)
    let l:text = input("Header Text: ")
    let l:len = ((&tw ? &tw : 79) - len(text) - 2) / 2
    let l:fill = repeat(a:fillchar, l:len)
    return l:fill . ' ' . l:text . ' ' . l:fill
endfunction

Since this is just a proof of concept I didn't try to perfect it. For example, the width of the header can be "off by one" if the number of characters in the header text or the textwidth is an odd number. I'll leave it up to you to fix that and add any other refinements you'd like.

share|improve this answer
    
thanks. will try this out and refine, soon as I learn more about vim scripting. –  traycerb Apr 4 at 23:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.