Suppose I want to write a report on what I see from my window : the bus departs, Mrs Smith goes to the grocer's, etc… An example : http://withhiddennoise.net/2010/08/12/georges-perec-an-attempt-at-exhausting-a-place-in-paris/
Of course, abbreviations are ideal for that type of shorthand. We could have :
:iabbr bd The bus departs
:iabbr sg Mrs Smith goes to the grocer's
But it is evident that these events will keep on repeating, and we are supposed not to re-use the same words (at least, not too often).
Is it possible then to have something which would look like this :
:iabbr bd RANDOM(The bus departs, The bus drives away, The bus ta开发者_开发技巧kes off)
Thanks in advance
You could define a function like the following one
function! BD()
let l:t = localtime()
if l:t % 3 == 0
normal aThe bus departs
elseif l:t % 3 == 1
normal aThe bus drives away
else
normal aThe bus takes off
endif
endfunction
and then declare the following imap
:
:imap bd <ESC>:call BD()<CR>a
From now on, entering bd
in insert mode should insert one of the three random strings.
Edit: As for the question if there can be multiple patterns: if I understood the question right, this is possible with something like:
let g:abbreviations = [
\ ['The bus departs', 'The bus drives away', 'The bus takes off'],
\ ['foo' , 'bar' , 'baz' ],
\ ['That''s ok' , 'That''s fine' , 'That''s good' ],
\ ['more' , 'less' , 'the same' ],
\ ]
function! ExpandAbbr(abbr_no)
let l:t = localtime()
return g:abbreviations[a:abbr_no][l:t % 3]
endfunction
iabbr <expr> bd ExpandAbbr(0)
iabbr <expr> pg ExpandAbbr(1)
iabbr <expr> ok ExpandAbbr(2)
iabbr <expr> mo ExpandAbbr(3)
While it is possible to map the abbreviation to the result of the function execution, the problem here is that vim does not have random function, out of the box.
Simple example:
let l_position = 0
function! GetSome(the_list)
if g:l_position >= len(a:the_list)
let g:l_position = 0
endif
let result = a:the_list[g:l_position]
let g:l_position += 1
return result
endfunction
iabbr <expr> tt GetSome(["asdf", "afawe", "wewefsdf", "wefsdf", "jkhkljo"])
Now, every time you type "tt", it will substitute it for the result of GetSome() function, which cycles through the supplied list.
If you need the result to be random, you have to use some tricks.
As far as I know you can't do that with iabbr
, but you can do that with some templating language/extension (e.g. snipMate, here is a tutorial http://www.catonmat.net/blog/vim-plugins-snipmate-vim/ ). Or with setting up a function in vimscript (or any of the supported languages).
精彩评论