[英]using wildcards in vimrc abbreviations
I'm setting up my .vimrc
for writing some HTML and currently have these lines:我正在设置我的.vimrc
来编写一些 HTML,目前有以下几行:
abb <buffer> <h1> <h1></h1><ESC>?<<CR>i<C-R>=Eatchar('\m\s\<bar>/')<CR>
abb <buffer> <h2> <h2></h2><ESC>?<<CR>i<C-R>=Eatchar('\m\s\<bar>/')<CR>
abb <buffer> <h3> <h3></h3><ESC>?<<CR>i<C-R>=Eatchar('\m\s\<bar>/')<CR>
abb <buffer> <h4> <h4></h4><ESC>?<<CR>i<C-R>=Eatchar('\m\s\<bar>/')<CR>
abb <buffer> <h5> <h5></h5><ESC>?<<CR>i<C-R>=Eatchar('\m\s\<bar>/')<CR>
abb <buffer> <h6> <h6></h6><ESC>?<<CR>i<C-R>=Eatchar('\m\s\<bar>/')<CR>
Which automatically adds a closing tag to any opening h-tag I write, moves the cursor to the middle of them and gets rid of the whitespace with the function from :helpgrep Eatchar
它会自动为我编写的任何打开的 h 标签添加一个结束标签,将光标移动到它们的中间,并使用来自:helpgrep Eatchar
的函数去除空白
My qestion is, if there is a more elgant way of doing this, by telling vim to take take <h[1-6]> and adding a closing tag with the same number, instead of having to define each case specifically?我的问题是,如果有更优雅的方式来做到这一点,通过告诉 vim 接受 <h[1-6]> 并添加一个具有相同数字的结束标记,而不是必须专门定义每个案例? Or for that matter, is there a way to define this whole process of adding a closing tag and moving the cursor back as a function and then just specifying all the tags I want it to be applied to?或者就此而言,有没有办法定义添加结束标记并将光标移回作为函数的整个过程,然后仅指定我希望将其应用于的所有标记? Because below this I have a quite a few lines for a bunch of other tags that look almost identical.因为在此之下,我有相当多的行用于一堆看起来几乎相同的其他标签。
Neither :help abbreviations
nor :help mapping
can use a wildcard on the left hand side. :help abbreviations
和:help mapping
都不能在左侧使用通配符。 One solution to that kind of problem is to loop over heading levels and create your abbreviations programatically:此类问题的一种解决方案是遍历标题级别并以编程方式创建缩写:
for level in range(1, 6)
execute "abbreviate <buffer> <h" .. level .. "> <h" .. level .. "></h" .. level .. "><ESC>?<<CR>i<C-R>=Eatchar('\\m\\s\\<bar>/')<CR>"
endfor
It certainly is a tad more clever but I wonder if the loss of legibility is really worth it.它当然更聪明一点,但我想知道失去易读性是否真的值得。
I've written a function to satisfy my similar requirement recently.我最近写了一个函数来满足我的类似需求。 This feature can be found in other tools like VSCode.此功能可以在其他工具中找到,例如 VSCode。
When I type >
, it'll try to look back to check if >
is in a tag like <div>
.当我输入>
,它会尝试回过头来检查>
是否在像<div>
这样的标签中。 If so, it will complete it and set cursor at <div>|</div>
.如果是这样,它将完成它并将光标设置在<div>|</div>
。
Compared with emmet or snippets, this way feels more straightforward.与emmet或snippets相比,这种方式感觉更直接。
function! CompleteTag()
let line = getline('.')
let end = col('.') - 1
let begin = end - 1
let tagname_regexp = '[a-zA-Z0-9-]'
while begin > 0
if line[begin] !~ tagname_regexp
break
endif
let begin -= 1
endwhile
if line[begin] == '<'
let tagname = line[begin+1:end-1]
let move = MoveBack(len(tagname)+3)
return '></'.tagname.'>'.move
endif
return '>'
endfunction
function! MoveBack(n)
let move = ''
for i in range(0, a:n-1)
let move .= "\<Left>"
endfor
return move
endfunction
autocmd FileType jsx,html,vue,svelte inoremap<buffer> > <c-r>=CompleteTag()<cr>
autocmd FileType jsx,html,vue,svelte inoremap<buffer> <c-cr> <cr><esc>O
I'm not sure how to parameterize a single abbreviation;我不确定如何参数化单个缩写; I suspect you'll need to delve into functions for that.我怀疑您需要为此深入研究函数。 You can, however, define a map consisting of the trailing part:但是,您可以定义由尾随部分组成的映射:
inoremap nowsp <ESC>?<<CR>i<C-R>=Eatchar('\m\s\<bar>/')<CR>
then use that map in your abbreviations然后在您的缩写中使用该地图
abb <buffer> <h1> <h1></h1>nowsp
abb <buffer> <h2> <h2></h2>nowsp
" etc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.