Vim:在可视块模式下快速选择矩形文本块

use*_*293 7 vim

我正在寻找一种快速方式来选择可视块模式下的文本块.我处理这种性质的文件:

aaaa bbbb cccc
aaaa bbbb cccc
aaaa bbbb cccc

dddd Xeee ffff
dddd eeee ffff
dddd eeee ffff

gggg hhhh iiii
gggg hhhh iiii
gggg hhhh iiii
Run Code Online (Sandbox Code Playgroud)

我的目标是在可视块模式下选择中间块.我会做:

  1. 导航到角落(X所在的位置)
  2. 按Ctrl-V
  3. 'e'将选择范围扩展到块的末尾
  4. 'jj' or '2j' to extend the selection downward to the bottom of the block.

I'm looking for an alternative to (4) that, similar to 'e', would move to the last row of the block. In this simple example 'jj' is not too inconvenient, but sometimes these are large blocks.

There's a similar question here , but that involves jumping a pre-determined number of lines. Is there a way to do this, again an analog to 'e', but moving row-wise instead of column-wise? Thanks!

Ing*_*kat 5

从开始X,你可以这样做<C-v>}kee:

  1. <C-v> - 启动逐块可视模式
  2. } - 转到段落的末尾(该动议据称提供了这个相当复杂的组合的好处)
  3. k - 上面一个排除空行
  4. ee - 将光标从第一列移动到内部块的末尾.


glt*_*lts 5

我尝试制作一个“选择光标周围的视觉块”的功能很有趣。

function! ContiguousVBlock()
  let [lnum, vcol] = [line('.'), virtcol('.')]
  let [top, bottom] = [lnum, lnum]
  while matchstr(getline(top-1), '\%'.vcol.'v.') =~# '\S'
    let top -= 1
  endwhile
  while matchstr(getline(bottom+1), '\%'.vcol.'v.') =~# '\S'
    let bottom += 1
  endwhile

  let lines = getline(top, bottom)
  let [left, right] = [vcol, vcol]
  while len(filter(map(copy(lines), 'matchstr(v:val,"\\%".(left-1)."v.")'),'v:val=~#"\\S"')) == len(lines)
    let left -= 1
  endwhile
  while len(filter(map(copy(lines), 'matchstr(v:val,"\\%".(right+1)."v.")'),'v:val=~#"\\S"')) == len(lines)
    let right += 1
  endwhile

  call setpos('.', [0, top, strlen(matchstr(lines[0], '^.*\%'.left.'v.')), 0])
  execute "normal! \<C-V>"
  call setpos('.', [0, bottom, strlen(matchstr(lines[-1], '^.*\%'.right.'v.')), 0])
endfunction
nnoremap <Leader>vb :<C-U>call ContiguousVBlock()<CR>
Run Code Online (Sandbox Code Playgroud)

您可以尝试使用<Leader>vb:它应该选择光标周围的任何连续的非空白矩形块。优选垂直轴。

也许我稍后会改进它,但现在如果你愿意的话,你可以尝试一下它是否能解决你的问题。

作为我自己尝试的替代方案,您可以尝试流行的插件textobj-word-column。它为您提供文本对象ac ic aC iC来选择一列单词或单词。