## 18.13. vi Macro for Splitting Long Lines

When you add text to the start of a line and make the line longer than your screen's width, vi won't break ("wrap") the line unless your cursor crosses the wrapmargin point near the righthand edge of the screen. You can get lines that are too long.

Here are two macros that cut (Kut) the current line:

Go to http://examples.oreilly.com/upt3 for more information on: exrc

>     map K 78^V|lBhr^M
>     map K 078lF r^M

The first macro doesn't seem to work on some versions of vi. It's the better one though, because it uses the `|` (vertical bar) command to move to column 78, even if there are TABs in the line. Then it moves one more character to the right (if it can), moves back to the start of the word, moves back one more character onto the blank or TAB before the word, and replaces that character with a RETURN.

The second macro counts TABs as single characters, but it works on every version of vi I've tried. It moves to the left edge, then to the 79th character, then back to the previous space. Finally, it replaces that space with a carriage return.

You might try adding a `J` to the start of either macro. That'll join the next line to the current one before cutting; it might make a nicer "wrap." Another way to do this is with a filter-through (Section 17.18) and the fmt (Section 17.28) command:

>     !!fmt

That will break the current line neatly, though it also might change the spacing after periods (.) or replace leading TABs with spaces.

-- JP

Copyright © 2003 O'Reilly & Associates. All rights reserved.