How to return to the previous indentation level in insert mode?

Sometimes vim smartindent does not select the correct indentation level for the next line as soon as you press enter and you would like to return to the indentation level of the previous line and just jump from there. I know that you can press ctrl-d several times to achieve this, but it would be more useful for a key that immediately jumps to the line indent level above.

+7
source share
4 answers

If you can’t work hard to install the plugin for such an easy task (I couldn’t), try this simple mapping:

 :inoremap <CD> <Esc>:call setline(".",substitute(getline(line(".")),'^\s*',matchstr(getline(line(".")-1),'^\s*'),''))<CR>I 

Now, Ctrl D in insert mode performs the action: indent the current line, like the previous line.

This works best before you start typing on a new line, because it will only reset the cursor after the indent.

+1
source

I don’t know if this was a pure coincidence, but Aleksey Radev just published a prev_indent plugin that provides an insert mode display and :PrevIndent to move the current line to the previous indent level.

+3
source

In normal mode, you can use < and > to increase or decrease the indent. They work as expected with movements, visual selection, and >> << for the current line. You can also use = to select the "right" indentation level, again it works as expected with respect to movements, etc.

So what I usually do is correct indentation errors in normal mode after I finish editing, by combining block selection and using = , and then committing individual lines with << and >> .

+1
source

You can use <CO>=G to indent from the cursor to the end of the file in insert mode. Or <CO><< to remove one level of indentation.

0
source

All Articles