Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have 2 files

file1.txt
a =
b =

file2.txt
1
2

Can I merge them using gvim so that the output looks like :
a = 1
b = 2

share|improve this question
    
There is a similar question (2 blocks from the same file) on SO: Merge multiple lines (two blocks) in Vim – ThiefMaster Jun 5 '12 at 6:42
up vote 7 down vote accepted

Yank a vertical block: Select e.g. the lines in file2.txt with CTRLv, yank with y and paste after moving to end of the first line in file1.txt with p.

If you run vim on a GNU stack you could also use the paste program to do the same. Use -d to specify what delimiter to use between the fields from the files.

paste -d "" 1 2 > merged.txt
share|improve this answer

works with pure vim as well:

 % vim -O file1.txt file2.txt  # open both files vertically splitted

then in file1.txt yank the block to a named register, lets say 'a':

  • gg go to begin of file
  • ctrlv go to select block mode
  • G go to last line
  • $ go to last character on last line
  • "ay yank to register 'a'

then switch over to file2.txt and do a:

  • gg go to begin of file
  • "aP paste content of register 'a' before text
share|improve this answer

if you're working on Unix or Linux, just use the paste command:

:%!paste - file2.txt

this also works in the classic vi, or direct from the command line:

$ paste file1.txt file2.txt >out.txt

Hope, this helps

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .