0

Is it possible to reformat/reflow text files into two-column layouts (as in some newspapers)? I can do that in Word, but I'd rather do it in vim.

5
  • 4
    It's unclear what you want to accomplish... Do you want to split the screen into two columns and have different files (or different locations of the same file) in them? If so, you want windows and the :vsplit command. Or do you want to actually reformat/reflow your text into two columns? I'm afraid that concept would be very foreign to Vim, which is actually a text editor rather than a layout or typesetting tool... You could use Vim to write LaTeX or similar sources and have those tools typeset the contents for you... But have two columns in Vim, that would look odd... Can you explain?
    – filbranden
    Aug 17 '20 at 17:38
  • 1
    Thanks filbranden, Yes I was hoping to reformat/reflow the text into two columns using Vim. You are quite right that unlike Word, Vim is not a typesetting tool. Calling both of them editors is confusing.
    – elmclose
    Aug 18 '20 at 6:03
  • I think the OP wants two-column layout, like a newspaper. Probably vim can not do that, or maybe with some complex add-ons and scripting.
    – peterh
    Aug 18 '20 at 9:33
  • 1
    Try Dr. Chips MPage plugin: drchip.org/astronaut/vim/index.html#MPAGE Aug 18 '20 at 10:06
  • Excellent link Christian, thanks.
    – elmclose
    Aug 18 '20 at 10:56
2

Based on this unix.se answer, I would do

:%!pr -t -2 -w 200

Or some appropriate width (e.g., to insert twice textwidth, type Ctrl-r=&textwidth*2Enter after the -w ).

1
  • Good link Ben. I'd forgotten about fold and never heard of pr. With these two Linux commands I can get what I want. Of course I cannot edit the resulting columned output in Vim as it has no memory of Linux operations. But that is not a problem. I can edit in Vim but display using Linux commands. Thanks Ben, I will accept this as the answer.
    – elmclose
    Aug 19 '20 at 6:51
3

You can reuse split windows for that.

Sequence of commands:

  1. :vsplit to vertically split window
  2. :set scrollbind to bind current window scroll with new window
  3. :<C-w>w to go to new split window
  4. :<C-f> to scroll this new window one page down
  5. :set scrollbind to bind this window scroll to prev window

Now you have emulation of "two-column" layout.

I actually have this kind of command (named differently though, but here it would be called TwoColumns):

" Two columns.
" 1. Vertically split window
" 2. Offset it one screen
" 3. Scrollbind
command! TwoColumns
        \   exe "normal zR"
        \ | set noscrollbind
        \ | vsplit
        \ | set scrollbind
        \ | wincmd w
        \ | exe "normal \<c-f>"
        \ | set scrollbind
        \ | wincmd p

It additionally opens all folds, resets scrollbind and goes back to current window in the end.

PS, this command depends on default value of splitright (off by default). If splitright is set, one have to slightly change the command.

enter image description here

3
  • Thanks Maxim, this is very interesting, though it is not what I had in mind. I saw a similar idea in Vim Tips Wiki (vim.fandom.com/wiki/View_text_file_in_two_columns)
    – elmclose
    Aug 18 '20 at 6:12
  • Might be nice also to remap <c-f> and <c-b> to scroll twice as far, so using them replaces all the visible content with new content.
    – Rich
    Aug 18 '20 at 10:50
  • @Rich then one should maintain set of autocommands and state to reset them back as soon as "two-column mode" is off. Which wouldn't be that trivial :)
    – Maxim Kim
    Aug 18 '20 at 12:52

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.