sponsor Vim development Vim logo Vim Book Ad

vim-indent-object : Text Objects based on Indentation Level

 script karma  Rating 206/81, Downloaded by 2866  Comments, bugs, improvements  Vim wiki

created by
Michael Smith
 
script type
utility
 
description
Vim text objects provide a convenient way to select and operate on various
types of objects. These objects include regions surrounded by various types of
brackets and various parts of language (ie sentences, paragraphs, etc).

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.

Project Repository:
http://github.com/michaeljsmith/vim-indent-object

Feedback/criticism is welcome:
msmith@msmith.id.au
http://msmith.id.au

This plugin defines two new text objects. These are very similar - they differ
only in whether they include the line below the block or not.

        Key Mapping       Description

        <count>ai         (A)n (I)ndentation level and line above.
        <count>ii         (I)nner (I)ndentation level (no line above).
        <count>aI         (A)n (I)ndentation level and lines above/below.
        <count>iI         (I)nner (I)ndentation level (no lines above/below).


Note that the iI mapping is mostly included simply for completeness, it is
effectively a synonym for ii.

Just like regular text objects, these mappings can be used either with
operators expecting a motion, such as 'd' or 'c', as well as in visual mode.
In visual mode the mapping can be repeated, which has the effect of
iteratively increasing the scope of indentation block selected. Specifying a
count can be used to achieve the same effect.

The difference between |ai| and |aI| is that |ai| includes the line
immediately above the indentation block, whereas aI includes not only that,
but also the line below. Which of these is most useful largely depends on the
structure of the language being edited.

For example, when editing the Python code, |ai| is generally more useful, as
the line above the indentation block is usually related to it. For example, in
the following code (* is used to indicate the cursor position):

      if foo > 3:
         log("foo is big") *
         foo = 3
      do_something_else()

the if clause is logically related to the code block, whereas the function
call below is not. It is unlikely we would want to select the line below when
we are interested in the if block.

However, in other languages, such as Vim scripts, control structures are
usually terminated with something like 'endif'. Therefore, in this example:

      if foo > 3
         echo "foo is big" *
         let foo = 3
      endif
      call do_something_else()

we would more likely want to include the endif when we select the if
structure.

When scanning code blocks, the plugin usually ignores blank lines. There is an
exception to this, however, when the block being selected is not indented. In
this case if blank lines are ignored, then the entire file would be selected.
Instead when code at the top level is being indented blank lines are
considered to delimit the block.
 
install details
Unpack the zip archive onto your vim runtime directory.

Alternatively unpack it to a location of your choice, and update your runtimepath to point to it.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
vim-indent-object.zip 1.1.2 2010-04-06 7.0 Michael Smith Now uses linewise visual mode by default.
vim-indent-object.zip 1.1.1 2010-04-02 7.0 Michael Smith Fixed when using 'ignorecase'.
vim-indent-object.zip 1.1 2010-04-02 7.0 Michael Smith Made behaviour more consistent and intuitive.
vim-indent-object.zip 1.0 2010-04-01 7.0 Michael Smith Initial upload
ip used for rating: 3.145.47.253

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github