Question

So i need to edit something in every line except the ones that begin with #, because they are comments. I need to replace all tabs by +.

I know in Vi I can translate stuff like this:

1,$s/a/A/g , replaces all a's by A

but how do i add the range which specifies all lines that don't start with #?

Was it helpful?

Solution

The g (global) ex command does what you want:

:g!/^#/s/^I/+/g

The general format is g [!] /RE/ CMD, where RE is a pattern describing all the lines to apply the command to and CMD is any ex command. The optional ! reverses the sense of the RE, applying the command to all lines that DON'T match.

To get a TAB character on the line (the ^I above), you need to hit ctrl+V followed by TAB

OTHER TIPS

Seems like this could be achieved with a macro trick

qr/^[^#]<Enter>:s/\t/+<Enter>q
3000@r

Record a macro that will move to the next line which doesn't start with a #. Replace all spaces on that line with a tab character. Then just repeat that macro several hundred times (or whatever number is large enough for your file)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top