146

I need to copy some text in Nano, so I searched using Google and found a few posts here on Stack Exchange and other sites in which the OP asks "How can I copy?" and people usually answer "You can use ctrl+K". The person asks how to copy and they are told how to cut.

Other answers mention alt+6 but in my testing it doesn't work.

Is there no option to copy text in Nano? Is there a workaround?

Anwar
  • 76,649

8 Answers8

186
  1. Position the cursor at the beginning of the character from which you want to copy. Press Alt+Shift+A to set mark. ( Alternatively, set mark with Ctrl+6 )

  2. Use arrow keys to highlight the text to copy. highlight with nano image

  3. Use Alt+Shift+6 to copy (Alternatively, Alt + 6 )

  4. Navigate to the place you want to paste. Release paste with Ctrl+U

    paste image

Where is reference to these keybindings

All of these are defined under the documentation, which you can access with Ctrl+G at any point. Note that leading ^ stands for Ctrl and M for Alt in the documentation, since this is traditional way of Unix defining meta characters. Lines from documentation:

^^ (M-A) Mark text starting from the cursor position

and

M-^ (M-6) Copy the current line and store it in the cutbuffer

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • Amazing. But how do you do "alt" on an apple macintosh? – Darth Egregious Oct 05 '16 at 17:31
  • 5
    @user973810 Last time I checked, Apple laptops and desktops all have Alt key. If you're talking about Macintosh from way way back in the day from 1998 or so, well . . . I'm not familiar with those so can help you. Probably learn VI editor - it's far better than nano – Sergiy Kolodyazhnyy Oct 05 '16 at 17:40
  • No. You're wrong. There's function, control, option, and command. No "alt". – Darth Egregious Oct 06 '16 at 01:06
  • 5
    Look at the image Option on Macs acts as Atl key – Sergiy Kolodyazhnyy Oct 06 '16 at 01:12
  • 6
    @user973810 if you look closely at the option key, you may notice that it also says alt on it. http://nedbatchelder.com/pix/mackeyboard.jpg – TheWanderer Oct 06 '16 at 01:13
  • Even the iMac G3 (1999) has the double label http://www.vectronicsappleworld.com/archives/vintage/images/0012/snap38.jpg. – TheWanderer Oct 06 '16 at 01:16
  • @Serg Here is a direct link: https://images-na.ssl-images-amazon.com/images/I/61sOBcXjbXL.SL1000.jpg – EKons Oct 06 '16 at 08:56
  • 1
    I'm using 14.04 and when use ALT-SHIFT-6 I get "uknown command" and I'm still in marking mode (which I have to abort using ALT-SHIFT-6 again). – Tulains Córdova Oct 06 '16 at 13:07
  • @TulainsCórdova yes, that very same thing happened on my 16.04 system. I think that in the process i hit key combination that nano doesn't recognize. If the steps followed carefully and each key properly pressed, the "unknown command" doesn't show up – Sergiy Kolodyazhnyy Oct 07 '16 at 07:45
  • ctrl-6 to start marked area; arrow keys to extend marked area; alt-6 to copy or ctrl-k to cut; ctrl-u to paste. No shift key. Alternatively, without starting a mark, you can use alt-6 or ctrl-k (repeatedly if you want) to copy or cut one (or more) lines, followed by ctrl-u to paste them. – jrw32982 Oct 08 '16 at 00:42
  • @JohnWiersba that can be done as well. I got used to using Alt+Shift+A and Alt+Shift+6 since that's what it says on documentation : ^^ (M-A) Mark text starting from the cursor position . As for Ctrl+K, that's covered by Zanna's answer – Sergiy Kolodyazhnyy Oct 08 '16 at 00:51
  • @JohnWiersba I've updated the answer to include the alternative keybindings and quote from documentation – Sergiy Kolodyazhnyy Oct 08 '16 at 00:58
  • 1
    All I can read in this answer is "It's time to learn vim" – Nearoo May 14 '20 at 08:43
  • @Nearoo Learning vim is always a good idea - very powerful and commonly available editor :) I'd recommend Shawn Biddle's training class on YouTube. That's where I learned the basics and it grew on me. I've abandoned nano soon afterwards – Sergiy Kolodyazhnyy May 14 '20 at 10:28
  • @SergiyKolodyazhnyy, the short cut key (Alt + 6) is an alternative key. The main shortcut key is M-^, which means Alt + Ctrl. For almost all other options, main key itself will work but for copying this combination doesn't work. Do you have any idea why? – learner Jun 04 '20 at 03:13
  • 1
    @SergiyKolodyazhnyy, Sorry, Now I realized the character "^" is the character on keyboard and not a notation of Ctrl key. Pressing Alt + Shift + 6 will do it. – learner Jun 04 '20 at 03:28
