When I open my .java file in vim, I could see a couple of lines prefixed with one / more ^I characters. It looks like tabs in Eclipse that has got converted into ^I.

I would like to replace a single ^I into spaces with 4 characters.


^I^I^I^IList<History> rulePackagesHistory = result.getHistory();

How can do that in vim editor?


Add these lines to your .vimrc:

set tabstop=4
set shiftwidth=4
set expandtab

After that, each new tab character entered will be changed to 4 spaces, old tabs don't. You must type:


This will convert all existing tabs in files to spaces.

If you don't want to use retab, you can use perl to replace each tab by 4 spaces:

perl -i.bak -pe 's/\t/    /g' file
|improve this answer|||||

For the specific file you can run the following while being in vim

You will need to press Ctrl+V+I to create ^I.

:%s /^I/^I^I^I^I/g
|improve this answer|||||
  • it did not work me. – user648330 Aug 21 '14 at 10:22
  • That is quite strange. I'm not sure if you used literal ^I or the one I explained in the answer – Valentin Bajrami Aug 21 '14 at 10:33
  • I guess command could be :%s /^I/[4spaces]/g – user648330 Aug 21 '14 at 10:38
  • Well, yes if you want to replace the tab ^I with spaces then you indeed put spaces there as :%s /^I/ /g In your example(question) You've 4 ^I^I^I^I which indicate tabs. – Valentin Bajrami Aug 21 '14 at 10:50

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.