When I write, I tend to place hard returns inside my paragraphs. I use Vim as my writing composition tool. Later, I need to remove these same hard returns when I transfer my writing to a word processor in order to format and print my document.
How do you remove hard returns inside a paragraph? How do you get rid of some newlines, but not all newlines? How do you leave your paragraph breaks unscathed in a simple text file, yet, at the same time, remove all newlines that are internal to that paragraph?
I remove newlines internal to a paragraph with a regular expression. I might use more than one regular expression to get the job done.
Here's a list of commands I might use and have used in the past:
qa /.\{50\}\n.../ J zz q @a @@
Some notes about the above commands:
- The qa command starts to record a macro.
- The forward slash is a search. To be extra explicit, you can also place a trailing forward slash on the search string, but do not have to.
- The dot, or period, represents any character. In regular expressions, a dot is any character.
- The number 50 is a multiplier. It multiplies the previous character which is the dot.
- The entire expression, .\{50\} translates as any 50 characters.
- The entire expression /.\{50\}\n.../ translates to search for a line that is at least 50 characters long followed by a line that is at least 3 characters long
- The \n is a newline or hard return. In other words, the \n is the character we get rid of when we remove hard returns from our paragraphs
- The Capital-J is what we use to get rid of newlines. Capital-J is a command. It means join this line with the next line.
- The zz command completely redraws the screen so that the 2 lines that have been joined together are now in the center of the screen. I want to be able to see my work in order to check it. Each time I join 2 lines together, I want to check to see that this is truly what I intended.
There are many many variations on the above series of commands. The point? Find a regular expression that identifies hard returns in the middle of paragraphs and it becomes easier to remove them.
To make this task even easier, turn the whole hard return removal operation into a macro. Macros are one of my favorite vim features.
Ed Abbott
No comments:
Post a Comment