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/
Related
I'm using gedit. I have multiple tabs open, some are simply text that I have copied from other tabs, and haven't been saved to any file.
Is there a way to compare text that is selected in one tab to text selected in another tab? I know that I can use meld, diffuse, diff, etc., but those (afaik) require the selections to be saved to files first.
I remember using an editor (maybe emacs?) many years ago that could do something similar. But now I almost exclusively use gedit for text file editing.
Any ideas anyone?
TIA
ken
Okay, got a pointer from google:
In gedit, I created a new external tool that will run diff with the clipboard contents against the currently selected text.
Here's the bash code for the tool:
#!/bin/bash
SEL=$(cat)
diff -B <(echo -en "$SEL") <(xclip -selection c -o)
Note that this requires xclip, which is in most linux's repos. Sorry, don't know about Windoze.
I encountered a problem with my terminal where when I paste text, it is prefixed by 00~ and suffixed with 01~.
For example, I will highlight text and push Command-C. I then push Command-V into the terminal and I see those weird characters pop up at the beginning and end of the text.
For example, I can highlight text and paste it into the terminal. I then see 00~text01~.
The text can be from anywhere, even from the Terminal itself. I do not have any copy/paste plugins installed, this is just the normal Copy/Paste. I am using the default Mac Terminal without any modifications.
I did some searching online, apparently the Paste wraps the text in special characters so that certain applications will see that this is pasted text and will handle it properly. However, the terminal is not handling this correctly, and is therefore not removing the weird characters. Apparently this paste mode is called the "Bracketed Paste Mode" http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Bracketed-Paste-Mode
I found another question that gave a solution on how to solve this issue on a linux machine, but after trying that solution I still have that same problem.
Can someone tell me how I can disable bracketed paste mode for the terminal? Or tell me the right way to get rid of these annoying characters?
What happens when text is pasted
Text has made it into the "system" (e.g. X, MacOS/Aqua) copy buffer from somewhere, maybe from the same terminal. The text is not altered here.
The text is pasted into the terminal; that is, "system" sees to that the terminal (e.g. xterm) receives the unaltered character sequence from the copy buffer. The terminal is aware that this is a paste, not keyboard input.
The terminal sends the char sequence in the buffer to the program running in the foreground (a shell, an editor, whatever). To the program the received data is indistinguishable from user input through the keyboard.
Discussion
This transparency (or opaqueness? whatever) is often a good thing much like the Unix paradigm of transparent pipe plumbing in general. But sometimes programs could deal with the data better if they knew it is pasted. For example an editor like vim could switch off auto indent — after all, the code is likely indented already!
Bracketed paste
Enter bracketed paste. For principal reasons the paradigm of transparent data piping cannot be altered; but the data can be decorated with sequences which would ordinarily not appear in terminal input to mark its start and end. If the terminal is so configured — for the xterm the configuration would be to send ESC [ ? 2 0 0 4 h — the pasted data is bracketed with escape sequences: ESC [ 2 0 0 ~ <buffer contents> ESC [ 2 0 1 ~.
The foreground program receives this "decorated" data, and it's up to to the program to handle it. A naive program treats all of it as user input, which is what you see.
A good discussion of bracketed paste can be found in this article.
Remedies
There are two issues in your case: The terminal ends up unexpectedly in bracketed paste mode; and the receiving program — presumably the shell — does not handle it.
One solution is user83536's: Identify the program which leaves the terminal in that state and call it through a wrapper which simply switches bracketed paste mode off again after the program has ended.
The program probably tried to switch bracketed paste mode off but failed. One reason can be that it sends the wrong escape sequence. Try setting the TERMINAL environment variable to the value best describing your terminal.
Try to switch off bracketed paste in the offending application. In vim one would say set t_BE=. That prevents vim from putting the terminal in bracketed paste mode and when it is set in a session, sends the "end bracketed paste mode" to the terminal.
Embrace bracketed paste. It seems to be a good idea. For the bash and other programs using readline one would put set enable-bracketed-paste on. For vim one could follow the suggestions here.
This may not apply directly to your problem, but I found this symptom to probably, in my case, be caused by my editor-of-choice 'mcedit' (Midnight Commander)
To alleviate the bug problem, I added the following function to my .bashrc file:
### vvv 'function mcedit' is a fix-up for the ~0/~1 paste problem
function mcedit() { command mcedit $# ; printf '\e[?2004l' ; }
Then 'source .bashrc'
Now every time I execute 'mcedit', it automatically adds the 'printf "\e[?2004l"' when I close out to reset the "Bracketed Paste Mode"
Works for me, YMMV.
To disable bracketed paste mode in your terminal, run the following command:
printf '\e[?2004l'
To disable bracketed paste globally, on Linux, add this line to ~/.inputrc :
set enable-bracketed-paste 0
To disable only in the current running Xterm (v 372) (running bash shell version 5.1.16 (probably earlier too, but I don't know)):
% bind 'set enable-bracketed-paste 0'
With either of the above two methods, you can re-enable bracketed paste (in the current Xterm) in the obvious way, namely:
% bind 'set enable-bracketed-paste 1'
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.
In bash, I can type Ctrl+u it "cuts" text behind my cursor.
Then when I press Ctrl+y it pastes the text back.
Is there a file where the cut text is stored?
The "clipboard" -- actually called the kill ring -- is implemented by the readline library. It is not persistent, so it is not saved in a file.
There are lots of keystroke commands for "kill"ing and "yank"ing text, which are described in the bash manual:
Killing text means to delete the text from the line, but to save it away for later use, usually by yanking (re-inserting) it back into the line. (‘Cut’ and ‘paste’ are more recent jargon for ‘kill’ and ‘yank’.)
More complete documentation is in this section of the manual.
xsel can copy and paste to three different "clipboards". By default, it uses the X Window System primary selection, which is basically whatever is currently in selection. The X Window System also has a secondary selection (which isn't used much), and a clipboard selection. You're probably looking for the clipboard selection, since that's what the desktop environment (e.g. Gnome, KDE, XFCE) uses for its clipboard. To use that with xsel:
xsel --clipboard < new-clipboard-contents.txt
xsel --clipboard > current-clipboard-contents.txt
or use this link
In bash, when I go back in history, edit some command and run it, this edited command is appended to history and the original one is left intact. But every once in a while I somehow manage to affect the original command, i.e. my edit replaces the original command back in history. I can't put my finger on how this happens.
Can someone explain? My goal is to avoid this, so any edit to a previous command always gets appended to history and never replaces the original.
I somehow manage to affect the original command, i.e. my edit replaces the original command back in history.
Right. If you go back in your history and edit the line without pressing return to execute the command but instead moving to another history entry, you've just edited the history entry. If you then list your history, you will see a * on the line indicating that you edited it. I find this "feature" immensely frustrating. Others have provided good examples of how to reproduce this.
My goal is to avoid this, so any edit to a previous command always gets appended to history and never replaces the original.
I too wanted to disable it. I found the solution via this answer over on unix.stackexchange.
To summarize, you need to enable the revert-all-at-newline readline setting which is off by default. If the setting is on then bash will revert any changes you made to your history when you execute the next command.
To enable this setting in your shell, you should add the following to your ~/.inputrc file and then restart your shell:
$include /etc/inputrc
set revert-all-at-newline on
The first line is needed because I guess that if you supply your own .inputrc file the default /etc/inputrc file is not included which is probably not what you want.
If you go back to some previous command and edit it, but then DON'T execute it (instead using history commands to go to some other command and execute it), then the edits will remain there in your history list.
Pressing Ctrl + C, after editing, counters this behaviour. It leaves the original command in tact i.e. it cancels remembering edits to the original.
Here's my own answer, please correct or provide more details if you can.
When the "vi" option is set in bash ("set -o vi" -- "Use a vi-style command line editing interface"), there are two modes of editing a command from history.
The first mode (let's call it "basic") is when you start editing immediately using Backspace, Del and character keys.
The other mode is the "vi mode", entered when you hit Esc.
If you want to keep your history intact, DO NOT use both modes in the same edit. I don't know how bash works exactly, but you can think of it this way:
Entering the "vi mode" applies any changes done in "basic mode" to the original command, and creates a copy of the command that you can edit further using vi-style commands.
The changes get applied when you hit Enter (execute), Up, Down or j,k (move to another command in history).
The changes do not get applied if you hit Ctrl-C.
Using either basic or vi-style editing ALONE does not affect the original command in history.
What do
echo $HISTCONTROL
echo $HISTIGNORE
give you?
Edit:
I was able to reproduce behavior similar to what you've seen by following these steps:
At the shell prompt, enter:
echo abcd
echo efgh
Press up arrow twice, so "echo abcd" is shown
Press 1 to add that character at the end
Press escape to enter command mode
Press left arrow twice so the cursor is on the "c"
Press x to delete the "c"
Press enter
Now as you step back through history, you'll see a new entry at the end:
echo abd1
and the entry that previously had "echo abcd" will now look like this:
echo abcd1
This is one way, I'm sure there are others.