How to copy path of a file in emacs helm-mode - emacs

I'm using helm-mode in emacs to open files. However, when I try to copy the path of a file (say /home/user1/Documents/file1.txt) through mouse left-click and hold to paste it in terminal, I get a message saying
<down-mouse-1> is undefined
I guess helm does not support mouse operations as described here, in which case how can I copy path of a file from emacs (in helm-mode) to paste it in terminal

The answer given in this other thread may seem more straightforward.
In short: with the file selected in the minibuffer use C-u C-c C-k. This invokes helm-kill-selection-and-quit. The file's full path is copied to the kill ring and can be pasted in Emacs or elsewhere.

I guess you want to copy from Minibuffer to your system clipboard. Minibuffer keybinding isn't different from other buffers. If in other buffers you use M-w to copy the region, it should also work in Minibuffer. Note that if you niled x-select-enable-clipboard you need to enable it first. I have the following functions in my init.el
(defun copy-to-clipboard()
(interactive)
(setq x-select-enable-clipboard t)
(kill-ring-save (region-beginning) (region-end))
(setq x-select-enable-clipboard nil))
and
(defun paste-from-clipboard ()
(interactive)
(setq x-select-enable-clipboard t)
(yank)
(setq x-select-enable-clipboard nil))
Unfortunately you can't use your mouse to select the texts (ie. to make a region) in helm-mode; you need to set-mark-command (by default C-SPC or C-#) and move your point (ie. cursor). Or just hold the shift and move the point like most other text editors. There is also a mark-word command (by default M-#) that expands the region word by word.
I also recorded an asciinema (because they're fun 🙂) that you can watch it here

Related

Emacs disable mouse-1 in dired

In Emacs 23.2.1 in Dired mode the mouse-1 (left mouse button) performs visit file in other window. It also changes shape to a finger and highlights the filename when cursor hovers over the filename. How do I disable both visit file and filename highlighting ? I want mouse-1 to do its usual stuff: selecting text.
I can still select text if I start by clicking down in an area outside the filename or directory name. But I only want the filename marked, and not have a space in front included.
I just turn off mouse-1-click-follows-link by customizing it to nil. (You can also set it to a long time-limit value.)
Or if you want to do that only for Dired buffers, you can do this:
(add-hook 'dired-mode-hook
(lambda ()
(set (make-local-variable 'mouse-1-click-follows-link) nil)))
But it is typically better to name a function that you use on a hook (it's easier to remove it, for one thing):
(defun foo ()
(set (make-local-variable 'mouse-1-click-follows-link) nil)))
(add-hook 'dired-mode-hook 'foo)
If you have a recent version of Emacs, where setq-local is defined, then you can use just (setq-local mouse-1-click-follows-link nil) in the hook function, in place of (set (make-local-variable 'mouse-1-click-follows-link) nil)

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

How to exclude specific extension file or directory be opened another application by org-open-at-point(C-c C-o) in Emacs?

My org file is like following.
In org-mode when I use org-open-at-point(C-c C-o), another directory application(Finder in mac) will be opened. But I want to open directory ./ in Emacs dired by org-open-at-point(C-c C-o). C-u C-c C-o will do this. but I don't want to type C-u.
my.org
sample dir is [[./][here]]
sample memo is [[file:sample.memo][here]] <-- this won't be opened by C-c C-o,
because there is no application
to open *.memo file.
Thanks.
It's not 100% clear what the problem is. If you're on [[./][here]], C-c C-o should open the dired buffer automatically, and if on [[file:sample.memo][here]] it should open (or create) the file automatically.
If your goal is to FORCE org to visit a file in Emacs (overriding whatever you have set in org-file-apps), you pass the prefix argument to C-c C-o (ie, as C-u C-c C-o). If, as you mentioned, you don't want to prepend the C-u, you can write a little function as below and bind it to the keys of your choice. All it's doing is setting the the optional argument programmatically:
(defun ooap-force-emacs ()
"Visit a file in emacs from an org-mode buffer."
(interactive)
(org-open-at-point t))
You can use:
(add-hook 'org-mode-hook
'(lambda ()
(add-to-list 'org-file-apps '(directory . emacs) t)))
to open directories using Emacs instead of system default.
See:
M-x customize-variable [RET] org-file-apps

Emacs: what is the shortcut key to clear buffer?

Like, Control-A (select all) followed by delete?
How about using:
M-x erase-buffer
Which you could bind to whatever you want.
C-x h + del key clears the buffer
Note: This requires transient-mark-mode to be enabled (which it is by default).
Select all in Emacs is:
C-x h
(technically, that's mark-whole-buffer) and kill-region (to kill the marked region, which is now the entire buffer) is:
C-w
If you want to delete the region without copying it to the kill-ring, you can use
M-x delete-region
instead. If you do this alot, you'll want to bind delete-region to a key/key combo.
These macros build on the answers given above. To start using them paste them into your .emacs then restart emacs or (while in the .emacs buffer) type M-x eval-buffer.
(defun clear-buffer ()
"clear whole buffer add contents to the kill ring"
(interactive)
(kill-region (point-min) (point-max))
)
(defun clear-buffer-permenantly ()
"clear whole buffer, contents is not added to the kill ring"
(interactive)
(delete-region (point-min) (point-max))
)
Old folks might like to call it hk rather than clear-buffer-permanently,
and assign it to the nostalgic key sequence like so:
(define-key esc-map "\^[hk" 'hk)
That's the pre-Gnu TECO EMACS command to clear the buffer
(ESC ESC wHole Kill).
Go to the begin of the buffer, go to the end (both set the mark), then cut:
M-< M-> C-w
There is no shortcut, but you can define one...
Follow this link to get a macro for clearing a buffer.

emacs buffer bind to key

I don't know why but currently emacs opens only one copy of w3m. If w3m is already open then retyping the command to open w3m takes me to the already opened buffer. I would like to configure ansi-term similarly i.e. typing C-x C-a (command open ansi-term) should take me to already opened ansi-term instead of opening a new buffer altogether.
How can I achieve this in emacs?
You could write a wrapper function around ansi-term that checks to see if there already is an existing terminal buffer, and recycles that buffer if it exists:
(defun green-ansi-term ()
"Show an existing buffer called \"*ansi-term*\" if one exists, otherwise
call function ansi-term interactively."
(interactive)
(let ((existing-buffer (get-buffer "*ansi-term*")))
(if existing-buffer
(switch-to-buffer existing-buffer)
(call-interactively 'ansi-term))))