8

I've enabled set nowrap since I find that line wrapping makes it harder for me to quickly scan a document. It's also annoying having to scroll right to see the entire line that I am working on.

I'm wondering if there's a way to enable line-wrapping only on the line that my cursor is currently on and exclude all other lines.

3
  • 1
    I don't think there's any way to set it for one line only, but you can try the solution here... stackoverflow.com/questions/37734719/… Commented Jul 8, 2016 at 21:22
  • You could try detecting if you are on a long line (assuming the size of your window doesn't constantly change, this shouldn't be a problem), and toggling :setl wrap and :setl nowrap over and over, but that will probably be slow for large files. Commented Jul 8, 2016 at 21:23
  • 2
    that would give a nasty "wabbling" effect on scrolling and no this is not possible. Commented Jul 9, 2016 at 4:39

4 Answers 4

6

I was thinking about this more. You can't wrap just the current line in the current window, but you can open the buffer in a new window and enable wrapping there without affecting your current window.

Use a new window

split | setlocal wrap

After you're done, close the window that has wrapping enabled.

Use a new tab

If your horizontal space is limited because you're using multiple windows, opening a new tab will let you use the full terminal size without losing your current window layout.

tabedit % | setlocal wrap

Since it's the only window in that tab, you can close it with :q instead of :tabclose. You don't even have to write the file.

Either of the options above can be made into quick keys:

nnoremap <leader>s :<c-u>split<cr>:setlocal wrap<cr>
nnoremap <leader>t :<c-u>tabedit %<cr>:setlocal wrap<cr>

Use NrrwRgn

Another way of editing the lines in a split is using NrrwRgn. It's a plugin that takes a range of lines and isolates them in a new window. You select the lines you want to focus on and press <leader>nr. The drawback to this is that it's a new buffer and you won't have the undo history from the original buffer.

This autocmd will set the wrap option in the NrrwRgn window:

autocmd FileType * if exists('w:nrrw_rgn_id') | setlocal wrap | endif

FileType had to be used because BufWinEnter doesn't trigger.

4

Soft wrapping per line is not possible. All you can really do is manually wrap the line with gql and undo it after you're done looking.

It might not be the exact answer that you're looking for, but you can make horizontal scrolling more ergonomic.

nnoremap <left> 5zh
nnoremap <right> 5zl
xnoremap <left> 5zh
xnoremap <right> 5zl

Using the <left> and <right> arrows, you'll horizontally scroll by 5 characters. You can adjust the count to what feels right. A count can be used to make bigger horizontal jumps. Since 5 is used in the map, 5<right> would jump 25 characters to the right. The key can be held to keep scrolling, but the count won't be repeated.

If you don't want to use the arrow keys, you could still use zh, zl, zH, and zL a little more efficiently using the vim-repeat plugin.

function! s:scroll(keys) abort
  silent! call repeat#set(a:keys, v:count1)
  execute 'normal! '.v:count1.a:keys
endfunction

nnoremap <silent> zh :<c-u>call <sid>scroll('zh')<cr>
nnoremap <silent> zl :<c-u>call <sid>scroll('zl')<cr>
nnoremap <silent> zH :<c-u>call <sid>scroll('zH')<cr>
nnoremap <silent> zL :<c-u>call <sid>scroll('zL')<cr>

With vim-repeat involved, you initiate scrolling with the standard horizontal scroll maps, then hold . to keep repeating it. In this method, using a count (like 5zl) will be repeated.

1

I don't think setting the line-wrapping for just a single line is possible. But, there is a small work around that you can use to see the entire content of the current line. First set the text width as

:set textwidth=80 "Any number that works for you

And then to format the current line do a gqq on it and now you should be able to see your entire line. (help gqq for more)

But Unfortunately, this doesn't preserve the line number of the original line since it breaks the lines in to multiple lines. So, you have to undo the change every time you do this. I hope there is a better way than this but this is what I can think of now.

This whole process might become easier if you can put a toggle mapping of this with your Leader key. That way it should be much faster.

Edit: As pointed out in the comments, this method won't work when you've to edit a line because that will be in your undo stack. If you are using an Undo Tree plugin like GUndo or UndoTree, it should be fine for you as you can go back and forth in the list.

4
  • You could use the J (or gj) operator to join lines back together afterwards quickly. You'd have to map gqq to preserve original line length so you had some way to know how many lines were created when splitting it up, then join that many lines together to put it back together... Commented Jul 8, 2016 at 21:26
  • Yeah that's possible but hitting the u for undoing it should be easier as long as you don't mind having a couple more entries in your undo tree. Commented Jul 8, 2016 at 21:28
  • until you make edits to the line... Commented Jul 9, 2016 at 4:07
  • Oh, Yeah. I didn't think about that at all. So, I guess this works only when you are just trying to look at a line. Commented Jul 9, 2016 at 7:05
1

I've just made a plugin for this: vim-softwrap.

For now it works for me (and I use nu+rnu to make things more complicated, ahah), but I'm happy to hear some feedback.

Here's a screencast of it in action.

As you can see there's an option g:softwrap_unwrap that defaults to v:false to give the normal behavior (popup constrained in the current window), but can be changed to v:true to let the popup extend for as much as possible.

asciicast


The green bar when I move across windows is from another plugin of mine, WinZoZ.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.