Copy text from nano editor to shell [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
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.

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.

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

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:
From the first character you want to copy, hold Shift down and go all the way to the end.
Press Ctrl+K, which cuts the text from the file.
Press Ctrl+X, and then N to not save any changes.
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.

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.

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.

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

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.

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

Related

PhpStorm - Mark multiple lines

Is it possible to mark multiple lines in PhpStorm?
e.g.
LineA
LineB
LineC
lets say I try to mark LineA and LineC to copy paste them to another place, is this possible?
LineA
LineC
Sure .. but it requires a bit of "precision pointing" and using mouse (not sure if it's easily doable with keyboard only).
Basically -- the idea is to use Multiple Carets functionality:
Place caret at the beginning of first line
Now Alt + Click on the beginning of the other line to create new caret there (shortcut depends on OS -- on Linux it might be different shortcut/combo as that shortcut may used by Desktop/Window manager for own stuff)
Now you have 2 carets on the beginning of Line A and Line C
Just use Shift + End to select text to the end of line -- it will be done in both lines.
Now just Copy + Paste it elsewhere (or whatever you wanted to do).
Use Esc to get rid of carets.
Use multiple cursors. The keyboard shortcuts may vary (those described in the documentation do not work for me on Linux).
Explore clicking around the text while various combinations of the Ctrl, Alt and Shift keys are pressed. On macOS also try Cmd. One of them allows you to place multiple carets in the text (f.e. at the beginning of the lines you want to copy).
After you placed all the carets you need use Shift and the arrows to extend the selection. Use Copy and Paste as usual. Use Escape or click anywhere in the text to deselect and go back to the regular, single-caret, status.

^A and ^B separated values

I have a file that has contents that are separated by ^B and ^A values. This file has an extension .tsv but the values are separated by ^A and ^B. I want to add more lines to this file with the same delimited values but I'm not sure what the values of ^A and ^B are.
I read around and I think ^A can also be represented as \001
I'm not sure how to recreate the balues ^A and ^B. I tried entering contrl + A and control + B but I dont get the same outputs. Also tried \001 but I still cant seem to recreate it.
^A and ^B are caret notation for the ASCII control characters 1 and 2 respectively.
There is no universal way of typing these. Here are some options:
Bash:
echo $'foo\001bar\002baz' >> file
This appends "foo^Abar^Bbaz" to the file.
Vim: Ctrl+V Ctrl+A
In insert mode, this inserts a ^A (and similarly for B)
Emacs: Ctrl+Q Ctrl+A
This inserts a ^A (and similarly for B)
You can also simply copy-paste these characters in any graphical editor. However, if you use a terminal based text editor, you have to use the editor's keyboard controls instead of the mouse to copy/paste them.
You can use the "tr" command to make a temporary file, edit it and then return it to the original. Requires you to have two characters not in the original data (~ and % in this example).
First translate to get rid of the offending characters:
tr "\001\002" "~%" < original-File > temp-file
Edit "temp-file" as you need to.
Translate back to the original:
tr "~%" "\001\002" < temp-file > modified-file
You can check the original and check your work with:
hexdump -C file

Copy specific line from less

How to copy a specific line from less ? Lets say I am opening a man ( which is by default opened by less ) and want to select and copy it to clipboard and after that lets say paste it to file opened in vim ? I don't want to use the mouse wheel to paste. I am looking for a simple Ctrl-c , Ctrl-v method as in windows.
When opening a man page I can't switch to my default editor (which is vim ) with 'v' key because less shouts with "Cannot edit standard input" error.
Thanks a lot and sorry if this question is silly.
tl;dr, use m and |.
Example:
Within the man page of less, by running man less:
7g
mx
6g
|x
xclip (Linux) or pbcopy (macOS), to copy to clipboard.
cat > file, to save to file, or cat >> file for append mode.
We would get:
less - opposite of more
 
The key things to learn are just two less commands: m (mark), and | (pipe).
Command m (mark)
Followed by any lowercase letter, marks the current position with that letter.
The marker we used above is x, as in step 2, it marked line 7 with x.
Command | (pipe)
| <m> shell-command
<m> represents any mark letter. Pipes a section of the input file to the given shell command.
The section of the file to be piped is between the first line on the current screen and the position marked by the letter.
<m> may also be ^ or $ to indicate beginning or end of file respectively. If <m> is . or <newline>, the current screen is piped.
Using |xpbcopy, we pipe the line-range [7, 6] into pbcopy, as line 6 is currently the first line on the screen, and line 7 is the one we marked as x, and pbcopy is the command to put text into the macOS clipboard.
Alternatively, use xclip on Linux, or even dd of=/path/to/file to save as a file.
Note
The text range is boundary inclusive, so both the beginning and the ending lines of the range, or at least 2 lines are copied.
We marked the range in the backward way, namely from bottom to top, otherwise, less might behave awkwardly, and throw the whole screen through the pipe.
I think I found the solution: it is using tmux. Tmux provides it's own clipboard ( correct me if I am wrong ). From tmux I can enter the copy-mode wherever I am ( in MAN pages, less, console output ) and let me to copy the content.
The current accepted answer is based on setting a mark, navigating one line up, piping the current screen up to that mark into the clipboard.
Copy 1 line into clipboard
Navigate to the line (using 1g to go the first line)
|
Press RETURN
head -1 | clip (or xclip et. al)
head -1 | tr '\n' '\' | clip
Explanation
| pipes the whole screen into the command, so navigate to where we want that to start.
2-3. | <m> shell-command ​if <m> is . or newline, the current screen is piped.
head -1 just the first line
| tr '\n' '\' replace the the carriage return with \ (for pasting to shell)
| clip pipe to clipboard (or /dev/clipboard, xclip et. al)
Short answer: Ctrl+C and Ctrl+V are associated with other actions. For instance Ctrl+C sends an interrupt signal to the foreground process. Usually you need to use Ctrl+Shift+C and Ctrl+Shift+V in order to copy and paste from a terminal.
Long answer: This very good thread from superuser.

How to copy the GNU Screen copy buffer to the clipboard? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
When using GNU Screen we can work with scrollback buffer also known as "copy mode" using the Ctrl+a+[ command.
In there we can copy text to the copy buffer by pressing space selecting the text and pressing space again.
Is there some way to copy this text from screen copy buffer to the X clipboard?
In my case I'm using Ubuntu 12.04 with gnome and Xorg.
You can use a CLI clipboard tool like xsel or pbpaste and the cat utility to grab contents from STDIN. The steps on Linux with xsel are as follows:
Copy text from your screen session into GNU screen's copy buffer.
Run this command within screen: cat | xsel -b
If xsel didn't report any error, now dump screen's copy buffer to STDIN: Ctrl+a+]
Send an EOF to cat to terminate it: Ctrl+d
At this point, the contents of the screen copy buffer should be in your clipboard.
EDIT: As with all X programs, xsel needs to know how to contact your X server in order to access the clipboard. You should have your DISPLAY environment variable set appropriately.
This answer works for only a scenario where your end target is to paste the copied buffer contents immediately.
The simplest way to do this is by splitting your screen into two regions. You can do this by hitting CTRL+a then |'This is not an i. It is the PIPE sign on your keyboard'
Hit CTRL+a then TAB to switch to the second region, CTRL+a then c to create a new session in the second region.
If you want to copy from nano and paste in terminal, open up the file in nano on the left region, hit CTRL+a then ESC, scroll to the start point of your copy location and hit SPACE, select the text by scrolling to the end point and hit SPACE again to mark copy.
Now, all you have to do is hit CTRL+a then TAB to switch to the region on your right and hit CTRL+a then ].
Your text will be written out to the command line. Note that you can also check for hardcopy option if you want to write directly to file.
There is a simpler and less manual way to do this. In your screen .rc file, add the following line:
bindkey -m ' ' eval 'stuff \040' 'writebuf' 'exec sh -c "/usr/bin/pbcopy < /tmp/screen-exchange"'
How to use the copy functionality:
screen -c path/to/screen/config.rc
Hit Ctrl+A then Esc to enter copy mode.
Scroll up the text buffer and find the spot you want to leave your start marker for copying, then hit space.
Scroll down and select the text you wish to copy. When you are done, hit space again.
The text will now be in your clipboard.
EDIT:
On Linux with no pbcopy but with clipit, you can use as below:
bindkey -m ' ' eval 'stuff \040' 'writebuf' 'exec sh -c "/bin/cat /tmp/screen-exchange | /bin/clipit"'
This answer applies to OS X.
After copying the desired text into the GNU Screen paste buffer using copy mode, do the following:
In any of your screen windows, type pbcopy <enter>.
Then paste your text into the terminal using the GNU Screen paste command (Ctrl-a ] unless you've changed your escape key).
If the text does not end in a newline, press <enter> to insert one.
Finally, press Ctrl-d to cause pbcopy to push the text to the system clipboard.
Then you can paste the text elsewhere in OS X as usual using Command-v or an equivalent menu option.
Since nobody seems to have directly answered the question:
Once you have copied the output you want into your buffer you need to
Open a text editor with a new file i.e. vim somefile.txt
Go into edit mode i.e. i in vim
Press Ctrl + a then ] which will dump the contents of the buffer you just filled into the text editor
ta-da!
Exit your ssh terminal session, if you are currently connected to a server.
If you are using XQuartz on Mac OS and xsel on the server. You should update the XQuartz pasteboard settings by selecting Preferences in the xQuartz application menu.
XQuartz settings:
ssh into the remote machine and try run:
xsel -p <<<"THIS IS A TEST".
Press cmd + v and "THIS IS A TEST" should be output.
I wanted a way to do this programmatically similarly to #kungfuspider and tweaked their solution to work for Ubuntu WSL running on Windows.
Setup:
Download win32yank executable and place win32yank.exe somewhere useful (I created a symbolic link to it in /usr/bin with ln -s <path to exe> /usr/bin/win32yank)
Place the following into ~/.screenrc (from #kungfuspider). You might need to modify the command to point to the correct cat and win32yank locations.
bindkey -m ' ' eval 'stuff \040' 'writebuf' 'exec sh -c "cat /tmp/screen-exchange | win32yank -i --crlf"'
Reload ~/.screenrc without killing your session by executing CTRL+a : source ~/.screenrc
How To Use:
Enter copy mode with CTRL+a [
Move around with vim style key movement or arrow keys
Start selecting text by hitting space
Highlight desired text and finish copy by hitting space again, text should now be in your Windows clipboard and can be pasted back to Ubuntu with a right-click.
Note: It is very important to finish copy with a space because that's what the bindkey command is using to map win32yank
If it's just a little bit of info that you want to copy just highlight it with your mouse and then paste it where you want.
If you're trying to get a lot of info the screen session can be logged to a file and then you can copy from the file or clean it up a bit and use it for instructions on doing things
Finally today I found a solution with mouse:
Hold down Ctrl and right click with mouse.
Copy/paste context menu shows up.
Some screens at https://michalzuber.wordpress.com/2015/01/28/gnu-screen-copy-paste-with-mouse/

Hotkey for copying text as you type

When I'm programming I often use copy and paste, which requires me to grab the mouse, highlight the text, and press CTRL + C (I'm using Windows BTW). Is there any hotkey or solution out there for a keyboard short cut that I could press and have it start copying each character I type? It would be such a godsend to get rid of that whole highlighting mess!
You can probably develop a quick application that does that in VB (via global hotkey listener). But for me, I am a keyboard shortcut nerd, and I use CTRL + Shift + ← /→ to navigate the caret through "words" (sometimes that means every non-alphanumeric token, but it varies on the IDE you're using) that I want to copy--It's much faster than Shift + ← /→. I also use this combination frequently: End, Shift + Home, CTRL + C, which is a quick way to select and copy the whole line.
I can see where you're coming from with the "toggling copy", but I think using the full extent of the keyboard, while primitive, is the best and most practical.
Shift + ← and Shift + → will select text forward and backup.
Most editors have a lot more (Ctrl - Shift + ← for selecting to next word boundary for instance) but depends on the application.
Depending on how long your code is, you can always do Shift + End, then CTRL + C to copy the line, providing that you're at the end of the line. Shift + Home would select the preceding line if you're at the beginning of the line, etc.

Resources