I know there's a way because it happened by accident. I was frenetically moving in a short region of text and at some point, with some wrong keystroke, the leading spaces on the line became a tab (and the cursor was not there, but more on the right).

Anybody knows has an idea what did I press?

I'm pretty sure I was in normal mode, and probably also that no special keys are involved, iIrc.

2 Answers 2


Could you have typed >>? This would indent the current line further, and under certain settings (mostly related to softtabstop, shiftwidth, shiftround, etc.) could change a space into a tab.

Obviously u undoes the change, but << is the counterpart.

For correcting spaces vs. tabs after changing the appropriate settings, use :retab[!].

  • With my keyboard I can only press >> intentionally, I believe. Close to it there's only z and a... Furthermore, >> would have indented. Instead the leading spaces (I don't remember if they were 2 or 4, I'll check tomorrow) converted all toghether to a single tab.
    – Enlico
    Apr 15, 2021 at 17:44
  • Perhaps == then?
    – D. Ben Knoble
    Apr 15, 2021 at 19:02
  • >> is the vi command to shift the current line rightward; the shifting is controlled by the sw (shiftwidth) setting. Of course > can also take other movements, so >G will shift all lines from the current to the end of the document; and >'x will shift all lines from the current to the x mark, and so on). Also < is the command to shift towards the left.
    – Jim Dennis
    Apr 15, 2021 at 19:50
  • @JimDennis I'm aware, are you suggesting something is missing from the answer? Since only one line changed, the motion would have to be within the line.
    – D. Ben Knoble
    Apr 15, 2021 at 19:57

Normally I configure my vi/vim with

set expandtab
set tabstop=4 
set shiftwidth=4`

to "expand tabs" (to spaces), using "tab stops" of four spaces and the "shift width" also to four spaces.

To apply these changes to an existing file which already contains tabs I'd just use a common ex substitute command: :% s/^V^I/ /g

(Where ^V^I is Ctrlv (verbatim on the following keystroke) followed by Ctrli (the same as your Tab key on almost all systems), and the right hand side / / contains the desired number of spaces (four, in my case).

  • 2
    instead of using :s command for converting tabs to spaces, rather use :retab. It also supports the range argument. Apr 16, 2021 at 11:16

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.