When I create a new line or hit o I have Vim configured to match the previous indentation.

However, since I use hard tabs in most places, vim will replace any spaces that are greater than or equal to the current shift width with hard tabs.

Since I mix tabs and spaces depending on logical indent (tabs) and alignment (spaces), this gets really annoying.

For example, take the following comment ( = tab, = space):

›   /*
›   ›   Returns:
›   ›   ○○○○0 for success
›   ›   ○○○○non-zero for failure
›   */

If I go to the end of the 0 for success line and hit enter (or just go to the line and, in normal mode, hit o), seeing as how my tab width is 4, vim will copy the indentation but replace all sequences of 4 spaces with a hard tab instead of keeping them as 4 spaces.

›   /*
›   ›   Returns:
›   ›   ○○○○0 for success
›   ›   ›   <-- super annoying
›   ›   ○○○○non-zero for failure
›   */

Here is my current indentation configuration:

set autoindent
set nosmartindent
set nocindent
set indentexpr=-1
set indentkeys=
filetype plugin indent off
autocmd FileType * setlocal indentkeys=
autocmd BufNewFile,BufRead * setlocal indentkeys=

How do I get Vim to match the indentation exactly when it creates a new line with automatic indentation?

  • 2
    did you already try set copyindent?
    – Mass
    Apr 13, 2019 at 19:18
  • 1
    @Mass Good lord, that worked. In all of my google searches I did not come across that whatsoever. I've been dealing with this for 2 years and have looked to fix it a number of times. Can you create an answer so I can mark it as accepted? Apr 13, 2019 at 19:21

1 Answer 1


According to the comments,

set copyindent

should do it

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.