2

I am a tabs kind of guy, I do not like when my code contains leading whitespaces instead of leading tabs. I often exchange codes with colleagues and I first have to convert the leading whitespaces to tabs manually before doing anything else.

Is there a way to search for whitespaces but not tabs in vim? My end goal is to write a script that will convert leading whitespaces to tabs automatically, but an answer to the above question will put me in the right track.

2
  • / will search for single space (there's a space after slash)
  • /^ \+ will search for leading whitespaces
3

In addition to @grodzik's answer here are two points that might interest you:

You can have vim showing the tabs and spaces characters. If you use:

set list
set listchars+=tab:>-,space:.

You'll see lines like this (Here the line begins with 2 tab characters and 4 spaces characters):

enter image description here

See :h 'list' and :h 'listchars'


Also about the transformation of the tabs to spaces the :retab command might interest you. From :h :retab:

With !, Vim also replaces strings of only normal spaces with tabs where appropriate.

Thus in a the buffer containing my previous example if I use :retab! I'll get:

enter image description here

You can imagine using the command in an autocommand for example to automatically convert your new buffers.

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.