gpt4 book ai didi

vim - 使用vim代码折叠标记生成索引(目录)

转载 作者:行者123 更新时间:2023-12-04 16:52:42 30 4
gpt4 key购买 nike

在我的 .vimrc 中有这些行

:set foldmethod=marker
:set foldmarker=SECTION:,ENDSECTION:

用于自定义代码折叠。在我的文件中,相关语言的注释字符在代码折叠标记之前,然后是相应部分的标题。例如

# SECTION: First Section
some code
# SECTIION: Subsection
some more code
# ENDSECTION:
# ENDSECTION:

# SECTION: Second Section
some other code
# ENDSECTION:

此结构包含为文件生成内容所需的所有内容

First Section
Subsection
Second Section

(理想情况下,该索引具有类似于 vim 帮助系统的标记,因此我可以轻松跳转到相应的 SECTION;我不知道如何实现这一点)。我可以想到一个生成此文本的简单 perl 脚本,但我更喜欢基于在新窗口中显示索引的 vim 脚本的解决方案。也许已经有一个解决方案可以做到这一点?

最佳答案

将它放入你的 vimrc 并运行 :MkIdx<leader>z .您也可以将范围传递给命令,但默认是整个缓冲区。

function! MkIdx() range
let items = filter(getline(a:firstline, a:lastline), 'v:val =~ ''\C^\s*#\s\+SECTION''')
new
call setline(1, map(items, 'substitute(v:val, ''^\(\s*\)[^:]\+:\(.\+\)'', ''\1\2'', '''')'))
" Mapping to jump to section:
nnore <silent><buffer><leader>x :<C-U>call Go2Section()<CR>
endfunction

function! Go2Section()
let section = matchstr(getline('.'), '^\s*\zs.*')
quit
call search('\C# SECTION:\s\+'.section, 'cw')
endfunction

command! -bar -range=% MkIdx <line1>,<line2>call MkIdx()
" Mapping to build the index:
nnore <silent><leader>z :<C-U>MkIdx<CR>

编辑:将索引放在新缓冲区上。

编辑 2:不要留空行。

编辑 3:允许跳回到带有 <leader>x 的部分.

关于vim - 使用vim代码折叠标记生成索引(目录),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10072357/

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