Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get vim to display wrapped lines without inserting newlines?

Tags:

vim

word-wrap

I'm getting back in touch with my inner (g)vim due to an unscheduled MacBook mother(board) of a meltdown (my emergency backup Linux box won't run TextMate). All told I'm happy with vim's efficiency and power, but I'm mortified at how hard it is to get the kind of word wrap that even stupid HTML textareas achieve with no apparent effort.

Consider the text

Etiam ornare mollis tortor. Suspendisse sed neque. Nullam in elit. Cum sociis nullam.

By default, with an 80-character width vim displays this as

Etiam ornare mollis tortor. Suspendisse sed neque. Nullam in elit. Cum sociis nu
llam.

This wrapping doesn't care about whitespace, so sometimes it just slices words into pieces (nullam in this case). Of course, you can turn on word wrap, and get this:

Etiam ornare mollis tortor. Suspendisse sed neque. Nullam in elit. Cum sociis
nullam.

The problem is that vim inserts a newline at the linebreak, which I most emphatically do not want. In other words, I want the text to display exactly as vim displays it with word wrap turned on, but without inserting a newline. (This way it can be pasted into HTML textareas and email programs, among other places.)

Web searches have yielded nothing of use, despite diligent effort; I hope StackOverflow can succeed where my Google-fu has failed.

like image 482
mhartl Avatar asked Jan 22 '09 01:01

mhartl


People also ask

How do I turn on line 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 does set wrap do in Vim?

If you want to make Vim wrap long lines to fit in the window, you first have to enable :set wrap . By default Vim will break lines at exactly the width of the window, which causes some words to be split across two lines. To prevent this from happening, you can enable :set linebreak .

What is GQ in Vim?

In Vim, you may want to format long lines, that is, wrap long lines so the longest is, say, 80 characters. The standard approach is to set the local 'textwidth' option, then use gq to format the wanted lines. :setl tw=80 gggqG. In the above, gggqG is gg (go to the first line) then gq (format) to G (the last line).

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

To turn on word wrapping:

:set wrap
:set linebreak

To copy & paste the original text and not any padding works for gvim. For vim in an xterm though, it copies the space padding at EOL (though not the newline). I thought this should work but it doesn't:

:set mouse=a
like image 132
pixelbeat Avatar answered Oct 27 '22 07:10

pixelbeat


I found this very helpful - works like a charm.

Basically I just added these lines in my vimrc

" not to break on words
set formatoptions=1
set linebreak

" fixing up moving line by line in the paragraph
nnoremap j gj
nnoremap k gk
vnoremap j gj
vnoremap k gk
like image 40
smilitude Avatar answered Oct 27 '22 08:10

smilitude