![](/img/trans.png)
[英]Do while loops execute all lines of code if their conditional is no longer met in the middle of a block?
[英]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>
这不会提供您想要的确切信息,但是会在屏幕上显示该功能的文本(假设时间不太长)。
vim中的“段落”是一组连续的非空白行。 这是一段代码的近似值。 还要注意,您可以对mark命令使用任何字母,因此一次最多可以激活52个字母。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.