LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-03-2014, 02:46 PM   #1
cmunger
LQ Newbie
 
Registered: Mar 2014
Location: Dallas, TX
Distribution: RHEL 6.5
Posts: 15

Rep: Reputation: 0
vim editor copying text to another file


Hey guys,

I am trying to copy character strings from one text file, and need to paste them in another (separate) file. Needless to say, I can't figure this one out.

Its probably so simple I'm overthinking it.



Thanks in advance!
 
Old 03-03-2014, 02:54 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,520

Rep: Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106
http://stackoverflow.com/questions/4...her-file-in-vi

Short version:

Use 'yy' to copy a line (or 'dd' to cut a line) and 'p' to paste. If you want to paste into a different file, then use 'yy' (or 'dd') to copy it, then open the second file from within vi with ':e', and paste it there

Ex:
vi /path/to/first/file
arrow down to the line you want to copy
yy
:e /path/to/second/file
arrow down to the line where you want to paste
p

Last edited by suicidaleggroll; 03-03-2014 at 02:56 PM.
 
1 members found this post helpful.
Old 03-03-2014, 02:58 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
One method could be to open the files together in a split window and navigate between them using Ctrl-w:
Code:
vi -o file_one file_two
 
1 members found this post helpful.
Old 03-03-2014, 03:01 PM   #4
cmunger
LQ Newbie
 
Registered: Mar 2014
Location: Dallas, TX
Distribution: RHEL 6.5
Posts: 15

Original Poster
Rep: Reputation: 0
well that makes sense...*insert facepalm*
That solved half my problem of moving them to a new file.

But how would I copy just the specific lines that the character string, for instance "cat" exists in? The file size is 1000 lines, and I cant seem to figure out how to copy all of the instances at once.

EDIT: I need the whole line, but only the ones that have the string "cat" in them.

Last edited by cmunger; 03-03-2014 at 03:06 PM.
 
Old 03-03-2014, 03:15 PM   #5
cmunger
LQ Newbie
 
Registered: Mar 2014
Location: Dallas, TX
Distribution: RHEL 6.5
Posts: 15

Original Poster
Rep: Reputation: 0
UPDATE!!!:

Got it. Opened the two windows, and manually moved the files. At least that worked. Then remembered grep and >
Thanks for the input guys, learn something new every day right?
 
Old 03-03-2014, 05:59 PM   #6
byau
Member
 
Registered: Sep 2009
Location: Los Angeles, CA
Posts: 33

Rep: Reputation: 5
If you only have one terminal, this is what I do

Let's say you have file a.txt and b.txt. You want some of a.txt in b.txt

If the files are small

1) vi b.txt
2) navigate to where you want some of a.txt
3) type in two lines of "========="
4) put your cursor in between the two lines
======
[put cursor here]
======
5) :r a.txt

This should put all of a.txt in between your "=====" signs. I use the "=====" signs as markers so I know where a.txt is. Once I have deleted what I need and only have the lines I want, i then delete the "=====" lines

If the files are longer, then you can always do
1) cp a.txt a.txt.stage
2) vi a.txt.stage
Delete all lines you need so a.txt.stage only contains the lines you want
3) vi b.txt
4) navigate to where you want the excerpt to be
5) :r a.txt.stage


Two very handy vi tricks: using the "r" and "!"

"!" will run a command line. So if you want the output of a command inserted into a file you can do something like

:r !ls

Hope that is what you were looking for.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
best text editor (vi vs. vim etc) doughyi8u Linux - Software 30 12-17-2016 12:36 AM
[SOLVED] vi / vim editor - how to display text file one line on one row ? masuch Linux - Newbie 3 08-06-2012 09:14 AM
Editor comparison: vim VS vim-lite, Cleaning vim Ruler2112 *BSD 4 04-13-2009 04:26 PM
Which light text editor can copy text from file and paste in browser? davidas Linux - Software 9 03-06-2006 11:28 AM
copying blocks of text using vim p0rt Linux - Software 2 09-23-2004 03:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration