Mark block based on indentation level in Vim

梦想的初衷 提交于 2020-01-01 09:03:18

问题


Is it possible to mark a block in Vim based on the indentation already in place? Similarly to v{ .

It would be extremely useful for programming languages with whitespace-sensitive syntax (like Haskell and Python).

For example mark everything between the first let and return in this function:

checkArg (com:arg) s d ns 
  | com == "add-source " = do
      let s' = v ++ s
      lift $ saveLinks s'
      return (s', d)
  | com == "remove-source" = do
      let s' = filter (not . hasWord str) s
      lift $ saveLinks s'
      return (s', d)

http://en.wikipedia.org/wiki/Off-side_rule


回答1:


I use the indent object plugin:

This plugin defines a new text object, based on indentation levels. This is very useful in languages such as Python, in which the syntax defines scope in terms of indentation. Using the objects defined in this plugin, an entire if structure can be quickly selected, for example.

With this, you can select, delete, change, etc. blocks using the standard Vim text object commands, using "i" and "a" to refer to the block that you are in: "vii", "dii", etc. It it language-agnostic, though is especially useful/relevant in whitespace-structured languages such as Python.




回答2:


The plugin Jeet linked to looks neat, but here's a simple alternative.

If you've set foldmethod=indent...

you can use a visual block selection.

So starting on line 3, just type V]z.

:help fold-commands

MOVING OVER FOLDS

[z

Move to the start of the current open fold. If already at the start, move to the start of the fold that contains it. If there is no containing fold, the command fails. When a count is used, repeats the command [count] times.

]z

Move to the end of the current open fold. If already at the end, move to the end of the fold that contains it. If there is no containing fold, the command fails. When a count is used, repeats the command [count] times.

zj

Move downwards to the start of the next fold. A closed fold is counted as one fold. When a count is used, repeats the command [count] times. This command can be used after an operator.

zk

Move upwards to the end of the previous fold. A closed fold is counted as one fold. When a count is used, repeats the command [count] times. This command can be used after an operator.



来源:https://stackoverflow.com/questions/7377771/mark-block-based-on-indentation-level-in-vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!