I think that some plugins might do what you want: they create text objects based on the indentation and then let you navigate between these text objects.
But installing a plugin just for that is not always the best solution: you could do it by youself with a bit of vimscript to add to your vimrc
:
First let's create a function which loops on the lines of the buffer and goes to the first line with the same indentation level:
function! GoToNextIndent(inc)
" Get the cursor current position
let currentPos = getpos('.')
let currentLine = currentPos[1]
let matchIndent = 0
" Look for a line with the same indent level whithout going out of the buffer
while !matchIndent && currentLine != line('$') + 1 && currentLine != -1
let currentLine += a:inc
let matchIndent = indent(currentLine) == indent('.')
endwhile
" If a line is found go to this line
if (matchIndent)
let currentPos[1] = currentLine
call setpos('.', currentPos)
endif
endfunction
Note that the argument inc
should be 1
or -1
depending on the way (down or up) you want to scan the buffer.
Then you can simply call the function :call GoToNextIndent(1)
or create two convenient mappings:
nnoremap ni :call GoToNextIndent(1)<CR>
nnoremap pi :call GoToNextIndent(-1)<CR>
Here ni
in normal mode will go to the next line with the same indent level and pi
to the previous line.
You might want to change the mapped keys to something which doesn't mess with your configuration