5

Whenever I open a line using o or O, the cursor starts at the correct indentation (I'm not sure if vim uses syntax hints for this or just mimics the position of first character of the neighboring lines). Same happens when opening a line in insert mode with Enter keypress.

Is there a quick way to similarly auto-indent an empty line? Currently I'm just getting into insert mode and pressing tab manually to manually indent.

I most likely want to map this functionality to when A (append to line) is pressed on an empty line.

EDIT: I've seen How do I prevent vim from unindenting empty lines. This question is however different since I don't want to prevent vim from unindenting empty lines. I just want to be able to "revive indentation of emptied lines" so not all answers to that question apply to this question.

7
  • @StandardEyre that works perfectly! Please post it as an answer with a brief explanation (I had to look it up via :help S to understand why it works) and I'll accept it Jul 20, 2017 at 22:17
  • 1
    Possible duplicate of How do I prevent Vim from unindenting empty lines?
    – Herb
    Jul 21, 2017 at 4:16
  • @herb not exactly though. I don't want to prevent vim from unindenting empty lines. I just want to be able to "revive indentation of emptied lines" Jul 21, 2017 at 4:45
  • @PeeyushKushwaha How did S work for you? I think I have the same question as you do but S did not work for me, it only works in a line that already texts and correct indention.
    – zyy
    Jan 23, 2021 at 17:13

2 Answers 2

2

The solution is to use S on the empty line in normal mode.

If you search for documentation on it via :help S, you'll find

["x]S Delete [count] lines [into register x] and start insert. Synonym for "cc" linewise.

So merely typing S deletes the present line and starts insert (at the correct indentation).

Thanks to StandardEyre's comment, which led me to this answer.

2

As indicated on this related question, you cannot indent an empty line.

Your options are:

  1. to use S on the empty line in normal mode.
    • This deletes your empty line and starts a new line at the correct indentation
  2. to write something on the line then delete it
  3. to map CR to something that inserts white space and then deletes it right away
  4. to insert a line with white space characters (and redefine your idea of 'useless whitespace') ;-) instead of an empty line (no characters).
    • Note that this depends on (a) your use case and (b) your coding style.
    • When I use python I don't have a lot of constraints on coding style. Because I like to copy & paste python code I favor a line with white space, as supported by this answer

1
  • 1
    I use S on an empty line (previous line has indention) but get no indention, am I using it wrong?
    – zyy
    Jan 18, 2021 at 2:29

Your Answer

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

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