90

Is it possible to copy text from a file, opened with nano, to the shell?

I have a text file, and I want to copy several lines to the console, but I cannot find a keyboard shortcut to copy the text.

1
  • 2
    are you using a mouse?
    – Leon
    May 28, 2015 at 12:49

14 Answers 14

76

Nano to Shell:

1. Using mouse to mark the text.

2. Right-Click the mouse in the Shell.

Within Nano:

1. CTRL+6 (or CTRL+Shift+6 or hold Shift and move cursor) for Mark Set and mark what you want (the end could do some extra help).

2. ALT+6 for copying the marked text.

3. CTRL+u at the place you want to paste.

or

1. CTRL+6 (or CTRL+Shift+6 or hold Shift and move cursor) for Mark Set and mark what you want (the end could do some extra help).

2. CTRL+k for cutting what you want to copy

3. CTRL+u for pasting what you have just cut because you just want to copy.

4. CTRL+u at the place you want to paste.

6
  • 19
    But it is not copy, it is cut !
    – TonySalimi
    Sep 1, 2016 at 10:02
  • 21
    this doesn't work if the text is longer than the current display.
    – Jorjani
    Sep 13, 2018 at 13:21
  • 5
    u didnt mention that use alt + 6 to copy the marked text.
    – hariszhr
    Mar 1, 2019 at 19:28
  • 5
    The problem here is that your mouse cannot copy longer than the display in nano .
    – Brandon
    Sep 5, 2019 at 18:43
  • 2
    If ALT + 6 didn't copy, use ALT +SHIFT + 6 Oct 22, 2021 at 8:45
35

Much easier method (for short pieces of text):
$ cat my_file

Ctrl+Shift+c to copy the required output from the terminal

Ctrl+Shift+v to paste it wherever you like

9
  • this is not the response required, the question is how to cpy text displayed into nano editor
    – danilonet
    Sep 26, 2019 at 11:17
  • 5
    Indeed, it is the only appropriate response required to solve the problem at hand. This will be extremely useful to those that find out it cannot be done in nano, but still wish to copy lines from a text file. We should accept the answer 'It cannot be done' and offer useful alternatives. Oct 8, 2019 at 18:08
  • Actually, the question (unless altered in an edit) is not how to copy text display into the nano editor but out of it. Dec 13, 2020 at 20:33
  • I'm sorry, my bad. I was replying to @DaniloCalzetta. I thought it was obvious by context but it was not. My apologies. Dec 15, 2020 at 10:34
  • You mean my first comment? I can only edit after ~5 minutes or so. SO rules. Dec 15, 2020 at 14:03
25

For whoever still looking for a copy + paste solution in nano editor

To select text

  • ctrl+6
  • Use arrow to move the cursor to where you want the mark to end

Note: If you want to copy the whole line, no need to mark just move the cursor to the line

To copy:

  • Press alt + 6

To paste:

  • Press ctrl + U

Reference

0
11

nano does not seem to have the ability to copy/paste from the global/system clipboard or shell.

However, you can copy text from one file to another using nano's file buffers. When you open another file buffer with ^R (Ctrl + r), you can use nanos built-in copy/paste functionality (outlined below) to copy between files:

  • M-6 (Meta + 6) to copy lines to nano's clipboard.
  • ^K (Ctrl + k) to cut the current line and store it in nano's clipboard.
  • ^^ (Ctrl + Shift + 6) to select text. Once you have selected the text, you can use the above commands to copy it or cut it.
  • ^U (Ctrl + u) to paste the text from nano's clipboard.

Finally, if the above solution will not work for you and you are using a terminal emulator, you may be able to copy/paste from the global clipboard with Ctrl + Shift + c and Ctrl + Shift + v (Cmd + c and Cmd + v on OSX) respectively. screen also provides an external copy/paste that should work in nano. Finally if all you need to do is capture certain lines or text from a file, consider using grep to find the lines and xclip or xsel (or pbcopy/pbpaste on OSX) to copy them to the global clipboard (and/or paste from the clipboard) instead of nano.

