在 vim 缩写中使用反斜杠 [英] Using backslashes in vim abbreviations
问题描述
我希望能够编写 \bit
并将其扩展为 vim 中的某些内容.但是,如何在缩写的左侧编码反斜杠?
I want to be able to write \bit
and have it expand to something in vim. How do I encode a backslash in the left-hand side of an abbreviation, though?
我尝试了所有这些:
:iab \bit replacement_text
:iab <Bslash>bit replacement_text
:iab <bs>bit replacement_text
但是得到了 E474: Invalid argument
所有这些.
but got E474: Invalid argument
for all of these.
map_backslash
帮助主题建议
,但这似乎不起作用.
The map_backslash
help-topic suggests <Bslash>
, but this doesn't seem to work.
推荐答案
你可以在bit"上定义你的缩写,然后测试它前面是否有",如果是,则返回新文本,或者位"否则.
You can define your abbreviation on "bit", and then test if it is preceded by "", if so, return the new text, or "bit" otherwise.
function! s:Expr(default, repl) expr
if getline('.')[col('.')-2]=='\'
return "\<bs>".a:repl
else
return a:default
endif
endfunction
:inoreab bit <c-r>=<sid>Expr('bit', 'foobar')<cr>
这就是我在 MapNoContext().
请参阅 :h abbreviations 了解原因你问的不能直接实现.
see :h abbreviations for the reasons why what you asked can't be achieved directly.
它可以很容易地封装成这样:
It can be easily encapsulated this way:
function! s:DefIab(nore, ...) abort
let opt = ''
let i = 0
while i != len(a:000)
let arg = a:000[i]
if arg !~? '<buffer>\|<silent>'
break
endif
let opt .= ' '.arg
let i += 1
endwhile
if i+2 != len(a:000)
throw "Invalid number of arguments"
endif
let lhs = a:000[i]
let rhs = a:000[i+1]
exe 'i'.a:nore.'ab'.opt.' '.lhs.' <c-r>=<sid>Expr('.string(lhs).', '.string(rhs).')<cr>'
endfunction
command! -nargs=+ InoreabBSlash call s:DefIab('nore', <f-args>)
并用一个简单的:
InoreabBSlash <buffer> locbit foobar
或
InoreabBSlash bit foobar
这篇关于在 vim 缩写中使用反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!