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

How do I copy a few lines from one file to another file using the nano editor?

share|improve this question

migrated from stackoverflow.com Feb 8 '13 at 14:54

This question came from our site for professional and enthusiast programmers.

1  
you specified nano in question, but with vim and gedit tag? – Kent Feb 7 '13 at 23:03
up vote 8 down vote accepted

Assuming you are in pure console mode and can't use the mouse to copy/paste:

  1. Launch nano in multi-buffer mode (nano -F)
  2. CTRL-^ to start your selection.
  3. Arrow key around until you cover all the text you want to copy.
  4. ESC-^ to copy the selection into the cut buffer
  5. CTRL-R ESC-F to open a file into a new buffer
  6. CTRL-U to paste in the opened file

NOTE: To switch between buffers use either ESC-< and ESC-> or ESC-, and ESC-. (the later is helpful if < and > use the same key on your keyboard layout)

An alternative to 2-4 above is to go to the line(s) you want to copy and CTRL-K to delete them, pressing CTRL-K repeatedly to multiple lines to the buffer. When you've cut all the lines you want to copy, CTRL-Y to re-paste them back into the current buffer. Then continue with step 5.

ALT or any other keys with Meta-key behaviour could also be used instead of ESC in these commands.

share|improve this answer
    
What key is ^ for steps 2 and 4? And what keys are 'ESC-<' and 'ESC->'? Is it Escape and then hyphen and then shift comma / period all together? – anon58192932 Apr 21 at 13:55

Copy text from one file to another with nano text editor

Note: To help you understand better, we will use a

source file: /var/named/athens.local

destination file: /var/named/patra.local

  1. Open the destination file (the file that want to paste the text into), by using nano's multiple buffer.

nano -F destination_file

So we have:

nano -F /var/named/patra.local
  1. Press Ctrl+r.

  2. From inside nano editor, open the source file

    /var/named/athens.local

  3. Press ctrl+^ (this will enable the "mark set" mode)

  4. Select your text.

  5. When you have marked all the text you want, copy the text to

clipboard by pressing Alt+^

Note: Now your text is in clipboard.

Note: In help file you will see the Alt+^ described as M-^. 
  1. Press ctrl+x, to close the source file (/var/named/athens.local).

    Now we will see the destination file (/var/named/patra.local).

Move the cursor to the place you want to paste the copied text (which is

in buffer, don't forget that).

Press ctrl+u, to paste the text.

You are done.

share|improve this answer
    
I like this answer better than the accepted answer because you explain step by step what is actually happening. Thank you. – vrijdenker Apr 8 at 8:11
    
This is extremely clear thank you. To anyone else wondering - the ^ symbol really does mean Shift + 6. So to set the mark it's Control + Shift + 6 on a Windows / Linux keyboard. And to save to clipboard it's Alt + Shift + 6. – anon58192932 Apr 21 at 14:01

You must log in to answer this question.

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