62

Workaround: cut, then uncut, and uncut again in the place where you want to copy the text.

unicorns
rainbows

line4

I Ctrl+K unicorns

rainbows

line4

I uncut Ctrl+U

unicorns
rainbows

line4

I move the cursor to the end and Ctrl+U again

unicorns
rainbows

line4
unicorns
Anwar
  • 76,649
Zanna
  • 70,465
9

Yes copying text is a bit strange in nano, and somewhat confusing due to conflicting reports on the webs and lack of clarity (or common sense...) both in the way it was implemented (IMHO) and in the official docs...

Per the docs:

4.3 The Cutbuffer

Text can be cut from a file, a whole line at a time, by using the ’Cut Text’ command (default key binding: ^K). The cut line is stored in the cutbuffer. Consecutive strokes of ^K will add each cut line to this buffer, but a ^K after any other keystroke will overwrite the entire cutbuffer.

The contents of the cutbuffer can be pasted back into the file with the ’Uncut Text’ command (default key binding: ^U).

A line of text can be copied into the cutbuffer (without cutting it) with the ’Copy Text’ command (default key binding: M-6).

Since basically, you use CTRL+K to cut, CTRL+U to paste, and the cut line stays in the cutbuffer, you can do a quick cut and paste to replace, and then paste again to achieve a "copy."

But I don't like this "workaround"...

The last part of the docs, says how to copy: using ALT+6 (to copy) ad then pasting using CTRL+U to paste.

(Docs actually say "M" key, which means the Meta key, as mook765 said: "the Meta-key which does not exist"... and is usually just the ALT on modern keyboards.)

You can also select multiple lines using ALT+6 and moving the section with your arrow keys.

Hope this helps.

5

Note that this solution only works in GUI versions of Ubuntu and not in a TTY interface.

I copy text in nano all the time just by doing CTRL+SHIFT+C on the selected text because that's the default key combo to copy any text in terminal. This applies to nano as well. This can also be found in

Edit > Copy

TheOdd
  • 3,012
  • 7
    Good workaround, but this won't work in a TTY, only in GUI terminals :) nano however has it's own way of copying already, that doesn't depend on terminal – Sergiy Kolodyazhnyy Oct 04 '16 at 21:56
3

you can also do the following;

  1. nano -cm <text_to_edit>
  2. Place the cursor where you want to start copying,
  3. Press Shift + LeftClick and drag mouse throuh the text you want to copy, press Ctrl+Shift+C
  4. Place the cursor you want to paste the text,
  5. Press Ctrl+Shift+V

The options -c and -m elaborated in man nano:

-c, --constantshow
    Constantly show the cursor position on the status bar.  Note that this over‐
    rides option

-m, --mouse
    Enable  mouse  support,  if  available for your system.  When enabled, mouse
    clicks can be used to place the cursor, set the mark (with a double  click),
    and  execute  shortcuts.  The mouse will work in the X Window System, and on
    the console when gpm is running.  Text can still be selected  through  drag‐
    ging by holding down the Shift key.
Abel Tom
  • 641
3

mac pro m1: M=ESC

  1. Position the cursor.
  2. ESC+A: Mark text at the cursor position.
  3. Use arrow keys to highlight the text.
  4. ESC+6: Copy.
  5. Position the cursor.
  6. Ctrl+U: Paste.
3

From man nano

When a more precise piece of text needs to be cut or  copied,  one  can
mark  its  start  with  ^6, move the cursor to its end (the marked text
will be highlighted), and then use ^K to cut it, or M-6 to copy  it  to
the  cutbuffer.

M means the Meta-key which does not exist on all keyboards but on some. On keyboards wihtout the Meta-key other modifier-keys like Alt or Esc might work. On my machine i found that the combination Alt+6 works fine.

mook765
  • 15,925
  • The M key depends also on the OS you use. In case of Solaris 11.3 the meta key is Super Key (often with four squares) if presented on the keyboard. – Ali Muhammed Dec 24 '23 at 10:58
1

The easiest way that I find to copy text in nano is to select the text that you want to copy, with your mouse, and right click it. This will bring up a menu where it gives you an option of a few things one of witch is copy, select that option and you have copied the text. When you would like to paste the text do the same thing but select paste instead and it will paste the text.