Hint to Part 8 Exercise

Using a global command can cut the length of the command sequence roughly in half. The correct way to use it depends on something I did not explicitly say about global commands, but which you should be able to guess from what I did say.

Back to the question


Answer to Part 8 Exercise

A fairly simple way to handle both writing the tab-revised version of your file and keeping the original version in the editor buffer is this sequence:


:.g/^/%s/\({*\)^I^I/\1{/g|%s/^\({*\)^I/\1    /|%s/{/^I/g|w
u

The first line is pretty straightforward, excepting the initial global command. Otherwise it just replaces every pair of tabs at the start of a line with the dummy character ``{'', then changes any remaining solitary tab in the initial whitespace with four space characters, changes every dummy ``{'' to a single tab, and finally writes the file.

That initial global command seems silly, I know. It scans over just the current line, it marks that line without fail because every line has a starting point, and so it ends up running the remaining commands on the line for sure and exactly once. This is just what the command line would do without that initial global. So why is it there?

The answer is in that second line. When you run an undo after a global command, you don't just undo the last command the global ran; you undo every buffer change done by every command the global ran. (Note that the u is not preceded by a colon (``:''); it is a screen-mode command.) So as soon as the write is finished, the undo puts the entire buffer back as it was.

Back to the question


Back to the index