vimtricks.wiki Concise Vim tricks, one at a time.

How do I indent or unindent the current line while staying in Insert mode?

Answer

<C-t> / <C-d>

Explanation

While in Insert mode, you can adjust indentation without switching back to Normal mode. <C-t> (Ctrl+T) increases the indentation of the current line by one shiftwidth, and <C-d> (Ctrl+D) decreases it. This keeps you in Insert mode the entire time, making it ideal when you realize you need to re-indent a line as you're typing.

How it works

  • <C-t> — add one level of indentation (equivalent to >> in Normal mode)
  • <C-d> — remove one level of indentation (equivalent to << in Normal mode)
  • The amount shifted is controlled by the shiftwidth setting
  • These work on the whole line, not just the cursor position
  • 0<C-d> is a special form that removes all indentation from the current line

Example

Before (cursor at end of 'if condition:'):
    if condition:

Type <C-t> to indent the next line context, or use on current:
        if condition:

A practical workflow when writing Python:

def foo():
    if x:
        # cursor here, realize this block needs another indent level
        # press <C-t>
            # now properly indented

Tips

  • Useful when pasting code in Insert mode that needs indentation adjusted
  • Pairs well with <C-w> (delete word) and <C-u> (delete line) for Insert-mode editing
  • In Normal mode, use >> / << or > / < with motions for the same effect on multiple lines

Next

How do I visually select a double-quoted string including the quotes themselves?