Using :h sub-replace-special: %s@^ \+@\=repeat("\t", strlen(submatch(0)) / &ts)


You don't need spaces on empty line. This is the reason Vim removes auto-indentation after Esc. Whenever you're going to write something on empty line with indentation applied, you can press cc or S. Or you can re-indent text after you've finished with it (e.g. == or ='[ and so on).


You have autoindent on: *'autoindent'* *'ai'* *'noautoindent'* *'noai'* 'autoindent' 'ai' boolean (default off) local to buffer Copy indent from current line when starting a new line (typing <CR> in Insert mode or when using the "o" or "O" command). If you do not type anything on the new line ...

Only top voted, non community-wiki answers of a minimum length are eligible