如何设置打开文件时默认展开?
在我的 .vimrc
中,我放置了 set Foldmethod=syntax
来启用方法折叠等。但是,我不喜欢每次打开文件时的默认设置整个东西都折叠起来了。有没有办法启用 foldmethod
,但打开文件时文件会展开?
In my .vimrc
I've put set foldmethod=syntax
to enable folding of methods etc. However, I don't like the default that everytime I open a file, the whole thing is folded. Is there a way to enable foldmethod
, yet have files unfolded when I open them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
无论使用何种折叠方法,都应打开所有折叠。
foldlevel=0
全部折叠,foldlevel=1
仅部分折叠,...数字越大,折叠越少。should open all folds, regardless of method used for folding. With
foldlevel=0
all folded,foldlevel=1
only somes, ... higher numbers will close fewer folds.您可以将其放入
.vimrc
中:au BufRead * normal zR
它声明一个自动命令 (
au
),当读取缓冲区 (BufRead
) 时触发,匹配所有文件 (< code>*)并在正常模式下执行zR
(打开所有折叠)命令。You can put this in your
.vimrc
:au BufRead * normal zR
It declares an automatic command (
au
), triggered when a buffer is read (BufRead
), matching all files (*
) and executes thezR
(opens all folds) command in normal mode.将其添加到
.vimrc
中将在您打开文件时暂时禁用折叠,但仍然可以使用zc
恢复折叠Adding this to your
.vimrc
will temporarily disable folding when you open the file, but folds can still be restored withzc
在
.vimrc
中为BufWinEnter
添加一个 autocmd 以自动打开所有折叠,如下所示:告诉 vim 在打开后执行
silent :%foldopen!
BunWinEnter
事件(参见:h BufWinEnter
)。silent %foldopen!
将执行foldopen
由于%
在整个缓冲区上,并且由于!
将递归地打开所有折叠。任何最终的错误消息都将被silent
抑制。 (如果缓冲区实际上尚未包含任何折叠,您可能会收到类似E490: No Fold found
的错误消息)注意:您可以使用
BufRead
而不是BufWinEnter
但如果文件具有启用折叠的模型行,则会覆盖此 autocmd。我的意思是BufRead
autocmd 在处理模型行之前运行,而BufWinEnter
将在处理之后运行它们。我发现后者更有用In
.vimrc
add an autocmd forBufWinEnter
to open all folds automatically like this:That tell vim to execute the
silent :%foldopen!
after openingBunWinEnter
event (see:h BufWinEnter
). Thesilent %foldopen!
will executefoldopen
on the whole buffer thanks to the%
and will open all folds recursively because of the!
. Any eventual error message will be suppressed bysilent
. (You could get error messages likeE490: No fold found
if the buffer actually didn't contain any fold yet)Note: You could use
BufRead
instead ofBufWinEnter
but then if the file has a modeline that enables the folding that will override this autocmd. I meanBufRead
autocmds run before the modeline is processed andBufWinEnter
will run them after. I find the later to be more useful您可以添加
到 .vimrc 文件,它将开始编辑所有打开的折叠的任何新文件。
You can add
to your .vimrc file, and it will start editing any new file with all folds open.
如果您想要一种方法让它在打开后立即展开,您可以使用
set Foldlevelstart=99
正如许多答案所解释的那样。但是,如果您只想查看它们展开的情况,只需按
zi
即可展开所有内容。另外,zi
会将它们关闭。If you want a way to have it display unfolded as soon as it is opened, you can use
set foldlevelstart=99
as a lot of answers explained.But, if you just want to see them unfolded, you can just press
zi
and it will unfold everything. Another,zi
will close them back.您可以将其映射到按键以启用它。
例如,
然后在正常模式下按“,f”组合键
You could map it to keys to enable it.
For example,
Then while in normal mode hit the ",f" key combination
当您将
set nofoldenable
放入 .vimrc 文件中时,您可以打开展开的文件。You can open unfolded file when you put
set nofoldenable
into your .vimrc file.autocmd BufReadPost * 无声! :%foldopen!
这对我来说效果最好。缓冲区打开后,所有折叠都会打开。这将使他们达到正确的水平。
set Foldenable
方法不好,因为如果我选择关闭一个折叠级别,它会再次启用折叠,并将所有内容折叠到 0 级别,而不是仅在我激活的级别上下降一级。autocmd BufReadPost * silent! :%foldopen!
This worked best for me. After a buffer gets opened all folds are opened. This opens them to the correct level.
The
set foldenable
method was not good, because if I choose to close one fold level, it enabled folding again, and folded every thing to 0 level, instead of just going down one level on the one I activated.