I previously asked the following question on how to indent the cursor to the identation of the above line:

How to ident the cursor to the indentation of the above line?

A correct answer was provided for how to achieve this in normal mode.

I wonder if it's possible to do the same in insert mode, so that I don't first have to enter normal mode?

Currently, I only know how to use Ctrlt and Ctrld to increase and decrease indentation, respectively, in insert mode.


There is an insert mode command for this, although it's a bit obscure. I use it frequently when I've hit, e.g., backspace too much, and need to reindent the current line.

Press Ctrl-f in insert mode.

The docs are at :help i_CTRL-F, in a section on 'indentkeys' format.

| improve this answer | |
  • Thanks, it seems to be working fine, although suddenly it started to indent backwards to the beginning of the line in my case. Do I need to "enable" indentkeys in my ~/.vimrc? If not, why? – Shuzheng Jan 28 at 4:21
  • @Shuzheng the default value of indentkeys contains the ctrl-d definition. Most filetypes set it appropriately. – D. Ben Knoble Jan 28 at 5:19
  • So, I should also set filetype on? – Shuzheng Jan 29 at 3:16
  • 1
    You should do filetype indent on if you want better indenting, yes, but C-f doesn’t require it. – D. Ben Knoble Jan 29 at 3:17

so that I don't first have to enter normal mode?`

What's the point of that? Normal mode is 'normal'. Do as much in it as you can. For instance, use == to fix line's indent in Normal mode.

Another problem is why you keep doing manual indent too much? That could be an indication that something is wrong with your setup (and that was the real point of the previous question). Normally one simply keeps typing while Vim fixes all indents on the fly.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.