Search code examples
vim

Pasting some text at the right side of another text using Vim's column editing mode


I don't usually use Vim's column editing mode.
But I'm curious if I can do this.
Suppopse I have file like this.

KE987 ICN                          
Seoul/Incheon(Incheon)
30MAY2021(Tue) 13:20 (Local Time)
Terminal No : 2

LHR
London(Heathrow)
30MAY2021(Tue) 17:25 (Local Time)
Terminal No : 4

Using visual column enditing mode, can I make the file like this? I mean copying the bottom 4 lines and putting them in the right side of the top 4 lines.

KE907 ICN                               LHR
Seoul/Incheon(Incheon)                  London(Heathrow)
30MAY2021(Tue) 13:20 (Local Time)       30MAY2024(Tue) 17:25 (Local Time)
Terminal No : 2                         Terminal No : 4

ADD :
This is in reponse to romainl's answer to show my problem.
When I try to select to bottom block, ctrl-v, starting from the first character, down to the end line and right to the end of line, I can only select part of the block as shown below. This is because the length of each line differs and I cannot go further than the end of line of the last line and can't select the whole previous line.
enter image description here


Solution

  • It certainly is possible, but with some caveats:

    1. Move the cursor to the first character of the bottom paragraph.
    2. Press <C-v> to enter visual-block mode.
    3. Extend the selection vertically with the appropriate motion(s).
    4. Extend the selection horizontally to include every line in whole with $.
    5. Cut the visual selection with d.
    6. Move the cursor to the first character of the top paragraph.
    7. Press A to enter insert mode at the end of the line.
    8. Insert as many spaces as needed: <Space><Space><Space>
    9. Leave insert mode.
    10. Put the content of the default register with p.

    The main caveat is that the block you yanked and the target block must have the same number of lines, which is the case in the example you gave.

    Alternatively, you can use external programs:

    :1,4!paste - <(sed -n '6,9p' %) | column -ts $'\t'
    :5,9d
    

    where:

    • :1,4!<cmd> filters lines 1-4 though external command <cmd>. The text is passed as stdin to <cmd>.
    • paste <file1> <file2 "pastes" the content of <file1> and <file2>.
    • paste - <file2> "pastes" the text passed as stdin and <file2>.
    • <(<cmd>) exposes the output of <cmd> as if it were a file.
    • paste - <(<cmd>) "pastes" the text passed as stdin and the output of <cmd>.
    • sed -n '6,9p' % extracts lines 6-9 of current file. % is expanded by Vim before running the command.
    • :1,4!paste - <(sed -n '6,9p' %) "pastes" lines 1-4 of the current file and lines 6-9 of the current file.
    • | column -ts $'\t' neatly aligns the output of the previous command.
    • Finally, :5,9d deletes lines 5-9.

    See $ man paste, $ man sed, $ man column.