任何支架类型的块运动?


8

如果我想删除一个块,则可以使用文本对象运动。

即如果我的文字看起来像这样:

(let [a 1 b {:x 3  :y 4}]
      a)

例如,光标位于character上3

如果键入diB然后:x 3 :y 4将被删除。如果它是daB那么这两个块及周边括号被删除:{:x 3 :y 4}

因此,模式是:

operation inclusion block-motion

可能在哪里进行操作:

  • d -删除

  • c -变化

  • y -复制...

包含是:

  • i -内部(无括号)或

  • a -全部

和块运动:

  • b()()括号

  • B{}用于{}花括号

  • []和/ <>用于各自的括号等。

现在的问题是:对于最里面的带有这些类型的括号的块,是否存在块运动?

我希望能够做到da??被我找的议案。如果上面的示例中的光标在我{}说的范围内,3我将只删除the,{}但是如果我的光标在b我将删除该[]块中,等等。

Answers:


3

这就是为什么问题应该在SO上的原因:需要非琐碎的脚本编写...

" 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中的函数-顺便说一句,lh#position#char_at_pos()in conf 版本中有一个小错误:col()不能使用。


我收到您的代码的语法错误:line 13: E15: Invalid expression: E15: Invalid expression: {(...)。我有vim 7.2,您的代码需要7.3吗?顺便说一下,虽然通常将编程问题重定向到SO,但是脚本答案(针对Shell,编辑器和其他可编写脚本的程序)在这里很常见。
吉尔斯(Gilles)“所以,别再作恶了”

您是否支持字典?如果是的话,如果set cpo&vim在脚本的开头添加脚本,将会发生什么情况?该脚本已经在Windows XP的vim 7.2.148上进行了测试和开发。(我有点厌倦了这种散射vim的社区的,看到这个答案的评论stackoverflow.com/questions/4488979/...
吕克Hermitte

谢谢!如何调用它?我尝试b对所有块使用,但它仍然仅选择()块类型。我是否必须使用不同的名称,还是安装错误?
戈兰·乔维奇

ib已经意味着什么。您可以使用此一个用i%a%(查看映射)=> ,,di%va%i%c2a%
吕克Hermitte

1

并非默认情况下,但是可能存在一些机制来添加该功能。在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`|

0

有一个名为textobj-user的vim插件,它支持.. um,类似这样。实际上,我不确定我是否了解您要查找的内容,但是我认为addon的目的是使编写addon来实现所需的功能更加方便。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.