emacs buffer bind to key - emacs

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

Related

How to move an emacs buffer to a new window?

I have many buffers open in an Emacs window. I want to move one of the buffers to a new window. Is there a command which does that?
The command you are looking for is tear-off-window. Note that this command must be associated with a mouse click event.
For example, you can put the following code (from this reddit comment) in your init file (more about init file here):
(global-set-key [mode-line C-mouse-1] 'tear-off-window)
This will call tear-off-window when you Control-click on the buffer modeline.
If you want to use a keyboard binding, the modified version of tear-off-window is below. Put it into your init file to save it after restarting emacs.
(bind-key "C-x C-X" #'my/tear-off-window)
(defun my/tear-off-window ()
"Delete the selected window, and create a new frame displaying its buffer."
(interactive)
(let* ((window (selected-window))
(buf (window-buffer window))
(frame (make-frame)))
(select-frame frame)
(switch-to-buffer buf)
(delete-window window)))
In the code, the modified command is bind to "C-x C-X". You are free to change it to any other key sequence (more details here).
IIUC, you want to create a new WM window.
Emacs uses a slightly different terminology: what are usually called "windows" in a GUI environment, Emacs calls "frames". WIthin a single frame, Emacs subdivides its area into separate "windows" (IOW, even in a non-GUI environment, Emacs acts as a "tiling" window manager). So, you can create a new frame with C-x 5 2 (or equivalently M-x make-frame-command RET) and then in that frame, switch to the required buffer with C-x b <buffer-name> RET.
Note by the way, that you don't "move" the buffer to the new frame: the buffer exists independently of whether there is a (emacs) window in a frame that displays the buffer.

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

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

Emacs: How do I create a new "empty" buffer whenever creating a new frame?

My Emacs is on OS X system. Is there any way to make a new frame defaulted to an empty buffer whenever I use ⌘N (just like the way TextEdit works)? I prefer to write contents first and decide an appropriate filename later. However, Emacs wants me to decide the filename first and write contents later. I don't see any advantage for it. Does anyone know why Emacs works that way?
Basically, if I use C-x 5 2, Emacs always pops up a frame with whatever file I am currently working on. This is inconvenient. I also don't want my Emacs to pop up a new frame defaulted to *scratch* (many Google search results somehow suggest this approach). I prefer it to have a buffer temporarily called "Untitled" in the new frame, and if I use ⌘N again, Emacs pops up another temporarily "Untitled 2" buffer, and so on. In this way, I can decide the buffer filenames later.
You can create new buffers with switch-to-buffer. Type C-x b, enter a buffer name, and press RET. If no buffer with that name exists, Emacs creates a new one automatically in Fundamental Mode. You may switch to any other mode as usual with M-x, e.g. M-x python-mode. To change the default buffer, set the default value of major-mode to the desired buffer.
If you'd like to have a buffer name chosen automatically, and create a new frame, however, you need to write your own command:
(defun lunaryorn-new-buffer-frame ()
"Create a new frame with a new empty buffer."
(interactive)
(let ((buffer (generate-new-buffer "untitled")))
(set-buffer-major-mode buffer)
(display-buffer buffer '(display-buffer-pop-up-frame . nil))))
Bind this to C-c n:
(global-set-key (kbd "C-c n") #'lunaryorn-new-buffer-frame)
Now pressing C-c n creates a new frame with a new empty buffer named “untitled” where x is a consecutive number.
The following will create a buffer with a unique name. The buffer is not associated with any file, so if/when you ever C-x C-s save-buffer, you will be prompted to supply a filename.
(defun empty-frame ()
"Open a new frame with a buffer named Untitled<N>.
The buffer is not associated with a file."
(interactive)
(switch-to-buffer-other-frame (generate-new-buffer "Untitled")))
This might work for you, if I understand your request:
(defun empty-frame ()
(interactive)
(let ((fl (make-temp-file "Untitled")))
(switch-to-buffer-other-frame fl)))
This will open a new temporary file for each new buffer. If you'd rather not actually create the file, you can use this instead:
(defun empty-frame ()
(interactive)
(let ((bn "Untitled-")
(num 1))
(while
(get-buffer (concat bn (number-to-string num)))
(setq num (1+ num)))
(switch-to-buffer-other-frame
(concat bn (number-to-string num)))))
You've seen answers as to how to create new "Untitled" buffers, but as for why Emacs wants you to first choose a name, some of the reasons are:
Historical: that's just how it worked, and once you get used to it, it's no worse than the alternative.
Major modes and various other details are usually chosen based on the name of the file. So instead of creating an Untitled buffer and having to choose whether to put it into LaTeX mode or C mode, you just open a file with extension ".tex" or ".c".
Having a file name means that Emacs can use the standard auto-save procedure, whereas with the Untitled approach, applications need to have some special way to auto-save those Untitled documents at some "standard" place.
C-x b *untitled* will open new buffer if not exist. Also, see Emacs manual

Opening a window into a specified buffer

How can I open a new window (for example using C-x 3) into a new buffer, rather than a mirrored buffer that just echoes what I type.
So for example, let's say I'm messing around with python and I want to run the script in the shell. As it is currently I do this: C-x 3, M-x shell and then start it up and running. I'd rather just C-x 3 and it automatically opens into shell. I'm really new to Emacs so I don't know where to look for this.
It sounds to me like this, or something similar, is what you are looking for:
(defun pop-to-buff-at-right (buffer)
"Pop to BUFFER at the right of the current window."
(interactive "B")
(pop-to-buffer buffer '(display-buffer-in-side-window
(side . right)
(inhibit-same-window . t))))
You do not want to just split the window, which is specifically about showing the same buffer twice. You want to switch to another buffer, but you want it to be displayed to the right of the current window.
In emacs it is easy to define custom commands and bind it to keys. For instance, if you add this to your init file:
(defun open-shell-at-left ()
(interactive) ;; Tell emacs this function can be called interactively
(split-window-right) ;; Just what C-x 3 does
(shell)) ;; Just what M-x shell does
(global-set-key (kbd "C-c 3") 'open-shell-at-left)
You will have what you want when you type C-c 3. In general, you can find documentation about what a key binding does by typing C-h k and the keybinding. From that point, it is easy to chain existing commands into new ones.

Is there any way to automatically close filename completetion buffers in Emacs?

For example, when you open a file via C-x-C-f, you can TAB complete file names, and if there are more than one possible completions, it will pop open a completion buffer with a list of possible completions. The problem is, after you've opened the file, the window the buffer was in switches back to normal, but it doesn't close. Is there any way I can make those buffers close automatically after the file has been opened?
Sorry to enter really late on this but this is how I do:
;; Remove completion buffer when done
(add-hook 'minibuffer-exit-hook
'(lambda ()
(let ((buffer "*Completions*"))
(and (get-buffer buffer)
(kill-buffer buffer)))))
Tested on GNU Emacs 22.x and 23.x
Although it does not directly solve your problem have you considered ido-mode as a mechanism for opening files?
ido-mode will bind C-x C-f to ido-find-file this allows you to interactively opening files (selecting between name collisions from within the minibuffer C-s and various other nifty features) I find it a much easier method of finding files and it will get rid of the *Completions* buffer altogether.