From time to time I have to work with code that isn't indented to the level of indentation I have Vim set up to (4 spaces per level), usually after copy/pasting something in the file. I usually make do with <<
and >>
. The problem is they don't jump to the next indentation level, they just add or subtract 4 spaces.
If I have code like this
if condition:
do this
do that
doing >>
on do that
will result in
if condition:
do this
do that
I want it to go to this
if condition:
do this
do that
Beside easily matching the indentation of the line above, I want it to jump to the next level of indentation, not add 4 spaces.
This is what I have in my .vimrc regarding indentation
:set tabstop=4 shiftwidth=4 expandtab
filetype plugin indent on
in your.vimrc
, filetype (and plugin) indentation-related rules will apply. I.e. check what yourtabstop
value actually is (set tabstop?
will do; same for the other settings), when editing, and change those values by adding your line to an 'after' file, something like: stackoverflow.com/a/159066/5000478>>
and<<
respectively add and remove indent which is not what you want. What you want is "formatting", which is done with==
.:nnoremap >> ^i<tab><esc>
if you really want tab behaviour on>>
==
will jump to the level of the line above. While a good thing to know, it would not work in all cases I'm interested in. As an addition to the question, what I want is for>>
and<<
to indent/dedent up to the next multiple of shiftwidth from the border in that direction.==
uses eitherequalexpr
orequalprg
to reformat the given lines.