17

For example I have this JavaScript code. The | character represents the cursor position and it is on an empty line.

function a() {
    console.log("a");

    function b() {
        console.log("b");

        function c() {
            console.log("c");
        }
|
        c();
    }

    b();
}

After the requested operation, the contents would look like this:

function a() {
    console.log("a");

    function b() {
        console.log("b");

        function c() {
            console.log("c");
        }
        |
        c();
    }

    b();
}

What I am asking for is a mapping command.

1 Answer 1

21

As @jamessan mentioned, <C-f> will indent to the correct place from insert mode. You can also use <C-t> and <C-d> to increase or decrease the indention level from insert mode.

However, you may be able to avoid getting into that situation completely by using one of the following techniques:

  • Use o/O to start a new line from a previous line
  • Use cc/S to edit the line (takes indention into consideration)

For more help see:

:h cc
:h S
:h o
:h i_CTRL-T
:h i_CTRL-F
2
  • 4
    Also, <C-f> is typically defined in 'indentkeys' or 'cinkeys' (depending on which indent mechanism is being used) which will allow you to reindent the current line to where it should be, from insert mode. See :help i_CTRL-F
    – jamessan
    Dec 8, 2015 at 17:33
  • Note you need to :set cindent or these keys do nothing!
    – JonnyRaa
    Jun 14, 2018 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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