My attempt so far:

" Essay mode
nnoremap <F5> :call Toggle_essay()<CR>
function Toggle_essay()
    if &linebreak
        setlocal columns=999
        setlocal nolinebreak
        setlocal columns=60
        setlocal linebreak

I want to map a single button to toggle between columns=60, and columns=[terminal width], or whatever was the default when vim is first opened.

Issue is that !tput cols always returns 80, $COLUMNS are not exported to vim, and I'm uncertain about the behaviour for window splits.

Surely there's a command equivalent to return columns to its default on-start behaviour?

  • That sounds wrong. You should not change the 'columns' setting it might break vim. Why do you think you need this? – Christian Brabandt Jun 27 '17 at 5:30
  • @ChristianBrabandt I want to use vim comfortably to write prose. I use my terminal fullscreen, which often results in a default column of about 200+, too wide to read and write long paragraphs of text comfortably. The only alternative to columns, if I'm not wrong, is textwidth; but that inserts unwanted newlines when it wraps text. – ning Jun 27 '17 at 6:43
  • You could open empty vertical windows. That is how those distraction free plugins do it. – Christian Brabandt Jun 27 '17 at 8:08
  • @ChristianBrabandt I don't like the idea of that, unfortunately. I'm considering mapping the toggle to textwidth instead of columns, and mapping a 'save without linebreak-wraps' key with something like nnoremap <F4> :textwidth=0<CR>gggq:w<CR> – ning Jun 27 '17 at 9:05
  • then that is the correct way to do it. – Christian Brabandt Jun 27 '17 at 9:20

Use columns&vim to set columns to its default value.

However, as explained in the comments above, one should avoid changing columns. Rather, a combination of textwidth=60, textwidth=999, and gggqG<C-o><C-o>, where:

  • gg – go to start;
  • gq – apply current formatting to all;
  • G – go to end;
  • <C-o> – return cursor to previous position).

It seems to me that explicitly storing the state in a buffer-local variable would be easiest. We also double this state variable to hold the previous value of columns, so it can be restored later:

fun! Toggle_essay()
        if !exists('b:essay_mode')
                let b:essay_mode = &columns
                setlocal columns=60 linebreak
                setlocal nolinebreak
                let &l:columns =  b:essay_mode
                unlet b:essay_mode

nnoremap <F5> :call Toggle_essay()<CR>
  • I've found the solution to be setlocal columns&vim. Unfortunately, I can't answer my own question for the next 23 hours or so. My next problem here is that in a vertical split, this function sets the total columns of both windows =60, instead of each window being =60. – ning Jun 27 '17 at 1:47
  • I'm not sure that it's possible to set the display width of just a window outside of columns and textwidth @ning. – Martin Tournoij Jun 27 '17 at 2:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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