Cua-copy/paste and System clipboard in emacs - emacs

I've set this in emacs:
(setq x-select-enable-clipboard t)
And it works just fine.
That is, until I copy something from within emacs using cua-copy (bound to C-c).
Then, whenever I try to copy something from other programs and paste it into emacs, it keeps pasting the same entry it last copied using cua-copy.
The clipboard itself still works - the same entry is pasted regularly in any other program.
So far I tried defining several things but to no avail:
(setq x-select-enable-primary nil)
(setq x-select-enable-clipboard t)
(setq select-active-regions t)
(global-set-key [mouse-2] 'mouse-yank-primary)
(setq yank-pop-change-selection t)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
Only when I forcefully paste from the clipboard using the command x-clipboard-yank does it resolve to its proper state, until I use cua-copy again.
Did anyone Ever encountered such a problem, or have any idea how to solve it?

I don't know enough about Emacs to offer any insights, but I can share what works for me (on Ubuntu) since i also have copy/paste bound to C-c/C-v with this in my ~/.emacs:
(cua-mode t)
And per this post: Integrate Emacs copy/paste with System copy/paste I have copying and pasting to/from Emacs without the problem you describe with these lines in ~/.emacs:
(setq x-select-enable-clipboard t)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
I see you already tried the two lines above, but did you try them alone without the other lines which might be conflicting with them?

This Answer doesn't work for Emacs 24. Adding these lines to my .emacs is what worked for me:
(setq select-active-regions nil)
(setq mouse-drag-copy-region t)

Related

emacs terminal mode: how to copy and paste efficiently

I'm having a hard time making this emacs -nw work effectively under the terminal mode (emacs -nw).
Some setup information:
The working server is connected via SSH, and emacs is running on the server. Usually I'm connecting using SSH and "emacs -nw" to work on my files.
The emacs config is picked up from: https://hugoheden.wordpress.com/2009/03/08/copypaste-with-emacs-in-terminal/
;; make mouse selection to be emacs region marking
(require 'mouse)
(xterm-mouse-mode t)
(defun track-mouse (e))
(setq mouse-sel-mode t)
;; enable clipboard in emacs
(setq x-select-enable-clipboard t)
;; enable copy/paste between emacs and other apps (terminal version of emacs)
(unless window-system
(when (getenv "DISPLAY")
;; Callback for when user cuts
(defun xsel-cut-function (text &optional push)
;; Insert text to temp-buffer, and "send" content to xsel stdin
(with-temp-buffer
(insert text)
;; I prefer using the "clipboard" selection (the one the
;; typically is used by c-c/c-v) before the primary selection
;; (that uses mouse-select/middle-button-click)
(call-process-region (point-min) (point-max) "xsel" nil 0 nil "--clipboard" "--input")))
;; Call back for when user pastes
(defun xsel-paste-function()
;; Find out what is current selection by xsel. If it is different
;; from the top of the kill-ring (car kill-ring), then return
;; it. Else, nil is returned, so whatever is in the top of the
;; kill-ring will be used.
(let ((xsel-output (shell-command-to-string "xsel --clipboard --output")))
(unless (string= (car kill-ring) xsel-output)
xsel-output )))
;; Attach callbacks to hooks
(setq interprogram-cut-function 'xsel-cut-function)
(setq interprogram-paste-function 'xsel-paste-function)
;; Idea from
;; http://shreevatsa.wordpress.com/2006/10/22/emacs-copypaste-and-x/
;; http://www.mail-archive.com/help-gnu-emacs#gnu.org/msg03577.html
))
The reason to have:
(require 'mouse)
(xterm-mouse-mode t)
(defun track-mouse (e))
(setq mouse-sel-mode t)
is to enable mouse selection over text such that the text region is highlighted just as "C-x SPC" marking the region. Then I can use "M-x w" to copy and "C-x y" to paste text within emacs and between emacs and other apps.
All look perfect except that any operations related to X are REALLY SLOW! My connection to the remote server is smooth -- the latency is usually under 100ms. But to kill one line of text using "C-x k", it takes ~5 seconds! To paste it, it takes another 5 seconds!
When copy/paste is frequent sometimes, this becomes really annoying. I think this is related to the X sever messaging, but not sure if there is good way to fix this.
Any ideas?
Thanks!
This is not an ideal solution per se, but i figured out a way that I feel better than the previous one.
The idea is to get rid of X which causes heavy latency issues, i.e. keep only the following:
;; enable clipboard in emacs
(setq x-select-enable-clipboard t)
The results are:
copy/paste within Emacs is straightforward and fast.
copy from other apps to Emacs: Ctrl+Shift+v
copy from Emacs to other apps: mouse selection is now on X Selection, so right-click and copy shall copy the text into the Selection. Note that 'M-w" now won't copy anything into Selection or system clipboard.
This is again a compromise rather than a solution, but considering the fact that i copy/paste more often than inter-app operations, this is acceptable at the moment.
Still looking forward to a good solution!
You can accomplish this by using a terminal escape code!
There is a unique category of terminal escape codes called "Operating System Controls" (OSC) and one of these sequences (\033]52) is meant for interacting with the system clipboard. The great thing is that your terminal doesn't care where the code came from so it will work in remote sessions as well.
Most terminal emulators support it (iTerm2, OS X Terminal, and I think all Linux terminals besides GNOME). You can test if your terminal supports this sequence by simply running:
$ printf "\033]52;c;$(printf "Hello, world" | base64)\a"
Then paste from your system clipboard. If it pastes "Hello, world" then your terminal supports it!
I have this function in my init.el so when I call yank-to-clipboard Emacs will yank the value from my kill ring into the system clipboard:
(defun yank-to-clipboard ()
"Use ANSI OSC 52 escape sequence to attempt clipboard copy"
(interactive)
(send-string-to-terminal
(format "\033]52;c;%s\a"
(base64-encode-string
(encode-coding-string
(substring-no-properties
(nth 0 kill-ring)) 'utf-8) t))))
As I type this, I stumbled upon an almost-identical script supported by Chromium community: https://chromium.googlesource.com/apps/libapps/+/master/hterm/etc/osc52.el
For those running Emacs inside Tmux:
Tmux consumes the sequence, so you'll need to pipe the sequence to the Tmux active tty for this to work. I have a solution in my blog post here: https://justinchips.medium.com/have-vim-emacs-tmux-use-system-clipboard-4c9d901eef40
To extend on #justinokamoto's answer for use in tmux, it works great and is truly amazing. I haven't debugged it with e.g. tramp or other fancy emacs settings but to get it to work
Follow https://sunaku.github.io/tmux-yank-osc52.html great instructions, modifying your tmux.conf and ~/bin/yank
Make sure terminal access to your clipboard is enabled on your terminal
Then to pull into emacs you can use a function like:
(Caveat emptor, I am very new to elisp. This writes to a temporary file in /tmp/yank)
(defun custom-terminal-yank (&rest args)
(message "-> CLIP")
;; FOR EVIL MODE: UNCOMMENT SO FIRST YANKS TO KILL RING
;; need to yank first, with all those args
;; ;; https://emacs.stackexchange.com/questions/19215/how-to-write-a-transparent-pass-through-function-wrapper
;; (interactive (advice-eval-interactive-spec
;; (cadr (interactive-form #'evil-yank))))
;; (apply #'evil-yank args)
;; https://stackoverflow.com/questions/27764059/emacs-terminal-mode-how-to-copy-and-paste-efficiently
;; https://sunaku.github.io/tmux-yank-osc52.html
(f-write-text (nth 0 kill-ring) 'utf-8 "/tmp/yank")
(send-string-to-terminal (shell-command-to-string "~/bin/yank /tmp/yank"))
)
If anyone else uses evil mode as well (just to make things complicated) you can uncomment those lines and use something like
(define-key evil-visual-state-map "Y" 'jonah-terminal-yank)
So that normal "y" is for normal yanking in visual mode, but "Y" is for cross-clipboard yanking

Emacs, use Delete key to delete selection

First of all, this is not a dup of
how to delete region with [delete] key in emacs, which is for Emacs 23, and the answer is Emacs cua-mode.
How configure delete-selection-mode to only delete?, which is for Emacs 22, and the issue is not reproducable.
I'm talking about the Emacs Delete Selection Mode, I used to be able to delete selection of text by just pressing Delete key. I found my proof here.
In order to set emacs to delete the selected text when you press DEL, Ctrl-d, or Backspace, add the following to your .emacs file:
(delete-selection-mode t)
However, now, with my Emacs 24, the Delete key doesn't work on the selection anymore (only Backspace does). This is really annoying because for all editors that I use, pressing the Delete key will delete the text selection. I don't understand why Emacs has to be so different and inconvenient. Anyway,
Any easy way to fix it? I don't know whether the comment of the following was a joke or the actual solution:
(setq behave-like-something-actually-usable-by-humans t)
Thanks
PS, my Emacs 24 is from nightly build:
$ emacs --version | head -1
GNU Emacs 24.3.50.1
EDIT: The answer from Drew, if it is unclear to you (I got that on third attempt),
(global-set-key (kbd "<delete>") '(lambda (n) (interactive "p") (if (use-region-p) (delete-region (region-beginning) (region-end)) (delete-char n))))
works for me.
The <delete> key is bound by default to kill-line. It sounds like you want to bind it instead to delete-region. Just do it.
(global-set-key (kbd "<delete>") 'delete-region)
There appears to have been a change to the behavior of delete-char. I experienced that as well as I had bound DEL to that command. I had success binding the key to delete-forward-char instead, which implements the old behavior. See here.
In 21.4.22, I had to use the (region-active-p) function instead of (use-region-p)
(global-set-key `backspace '(lambda (n) (interactive "p") (if (region-active-p) (delete-region (region-beginning) (region-end)) (delete-backward-char n))))

Keep getting mode name when switching buffers in Emacs

I have strange thing in my Emacs and I can't locate it, everytime I switch a buffer I get message with major mode name even when I call the function I get minibuffer-inactive-mode
The only global function (for all modes) in my .emacs file (I think) is this:
(add-hook 'after-change-major-mode-hook (lambda ()
(if (not (memql (intern (major-mode))
'(fundamental-mode
erc-mode
text-mode
sql-mode)))
(local-set-key (kbd "RET")
'new-line-and-indent-fix))))
How to find the place that add this annoying thing? What different hook can be executed on each mode?
There is no major-mode function in vanilla Emacs. Whatever that function is in your config, it's probably responsible for displaying the message you're seeing.
You want to fix your code (as per Stefan's comment), but you probably also want to look into that non-standard function:
M-x find-function RET major-mode RET

Breaking out of .emacs script

I use two emacs (Aquamcs and text based emacs) on my Mac.
I normally use text based emacs for just editing something, so I don't want to load anything with it.
What I came up with is to have the checking code in .emacs to exit/break if it's text based emacs (darwin system but not aquamacs).
(when (and (equal system-type 'darwin) (not (boundp 'aquamacs-version)))
(exit) ??? (break) ????
)
It seems to work, but I don't know how to break out of .emacs. How to do that?
ADDED
I just wanted to speed up in loading text based emacs on my mac, and I thought about breaking out as a solution. Based on the helpful answers, I came up with the following code that runs .emacs only when it's not a text based emacs.
(setq inhibit-splash-screen t)
(unless (null window-system)
I don't know of any way to do exactly what you want. Some workarounds:
You can stop the evaluation of your .emacs by evaluating (error "message") but that's a bit unpleasant.
You can re-order your .emacs so that there's a (unless (CONDITION) ...) around the whole of the file.
You can run emacs -Q FILE when you're at the command line.
Why do you want to do this? Are you concerned at the time it takes to load your .emacs? If so, you might consider using the Emacs client/server instead.
I am not sure how to exit as well but..... I would rather advice another kind of logic for your init file than a flat file with all different configurations.
Take for example your ~/.emacs (or better ~/.emacs.d/init.el) as your controller and files like ~/.emacs.d/aquamacs.el or ~/.emacs.d/textmode.el as your individual configuration files.
That would make your init having something like this :
(defun my-short-hostname()
(string-match "[0-9A-Za-z]+" system-name)
(substring system-name (match-beginning 0) (match-end 0))
)
;Load different config file in text mode or gui mode.
(if (null window-system)
(load-file "~/.emacs.d/texmode-emacs.el")
(load-file "~/.emacs.d/gui.el"))
;Load configuration for this host only, ie ~/.emacs.d/myhostname.el if exist
(if (file-exists-p
(downcase (concat "~/.emacs.d/" (my-short-hostname) ".el")))
(load-file (downcase "~/.emacs.d/" (my-short-hostname) ".el"))))
I suggest having specific, different files to load conditionally from your .emacs, one for one setup, another for another setup.
Alternatively, just wrap the code for each setup in a progn and do the conditional in place, in .emacs itself.

How do I make Emacs start without so much fanfare?

Every time I start Emacs I see a page of help text and a bunch of messages suggesting that I try the tutorial. How do I stop this from happening?
Emacs has a couple of variables which inhibit these actions. If you edit your emacs control file (.emacs) and insert the following:
;; inhibit-startup-echo-area-message MUST be set to a hardcoded
;; string of your login name
(setq inhibit-startup-echo-area-message "USERNAME")
(setq inhibit-startup-message t)
that should solve your problem. They basically set the inhibit parameters to true to prevent the behavior you want to get rid of.
Put the following in your .emacs:
(setq inhibit-startup-message t)
(setq inhibit-startup-echo-area-message t)
You can customize message in minibuffer therefore remove fanfare:
;; Hide advertisement from minibuffer
(defun display-startup-echo-area-message ()
(message ""))
Put the following in your personal init file (ususally ~/.emacs.el):
(setq inhibit-startup-message t)
(Or (setq inhibit-startup-screen t) in with older Emacs versions.)
You can also turn off the message "For information about GNU Emacs and the GNU system, type C-h C-a." in the echo with the variable inhibit-startup-echo-area-message, but it is not enough to set it to t; you must set it to your username. See the documentation for inhibit-startup-echo-area-message.
If your init file is byte-compiled, use the following form instead:
(eval '(setq inhibit-startup-echo-area-message "YOUR-USER-NAME"))
Add the below to your init file
(setq inhibit-startup-message t
inhibit-startup-echo-area-message t)