So I know that, in Vim, I can use the :retab command to convert an entire file from multiple-spaces to tabs or from tabs to spaces, depending on the value of :set expandtab.

But is there a way that I can convert spaces to tabs on a single line?

My old editor, Vile , has ^A-space and ^A-tab to do this line-by-line. But I cannot find the matching command in Vim.

1 Answer 1


The retab command accepts a range.

To convert spaces into tab in the current line (providing that the expandtab is set to no :set noet) you could do:


To convert spaces into tab on line 42 you could do:


More information with :help retab

You could get your key command back with the following mappings:

nnoremap <C-a><tab> <cmd>set noet<bar>.retab!<cr>
nnoremap <C-a><space> <cmd>set et<bar>.retab<cr>

vnoremap <C-a><tab> :<C-u>set noet<bar>'<,'>retab!<cr>
vnoremap <C-a><space> :<C-u>set et<bar>'<,'>retab<cr>

Remark: Be aware that with this mapping the Ctrl a that increments the next integer will hold timoutlen before triggering.

  • Perfect. Thanks. I'm still getting used to vim.
    – hymie
    Feb 20 at 20:12
  • Welcome to Vim! Thanks for the feedback :-) Feb 20 at 20:16
  • 1
    Another remark: the mappings will change the expandtab option as a side effect. All the lines formatted afterwards will be the same. If that's what you wanted anyway, it's perfect. If not, it may be surprising.
    – Friedrich
    Feb 21 at 7: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.