We help IT Professionals succeed at work.

How to convert indentation to space in Vim ?

I typed following command in vim to show white space characters, such as eol, tab, and space.
:set listchars=eol:$,nbsp:_,tab:>-,trail:~,extends:>,precedes:<

The file was cloned from git repository.  Indentation is setup for tabs.  How do I convert indentation to space?

 50 addtask listtasks$
 51 do_listtasks[nostamp] = "1"$
 52 python do_listtasks() {$
 53 >-import sys$
 54   print('Hello World')$
 55 >-for e in bb.data.keys(d):$
 56 >->-if d.getVarFlag(e, 'task', False):$
 57 >->->-bb.plain("%s" % e)$
 58 }$
Watch Question

Software Engineer
Distinguished Expert 2019
In vi command mode:.
%s/\t/    /g

Open in new window

In the example above is 4 spaces.
For a language like python correct indentation is crucial. and spaces are NOT equivalent to tabs... in any way or form...


:%s/\t/    /g
This worked!  Could you please explain each character in this command?
: command mode
% probably means to apply the command to the whole file. not sure : i hardly ever use vi
s replace command
/ delimiter ... meaning s/X/Y/ replaces all instances of X with Y. "/" can be replaced with a different ascii character
nociSoftware Engineer
Distinguished Expert 2019

% is all lines (1,$) as a line selector.
the final g on the command means to replace ALL items matching on a line not just the first one.
\t is  a visible way to type TAB   (one can also use TAB character in that spot.


Thank you for solving the problem.