如果我想删除一个块,我可以使用文本对象动作。
即,如果我的文字如下所示:
(let [a 1 b {:x 3 :y 4}]
a)
例如,光标位于字符上3
。
如果我输入diB
,则将:x 3 :y 4
被删除如果是daB
,则块和周围的括号都将被删除:{:x 3 :y 4}
所以,模式是:
operation inclusion block-motion
其中操作可能是:
d
- 删除c
- 改变y
- 复制 ...
包含是:
i
- 内部(无括号)或a
- 全部
和块运动:
b
,(
或者)
对于()
括号B
,{
或}
卷发{}
[
or]
和<
or>
分别代表各自的括号,等等。
现在,问题是:最里面的块是否有带有任何这些类型的括号的块运动?
我希望能够da?
成为?
我正在寻找的动议。如果上面的示例中的光标{}
在3
我会删除的范围内,{}
但如果我的光标在b
我会删除该[]
块,等等。
答案1
这就是为什么问题应该是这样的:需要非平凡的脚本......
" Public Mappings {{{1
onoremap <silent> i% :<c-u>call <sid>SelectFirstPair(1,0)<cr>
xnoremap <silent> i% :<c-u>call <sid>SelectFirstPair(1,1)<cr><esc>gv
onoremap <silent> a% :<c-u>call <sid>SelectFirstPair(0,0)<cr>
xnoremap <silent> a% :<c-u>call <sid>SelectFirstPair(0,1)<cr><esc>gv
" Public Mappings }}}1
"------------------------------------------------------------------------
" Private Functions {{{1
" Note: most functions are best placed into
" autoload/«your-initials»/«omap_any_bracket».vim
" Keep here only the functions are are required when the plugin is loaded,
" like functions that help building a vim-menu for this plugin.
let s:k_pairs = {
\ '(': ')',
\ '[': ']',
\ '{': '}',
\ '<': '>'
\ }
let s:k_begin = '[([{<]'
let s:k_end = '[)\]}>]'
function! s:SelectFirstPair(inner, visual)
" In case we already are in visual mode, we may have to extend the current
" zone if it selects a pair of brackets
if a:visual
let char_b = lh#position#char_at_mark("'<")
if char_b =~ s:k_begin
\ && s:k_pairs[char_b] == lh#position#char_at_mark("'>")
call search('.', 'bW') " previous char
elseif a:inner
" handle case the case "vi%i%i%"
let current_pos = getpos('.')
call setpos('.', getpos("'<"))
call search('.', 'bW') " previous char
let pos_b = getpos('.')
call setpos('.', getpos("'>"))
call search('.', 'W') " next char
let pos_e = getpos('.')
let char_b = lh#position#char_at_pos(pos_b)
let char_e = lh#position#char_at_pos(pos_e)
echomsg "chars = ".char_b.char_e
if char_b =~ s:k_begin
\ && s:k_pairs[char_b] == char_e
call setpos('.', pos_b) " restore start_pos
call search('.', 'bW') " previous char
else
call setpos('.', current_pos) " restore init_pos
endif
endif
endif
" Searching the n outer blocks requested
let cnt = v:count <= 0 ? 1 : v:count
while cnt > 0
let cnt -= 1
let char_c = lh#position#char_at_pos(getpos('.'))
let accept_at_current = char_c =~ s:k_begin ? 'c' : ''
" Begin of the current outer block
if 0 ==searchpair(s:k_begin, '', s:k_end, 'bW'.accept_at_current, 'lh#syntax#skip()')
throw "No outer bloc"
endif
if cnt > 0
call search('.', 'bW') " previous char
endif
endwhile
let char_b = lh#position#char_at_pos(getpos('.'))
normal! v
" End of the outer block
let pos_e = searchpair(s:k_begin, '', s:k_end, 'W', 'lh#syntax#skip()')
let char_e = lh#position#char_at_pos(getpos('.'))
if pos_e == 0
throw "pos_e == 0"
elseif s:k_pairs[char_b] != char_e
echomsg "unbalanced blocks"
endif
" Adjusting the extremities
if a:inner
call search('.', 'b')
normal! o
call search('.')
normal! o
endif
endfunction
" Private Functions }}}1
注意:我已经重用了来自lh-vim-lib-- 顺便说一句,conf 版本中有一个小错误lh#position#char_at_pos()
:col()
不能使用。
答案2
默认情况下不是这样,但可能有某种机制可以添加该功能。在visual.txt中,关于对可视区域进行操作的部分,有这样的内容:
The objects that can be used are:
aw a word (with white space) |v_aw|
iw inner word |v_iw|
aW a WORD (with white space) |v_aW|
iW inner WORD |v_iW|
as a sentence (with white space) |v_as|
is inner sentence |v_is|
ap a paragraph (with white space) |v_ap|
ip inner paragraph |v_ip|
ab a () block (with parenthesis) |v_ab|
ib inner () block |v_ib|
aB a {} block (with braces) |v_aB|
iB inner {} block |v_iB|
at a <tag> </tag> block (with tags) |v_at|
it inner <tag> </tag> block |v_it|
a< a <> block (with <>) |v_a<|
i< inner <> block |v_i<|
a[ a [] block (with []) |v_a[|
i[ inner [] block |v_i[|
a" a double quoted string (with quotes) |v_aquote|
a' a single quoted string (with quotes) |v_a'|
i' inner simple quoted string |v_i'|
a` a string in backticks (with backticks) |v_a`|
i` inner string in backticks |v_i`|
答案3
有一个 vim 插件叫做文本对象用户支持..嗯,类似这样的东西。实际上我不确定我是否理解你在寻找什么,但我认为插件的目的是让编写插件来实现你想要的东西更方便。