How do I indent or dedent the current line without leaving insert mode?
Answer
<C-t> and <C-d>
Explanation
When you're typing in insert mode and realize the current line needs more or less indentation, you don't have to leave insert mode to fix it. <C-t> adds one level of indentation and <C-d> removes one level, both respecting your shiftwidth setting.
How it works
<C-t>(Ctrl+T): Inserts oneshiftwidthworth of indentation at the beginning of the current line, shifting the entire line right. Your cursor stays in its relative position within the text.<C-d>(Ctrl+D): Removes oneshiftwidthworth of indentation from the beginning of the current line, shifting it left.- Both commands work regardless of where your cursor is on the line — they always affect the line's leading whitespace.
Example
With shiftwidth=4, suppose you're typing inside a function and realize you need to nest deeper:
Before (cursor at |):
function test() {
if (true) {
console.log("hello")|
Press <C-t> to indent:
After:
function test() {
if (true) {
console.log("hello")|
Press <C-d> to dedent back if needed.
Tips
0<C-d>removes all indentation from the current line while staying in insert mode^<C-d>temporarily removes all indentation for the current line only (restores on next line)- These are faster than exiting to normal mode, using
>>or<<, and re-entering insert mode