Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unwrap text in Vim?

Tags:

vim

I usually have the tw=80 option set when I edit files, especially LaTeX sources. However, say, I want to compose an email in Vim with the tw=80 option, and then copy and paste it to a web browser. Before I copy and paste, I want to unwrap the text so that there isn't a line break every 80 characters or so. I have tried tw=0 and then gq, but that just wraps the text to the default width of 80 characters. My question is: How do I unwrap text, so that each paragraph of my email appears as a single line? Is there an easy command for that?

like image 202
Ray Avatar asked Jun 14 '12 18:06

Ray


People also ask

How to turn off text wrapping in Vim?

Command to toggle word wrap in Vim While :set wrap will turn on word wrap in Vim and :set nowrap will turn off word wrapping, you can also use both commands with the ! (bang) symbol to toggle word wrap.

How to word wrap in Vim?

If you want to wrap lines in a specific area, move the cursor to the text you want to format and type gq followed by the range. For example, gqq wraps the current line and gqip wraps the current paragraph.

What is nowrap in Vim?

Answer: To get Vim to not wrap text, issue the "vim set nowrap" command, like this: :set nowrap. By default vim will wrap long lines in the editor, but this command changes that display, so long lines will now go off-screen to your right.


2 Answers

Go to the beginning of you paragraph and enter:

v i p J

(The J is a capital letter in case that's not clear)

For whole document combine it with norm:

:%norm vipJ 

This command will only unwrap paragraphs. I guess this is the behaviour you want.

like image 167
Fatih Arslan Avatar answered Sep 21 '22 09:09

Fatih Arslan


Since joining paragraph lines using Normal mode commands is already covered by another answer, let us consider solving the same issue by means of line-oriented Ex commands.

Suppose that the cursor is located at the first line of a paragraph. Then, to unwrap it, one can simply join the following lines up until the last line of that paragraph. A convenient way of doing that is to run the :join command designed exactly for the purpose. To define the line range for the command to operate on, besides the obvious starting line which is the current one, it is necessary to specify the ending line. It can be found using the pattern matching the very end of a paragraph, that is, two newline characters in a row or, equivalently, a newline character followed by an empty line. Thus, translating the said definition to Ex-command syntax, we obtain:

:,-/\n$/j 

For all paragraphs to be unwrapped, run this command on the first line of every paragraph. A useful tool to jump through them, repeating a given sequence of actions, is the :global command (or :g for short). As :global scans lines from top to bottom, the first line of the next paragraph is just the first non-empty line among those remaining unprocessed. This observation gives us the command

:g/./,-/\n$/j 

which is more efficient than its straightforward Normal-mode counterparts.

like image 43
ib. Avatar answered Sep 24 '22 09:09

ib.