我知道你可以用
set foldcolumn=1
Run Code Online (Sandbox Code Playgroud)
启用折叠列
但有没有办法只在文件中存在折叠时自动打开它?
当文件变得足够大时,我的方法比@Zsolt Botykai更快.对于小文件,我认为时差是无关紧要的.该函数不是检查每一行的折叠,而只是尝试在折叠之间移动.如果光标从不移动,则没有折叠.
function HasFolds()
"Attempt to move between folds, checking line numbers to see if it worked.
"If it did, there are folds.
function! HasFoldsInner()
let origline=line('.')
:norm zk
if origline==line('.')
:norm zj
if origline==line('.')
return 0
else
return 1
endif
else
return 1
endif
return 0
endfunction
let l:winview=winsaveview() "save window and cursor position
let foldsexist=HasFoldsInner()
if foldsexist
set foldcolumn=1
else
"Move to the end of the current fold and check again in case the
"cursor was on the sole fold in the file when we checked
if line('.')!=1
:norm [z
:norm k
else
:norm ]z
:norm j
endif
let foldsexist=HasFoldsInner()
if foldsexist
set foldcolumn=1
else
set foldcolumn=0
endif
end
call winrestview(l:winview) "restore window/cursor position
endfunction
au CursorHold,BufWinEnter ?* call HasFolds()
Run Code Online (Sandbox Code Playgroud)
您很可能可以创建一个函数来检查文件是否有任何折叠,例如:
function HasFoldedLine()
let lnum=1
while lnum <= line("$")
if (foldclosed(lnum) > -1)
return 1
endif
let lnum+=1
endwhile
return 0
endfu
Run Code Online (Sandbox Code Playgroud)
现在你可以将它与一些一起使用autocommand,例如:
au CursorHold * if HasFoldedLine() == 1 | set fdc=1 | else |set fdc=0 | endif
Run Code Online (Sandbox Code Playgroud)
华泰
| 归档时间: |
|
| 查看次数: |
2346 次 |
| 最近记录: |