4
  • 17
    What is Meta? I don't have such a key on my keyboard.
    – greatvovan
    Sep 29, 2018 at 0:14
  • 1
    M- stands for the Meta key, which can mean pressing the "Alt" key simultaneously with the letter key or by pressing "Esc" key before the letter key. "Alt" generally only works when using an X version of the application, so when using a dumb terminal, only "Esc" will work. Quote from: link answer by @FredericLachasse Apr 26, 2019 at 14:55
  • Meta is the OS system key; so, Win on Windows, that weird looped square thing or Command on macOS. In nano, however, M stands for Alt – don't ask me why. Dec 13, 2020 at 20:30
  • What if all I need to do is indeed capture certain lines of text from a "file", but that file happens to be the crontab of another user (accessed with sudo -u whatever crontab -e? Where do I find that text file (if it is such) on the filesystem, so that I can grep it or even open it with another editor?
    – Andyc
    Apr 10 at 9:48
10

The thread is quite old, but today I humbled around with the same question and all the mentioned solutions above did not help. As I wished to copy long lines my solution is - acording to what @themisterunknown wrote above - outside nano. I used awk!

awk '{ if (NR==87) print $0 }' filename

where NR==[line number] and $0 is complete line.

2
  • This should be marked as the correct answer in my opinion. The question clearly asks to paste the text outside the nano editor, which, if the line is too long to fit on the screen is not possible within nano. Aug 5, 2019 at 15:49
  • This is THE answer, so simple and straightforward
    – Richlewis
    Jul 8, 2020 at 10:36
5

I don't know any way to do this directly in nano. However you can use "cat" or "grep" to display lines of your file in the console.

If you use a terminal multiplexer like "screen" you can copy and paste strings like this.

1
  • This is what worked for me. cat the file to the terminal, then highlight the text and use shell to copy and paste. (Shift Ctrl C/Shift Ctl V)
    – Chanoch
    Oct 16, 2018 at 10:37
3

The following works in Nano but also anywhere in a terminal:

  • Copy text from a terminal, after selecting with your mouse: Ctrl + shift + C.

  • And to past text in to a terminal: Ctrl + shift + V.

1
  • I commented too early. It did work great, only without starting nano with mouse enabled, that is: nano -m didn't work, nano alone did.
    – Andyc
    Apr 10 at 13:26
2

Simply use Ctrl+Shift+6 to copy current line or you can set mark using Ctrl+6 and copy multiple lines using above command as well.

2

Relatively straightforward solution:

  1. From the first character you want to copy, hold Shift down and go all the way to the end.

  2. Press Ctrl+K, which cuts the text from the file.

  3. Press Ctrl+X, and then N to not save any changes.

  4. Paste the cut text anywhere you want.

Alternatively, if your text fits into the screen, you can simply use mouse to select and it automatically copies it to clipboard.

1
  • It should. What happens when you do it?
    – Jorjani
    Oct 30, 2018 at 11:48
2

The copy buffer can't be accessed outside of nano, and nowhere I found any buffer file to read.

Here is a dirty alternative when in full NOX: Printing a given file line in the bash history.

So the given line is available as a command with the UP key.

sed "LINEq;d" FILENAME >> ~/.bash_history

Example:

sed "342q;d" doc.txt >> ~/.bash_history

Then to reload the history into the current session:

history -n

Or to make history reloading automatic at new prompts, paste this in .bash_profile:

PROMPT_COMMAND='history -n ; $PROMPT_COMMAND'

Note for AZERTY keyboards and very probably others layouts that require SHIFT for printing numbers from the top keys.

To toggle nano text selection (Mark Set/Unset) the shortcut is:

CTRL + SHIFT + 2

Or

ALT + a

You can then select the text with the arrows keys.

All of the others shortcuts works fine as the documentation:

CTRL + k or F9 to cut.

CTRL + u or F10 to paste.

1

Select the text in nano with the mouse and then right click on the mouse. Text is now copied to your clipboard. If it does not work try to start nano with the mouse option on : nano -m filename

1

First method

This method seems to work when the content doesn't include ●.

Install xsel or similar and assign a global shortcut key for this command in your WM or DE:

xsel -o | sed -r 's/^ ?[[:digit:]]+($| +)//g' | perl -pe 's/\n/●/g' | sed -r 's/●●/\n\n/g; s/ ?● {1,}/ /g; s/●/\n/g' | xsel -b

Put this in your ~/.Xresources:

*selectToClipboard: false

Issue this in your xterm once to activate the above option:

xrdb -load ~/.Xresources

Now select the line(s) including the line numbers by pressing Shift while dragging the mouse. After the selection click your key combo; the line(s) are coppied and ready to be pasted anywhere you like.

Second method

Doesn't have the shortcoming of the first method.

Install xdotool and xsel or similar.

Put these two lines

Ctrl <Btn3Down>: select-start(PRIMARY, CLIPBOARD)
Ctrl <Btn3Up>: select-end(CLIPBOARD, PRIMARY)

in your ~/.Xresources like so:

*VT100*translations: #override \n\
    Alt <Key> 0xf6: exec-formatted("xdg-open '%t'", PRIMARY, CUT_BUFFER0) \n\                           
    Ctrl <Key>0x2bb: copy-selection(CLIPBOARD) \n\                              
    Alt <Key>0x2bb: insert-selection(CLIPBOARD) \n\                             
    Ctrl <Key> +:       larger-vt-font() \n\                                    
    Ctrl <Key> -:       smaller-vt-font() \n\
    Ctrl <Btn3Down>: select-start(PRIMARY, CLIPBOARD) \n\
    Ctrl <Btn3Up>: select-end(CLIPBOARD, PRIMARY) 

Issue this in your xterm once to activate the above option:

xrdb -load ~/.Xresources

Create this scrip in your path:

#!/bin/bash                                                                  

filepid=$(xdotool getwindowpid $(xdotool getactivewindow))                   
file=$(ps -p "$filepid" o cmd | grep -o --color=never "/.*")                   

firstline=$(xsel -b)                                                         
lastline=$(xsel)                                                             

sed -n ""$firstline","$lastline"p" "$file" | xsel -b

Assign a global shortcut key to call this script in your WM or DE.

Now when you want to copy a line (paragraph), select only the line number of that line (paragraph) by right mouse button while pressing Shift+Ctrl. After the selection click your custom global key combo you've created before. The line (paragraph) is coppied and ready to be pasted anywhere you like.

If you want to copy multiple lines, do the above for the first line and then for the last line of the range, instead of Shift+Ctrl+Btn3 (right mouse button), just select the number by left mouse button while pressing only Shift. After this, again call the script by your custom global shortcut. The range of lines are coppied and ready to pasted anywhere you like.

0

M-^ is copy Text. "M" in my environment is "Esc" key ! not "Ctrl"; so I use Esc + 6 to copy that.

[nano help] Escape-key sequences are notated with the Meta (M-) symbol and can be entered using either the Esc, Alt, or Meta key depending on your keyboard setup.

-1

1) Ctrl + 6 to mark the text that you want to copy

2) Ctrl + k to cut the text and Ctrl + u to paste back to the original place

3) Go to the desired line where you want to paste the code marked in step (2). Ctrl + u to paste it.

Hope it helps.

1
  • So its a two step process...We cut, restore, then copy. Great Feb 4, 2018 at 22:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.