Copy text from nano editor to shell

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.

Answers


Nano to Shell:

1. Using mouse to mark the text.

2. Right-Click the mouse in the Shell.

Within Nano:

1. CTRL+6 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.

or

1. CTRL+6 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.


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


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.


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.


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.


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.


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.


What I've written previously may be fine to copy a part of a single line, but not good for complete lines. For single or multiple complete lines:

Install fzy (or fzf or similar) and put this in your ~/.bashrc:

nc () {
      fil=$(ps aux | grep [n]ano | grep -oE '[^ ]+$' | rev | cut -d'/' -f 1 | rev | uniq | fzy)
      find ~ -type d -name .cache -prune -o -type f -name "$fil" -exec sed -n ""$@"p" {} + | xsel -b && pkill -f 40x15
}

And put this in your global shortcuts config file or utility:

xterm -geometry 40x15

Now you can call this small xterm window by your shortcut and enter

nc FIRST_LINE_NUMBER,LAST_LINE_NUMBER

and choose the file to copy those lines from. Paste them any where even in another nano (after disabling autoindent by M-I).

For a single line just enter

nc LINE_NUMBER

This may be useful only to copy a part of a long line:

You need fmt and xsel (or xclip or wl-clipboard or a similar) utility and put this command in your global keybindings file or utility:

xsel | fmt -c -w 1000 - | xsel

Then:

  1. Disable line numbering if it's enabled by M-N (Alt-N in my config)
  2. Disable mouse support by M-M (Alt-M in my config)
  3. Easily select the whole area to be copied by mouse just like in terminal or any other programme
  4. Click your custom keybinding for the command above
  5. Middle click anywhere you want the text to be pasted

This will preserve the original/intentional empty lines, but all the other lines will be joined.

This is for primary selection. If you prefer clipboard then replace xsel with xsel -b and before the 4th step you need to Ctrl-Shift-C and instead of the 5th, paste it by either Ctrl-V or Ctrl-Shift-V depending on where you want to paste.

Ed: Instead of the first 3 steps you may just press Shift and click on the begining of the text to be copied and release Shift but not release the mouse and drag mouse to the end of the text to be copied and release mouse. And follow the above steps for clipboard not primary selection.


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) 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.


Much easier method: $ cat my_file

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

Ctrl+Shift+v to paste it wherever you like


ctrl+shift+c to copy text, you are allocated. ctrl+shift+v to paste text


Need Your Help

How to check if mongo db is running on Mac?

macos bash mongodb installation osx-yosemite

I had installed MongoDB few days back on my Mac and am not sure how I installed, but now how do I check if MongoDB is up and running in my system?

Cross platform , C/C++ HTTP library with asynchronous capability

c++ c http asynchronous cross-platform

I'm looking for a C/C++ library that will work on Windows and Linux which will allow me to asychronously query multiple webservers (1000's per minute) for page headers and download web pages in muc...