sponsor Vim development Vim logo Vim Book Ad

prev_indent : Utility functions for custom indentation of line under cursor

 script karma  Rating 0/0, Downloaded by 2802  Comments, bugs, improvements  Vim wiki

created by
Alexey Radkov
 
script type
utility
 
description
This script provides 2 utility commands PrevIndent and AlignWith for doing
custom indentation tricks with line under cursor in Insert and Normal modes.
Originally there was only one of them: PrevIndent, and that explains the name
of the script.

Command PrevIndent moves line under cursor to the previous indentation
level. Normally this command is not really needed as soon as indentation
levels in most file types tend to correspond to the value of the
shiftwidth and the result is easily achieved by pressing <C-d> in Insert
mode. But this is not a case for some programming languages (for example
Haskell indentation rules are very specific).

Command PrevIndent simply aligns the beginning of the current line with
the first previous line that starts from a less position.

Recommended mappings are

Insert mode:

    imap <silent> <C-d>       <Plug>PrevIndent

if you want to replace with PrevIndent standard <C-d> Insert mapping or

    imap <silent> <C-g><C-g>  <Plug>PrevIndent

(press <C-g> twice) otherwise.

Normal mode:

    nmap <silent> <C-k>k      :PrevIndent<CR>

Another command provided by the script is AlignWith. It finds a symbol
that was specified by user dynamically (i.e. using getchar()) in the right
hand side of the previous line and aligns beginning of the current line
with the column of the found symbol. If a symbol was not found then it is
searched from beginning of the previous line. Repeating AlignWith will
cycle alignment of the current line to the right through all searched
symbols in the previous line. User can specify an order of symbol to
search. For example issuing command

    :AlignWith 2

and then pressing '(' will skip first found '(' in the previous line and
align current line to the second found parenthesis.

Recommended mappings are

Insert mode:

    imap <silent> <C-g>g      <Plug>AlignWith

Normal mode:

    nmap <silent> <C-k>g      :AlignWith<CR>

In both Insert and Normal modes command AlignWith will wait until user
enters a character to align with. So for example in Insert mode user must
enter <C-g>g and another character after that.

Both commands must behave well for different settings of <Tab> expansions.
 
install details
put the script in plugin directory
 

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
prev_indent.vim 0.3 2014-03-07 7.0 Alexey Radkov minor improvements and fixes
prev_indent.vim 0.2.2 2013-05-13 7.0 Alexey Radkov skip empty lines when indenting
prev_indent.vim 0.2.1 2013-05-11 7.0 Alexey Radkov Fixed command AlignWith in repeated mode
prev_indent.vim 0.2 2013-05-09 7.0 Alexey Radkov Added command AlignWith to align against arbitrary symbol on the previous line
prev_indent.vim 0.1.1 2013-05-07 7.0 Alexey Radkov Normal mode supported.
prev_indent.vim 0.1 2013-05-07 7.0 Alexey Radkov Initial upload
ip used for rating: 142.132.191.50

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