`

Folding with Regular Expression (转载)

    博客分类:
  • vim
阅读更多

<!-- start content -->


From: http://vim.wikia.com/wiki/VimTip282

created  2002 · complexity  basic · author  Chris Butler · version  6.0


When searching with / , it would sometimes be nice to fold everything except for matches to your search. The following code does this, providing two levels of folding to allow you to show some context around each search match as well.

Contents

[show ]

<script type="text/javascript">wgAfterContentAndJS.push(function() { if (window.showTocToggle) { window.tocShowText = &quot;show&quot;; window.tocHideText = &quot;hide&quot;; showTocToggle();}});</script>

[edit ] Using a '/' search and key mapping

Add the following to your vimrc to provide a mapping to fold on an already-performed search:

nnoremap \z :setlocal foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)
\\|\\|(getline(v:lnum+1)=~@/)?1:2 foldmethod=expr foldlevel=0 foldcolumn=2<CR>
  • The foldexpr is an extension of foldexpr=(getline(v:lnum)=~@/)?0:1
  • The following options set the foldmethod to use the fold expression, and some other convenient values.

First search for a pattern, then fold everything else with \z
Use zr to display more context, or zm to display less context.

[edit ] Using a user-defined command

If you want to search and fold with a single command, either add the following as well:

command! -nargs=+ Foldsearch exe "normal /".<q-args>."^M\z"

Or get rid of the \z entirely:

command! -nargs=+ Foldsearch exe "normal /".<q-args>."^M" | setlocal foldexpr=
(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)\|\|(getline(v:lnum+1)=~@/)?1:2 foldmethod=
expr foldlevel=0 foldcolumn=2

In these last two code segments, be sure to replace the "^M" with an actual CTRL-M (carriage return) character. Type :Folds[earch] <search string>[ENTER] to use this command. Use zr to display more context, use zm to display less context as before.

[edit ] Customizing

To add a second level of context, you could add this to the end of foldexpr:

(getline(v:lnum-2)=~@/)\|\|(getline(v:lnum+2)=~@/)?2:3

but it will take longer as folded lines (the majority) evaluate the full expression.

If no context is desired at all, you can do the following instead:

foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum)=~@/)\|\|(getline(v:lnum)=~@/)?0:1

[edit ] Uses

Aside from the possible convenience of seeing only the search terms and their immediate context, this method of folding can be used for practical purposes as well. For example, viewing a "quick and dirty" api of a source code file.

To make a command to do a quick Java API for example, use:

" View the methods and variables in a java source file."
command! Japi Foldsearch public\s\|protected\s\|private\s

Note that a better way to do this for languages that support it would be to use syntax highlighting. Check the syntax file for the language in question to determine if this is an option. Many syntax files such as those for C, Perl, and VimL all define rules for at least some syntax-based folding, using :set foldmethod=syntax . Other languages such as Java do not currently have this functionality built in, so keep this idea around just in case you need it!

edit See also

edit Comments

 TO DO 

  • Explain better how the foldexpr string works.
    Convert it into a function?
    An excellent idea, or use line continuations. The current state is very hard to read (especially in IE where the scroll bar obscures part of the text).
  • Check if the comments in the original tip should be added (have they been taken into account?).
    Yes, they have, though the link to the script should probably be moved to a more prominent place.
  • A quick test suggests that "\<CR>" can be used instead of an actual ^M (so we should probably edit the tip in due course, although "\<CR>" possibly relies on some non-compatibility setting).
    I think if people are using a crazy setting that breaks things like "\<CR>" they should figure out how to fix the consequences on their own. No harm in mentioning it though, I guess.
  • Link to Folding for Quickfix .
  • Perhaps add following section (merged in from VimTip1022 ). A very quick test suggests it's ok.
    I like it. Probably add it as part of the explanation, though. Maybe build up to the final expression gradually.

edit Simple folding

After executing the following, you can search for a pattern then press F8 to fold misses.

:set foldexpr=getline(v:lnum)!~@/
:nnoremap <F8> :set foldmethod=expr<CR><Bar>zM

<!-- NewPP limit report Preprocessor node count: 115/1000000 Post-expand include size: 1028/2097152 bytes Template argument size: 108/2097152 bytes Expensive parser function count: 0/100 --><!-- Saved in parser cache with key vim:pcache:idhash:2275-0!1!0!!en!2 and timestamp 20100412041912 -->

分享到:
评论

相关推荐

    java中代码折叠插件com.cb.eclipse.folding,亲测好用

    java中代码折叠插件com.cb.eclipse.folding,com.cb.eclipse.folding_1.0.6.jar,使用方法百度,亲测可用,java中代码折叠插件com.cb.eclipse.folding,com.cb.eclipse.folding_1.0.6.jar,使用方法百度,亲测可用.java中...

    eclipse 4.7 oxy folding 代码折叠 com.cb.eclipse.folding_1.8.1.jar 20180207

    20180207凌晨,针对eclipse 4.7 修复了 代码折叠的某个插件的一些无法打开的bug和图标显示...eclipse-&gt;window-&gt;preferences-&gt;java-&gt;editor-&gt;folding 然后选择此插件 具体百度下,旧版本的使用介绍很多的 modify by y

    CodeFolding

    link 方式安装: Eclipse3.2... 在编辑区右击,选"preferences"(中文可能是"设置"),展开Java→Editor,在Folding项右窗口的下拉框中选"Coffee Bytes Java Folding",保存后重启 Eclipse.

    code folding插件包和使用说明

    code folding插件包 和 配置说明

    Computational Methods for Protein Folding

    Spectroscopic measurement techniques continue to improve in resolution and sensitivity, allowing a wealth of information to be obtained with regard to the kinetics of protein folding and unfolding, ...

    code folding

    code folding

    js折叠效果 folding.js

    javascript折叠效果,引入folding.js和folding.css就可以使用 例子: &lt;div class="folding"&gt; ;"&gt; src="../orgReport/tree.html" frameborder=0 scrolling=NO&gt; ...

    eclipse_folding插件.zip

    com.cb.eclipse.folding_1.0.6.jar包文件,用于eclipse的代码块折叠功能扩充使用。

    北京折叠英文版 Folding_Beijing.txt

    Folding Beijing BY HAO JINGFANG, TRANSLATED BY KEN LIU

    Fast Folding Algorithm for Detection Error Bounds i n Optimum Smoothing

    Fast Folding Algorithm for Detection Error Bounds i n Optimum Smoothing

    Code Folding

    Code Folding 加入多种代码折叠功能(比eclipse自带的更多)

    folding-cell-android.zip

    folding-cell-android,FoldingCell是一种材料设计,旨在通过折叠纸质材料扩展内容单元,博客附件,效果请查看博客相对应项目。

    Vi and Vim: Questions and Answers

    Folding, Split, Save, Buffers, Spell Checking, Multiple Files, Highlight, Vimdiff, Insert Mode, Tabbed User Interface, External Command, Alignment, Vimscript Python, GVim, Regular Expression,...

    folding-plugin让你的layout下的文件分组的插件.rar

    folding-plugin让你的layout下的文件分组的插件.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。

    com.cb.eclipse.folding_1.0.6.jar

    Eclipse折叠代码插件folding, 用法:把下载到的插件com.cb.eclipse.folding_1.0.6.jar放到Eclipse的plugins下面,重启Eclipse

    eclipse 代码折叠插件 _com.cb.eclipse.folding_1.0.6

    一、Eclipse代码折叠插件 关闭Eclipse,把下载到的插件com.cb.eclipse.folding_1.0.6.jar放到...2.设置Folding值,最后一个选项,打钩。 3.User Defined Regions添加开始和结束标签。 三、设置完成后,重启Eclipse

    code_folding

    code folding 前两个在eclipse 3.2下没成功 1.04的在eclipse3.2中可以用, 解压后,直接放到plugins目录下 eclipse 3.3还不清楚 &lt;br&gt;1.05的可能在3.4下可以用吧

    代码折叠插件 eclipse.folding_1.0.6.rar

    eclipse for if 代码折叠插件 com.cb.eclipse.folding_1.0.6.rar 解压后,讲里面的 一个jar文件放到eclipse/plugins 文件夹,重启eclipse ,然后 菜单-...Select folding to use: Coffee Bytes Java Folding。ok!

    com.cb.eclipse.folding代码折叠

    一、Eclipse代码折叠插件 关闭Eclipse,把下载到的插件com.cb.eclipse.folding_1.0.6.jar放到Eclipse...设置Folding值,最后一个选项,打钩。 3.User Defined Regions添加开始和结束标签。 三、设置完成后,重启Eclipse

    Android代码-folding-cell-android

    FoldingCell for Android About This project is maintained by Ramotion, Inc. We specialize in the designing and coding of custom UI for Mobile Apps and ...'com.ramotion.foldingcell:folding-cell:1.1.0

Global site tag (gtag.js) - Google Analytics