繁体   English   中英

移至区块码的中间

[英]Move to the middle of a block code

我使用Vim试图将光标移动到代码块的中间,但是我不知道该怎么做:

//cursor is for instance here.

{
    //or here

    //some code
    // .... **** move cursor here ****
    //some more code 

}

最终的想法是拥有一个快捷方式,该快捷方式可以保存当前位置,将光标移动到代码块的中间,将当前行设置到屏幕的中间(使用快捷方式“ zz”),然后再返回到保存的位置。

我更喜欢内置的vim函数,但是插件也可以。

编辑:这是为c + +,所以我想要它为方括号{}。

我试了一下(又快又脏):

function! Middleize()

  " use ]M to jump to either the end of the current method if we are in it
  " or the start of the next method if we are above the method
  normal! ]M

  " we record the current line number
  let first_line = line('.')

  " we go to the other end of the method
  normal! %

  " we record the current line number
  let second_line = line('.')

  " we started either from the top or from the bottom of the method
  " so we have to take that into account when calculating the number
  " of the line we want to jump to
  if first_line < second_line
    let middle_line = first_line + ((second_line - first_line) / 2)
  else
    let middle_line = ((first_line - second_line) / 2) + second_line
  endif

  " let's go!
  execute "normal! " . middle_line . "Gzz"
endfunction

nnoremap <F5> :call Middleize()<CR>

更多通用解决方案,但可能有用- 易移动插件可让您以极高的精度跳到各处。

例如:

初始状态

<Leader><Leader>w (默认)-' 文字运动 '

启用文字运动

g

光标在中间

然后跳回去,您只需向后做同样的事情(在这种情况下, <Leader><Leader>bg

尽管您可以:set scrolloff=9999使屏幕中间跟随光标,但这不会将当前行设置为屏幕中间。

这不会提供您想要的确切信息,但是会在屏幕上显示该功能的文本(假设时间不太长)。

  1. -在当前光标位置设置的标记。
  2. 重复按} (跳到一个段落),直到看到所需的代码。
  3. ` 一个 -返回到您设置的标志。

vim中的“段落”是一组连续的非空白行。 这是一段代码的近似值。 还要注意,您可以对mark命令使用任何字母,因此一次最多可以激活52个字母。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM