I'm already aware of existing solution to automatically "surround" sentences/word/etc (eg: auto-surround, vim-surround, etc) but those (as far as i know) don't support using normal strings instead of characters (eg: {}, ()...).
I did found this snippet which seems to do what i want:
function! ConditionalPairMap(open, close)
let line = getline('.')
let col = col('.')
if col < col('$') || stridx(line, a:close, col + 1) != -1
return a:open
else
return a:open . a:close . repeat("\<left>", len(a:close))
endif
endf
inoremap <expr> ( ConditionalPairMap('(', ')')
inoremap <expr> { ConditionalPairMap('{', '}')
inoremap <expr> [ ConditionalPairMap('[', ']')
Only problem is that I'm not sure how to make this work with normal strings. Here what i tried using the above function:
inoremap <expr> ( ConditionalPairMap('te', '.te')
I mainly want to use those as custom "marker" or "header" for certain block of text. The above closing and opening marker are just example.
Additionally, I'm aware that by using normal or any string as either opening/closing surrounding will be confusing when writing normal text, that's why I'd prefer if it would only trigger the closing marker when there a newline in front of it, like so:
te # no newline yet
If there a newline after it, place the closing after it:
te #a newline placed here
.te
PS: This is mainly to be used in Insert mode
.
Any way to do this?
inoremap <expr> <CR> getline('.') =~# '^\s*te\>' ? '<CR>.te<C-o>O' : '<CR>'