gpt4 book ai didi

c++ - 移动到 block 代码的中间

转载 作者:行者123 更新时间:2023-11-30 01:22:51 27 4
gpt4 key购买 nike

我试图使用 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>

关于c++ - 移动到 block 代码的中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15682365/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com