I've had this problem many times:
I have a source code, but if I copy / paste it into Wordpress and enclose it with <code>
</code>
the beginning spaces are "compressed" into one.
Thus I'd like to know how I could change all the spaces only at the beginning of the line by
.
I.e.
extend: 'Ext.panel.Panel',
becomes
extend: 'Ext.panel.Panel',
I would propose the following three solutions addressing the issue that are
listed below in order of my personal preference.
Substitution using the preceding atom matching syntax (see :help
\@<=
).
:%s/\%(^ *\)\@<= /\ /g
If brevity of the command is crucial, one can shorten it using "very
magic" mode (see :help \v
) and changing capturing group (:help
\%(
) to non-capturing.
:%s/\v(^ *)@<= /\ /g
Two-staged substitution that splits a line just after leading spaces,
replaces those spaces, and rejoins that line.
:g/^/s/^ \+/&\r/|-s/ /\ /g|j!
Another two-step substitution that replaces each of the leading spaces
by certain symbol that does not occur in the text, and changes that
symbol to
.
:exe"g/^ \\+/norm!v//e\rr\r"|%s/\r/\ /g
:%s/^ \+/\=repeat(" ",strlen(submatch(0)))
But it wouldn't surprise me if there's a shorter substitute command. Come on Vimgolfers!
Using a look-behind assertion to replace spaces precedeed by only spaces at the beginning of a line:
%s/\(^ *\)\@<= /\ /g