How to open ido completion buffer immediately on ido-insert-file - emacs

I have a directory ~/emacs/templates/matlab with template files. When pressing a keyboard shortcut I would like to insert a given template at point in the current buffer. This can be achieved using ido-insert-file (see Buffer menu to select a set of filenames in Emacs ) :
(defun test-ido-insert-file ()
(interactive)
(save-excursion
(let ((default-directory "~/emacs/templates/matlab" ))
(ido-insert-file))))
However, I have to press the TAB key twice before the ido completion buffer is displayed.
First, running my test-ido-insert-file I get the following:
then pressing the TAB key twice, I finally get:
How can I bring up the completion buffer immediately, avoiding pressing the TAB key twice?

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.

emacs switch to last active window?

I was wondering if there is a way in Emacs lisp to jump back to the last active window, just as popd would do in Linux?
The reason I ask is that in some environments for evaluating code (e.g. babel-repl) the editing area for the source code loses focus to the REPL once the REPL is launched. I'd like to change its behavior and switch the focus back to the editing area, e.g., by adding an additional command in elisp to jump back to the last active window before launching the REPL.
previous-window or get-mru-window should do what you want. You can then switch with
(select-window (previous-window))
If you are interested in switching just the active buffer, you can use:
(defun prev-window ()
(interactive)
(other-window -1))
(global-set-key [(f12)] 'prev-window)
(global-set-key "\C-cp" 'prev-window)
(global-set-key "\C-cn" 'other-window)
I have this in my ~/.emacs file. CTRL+C then p (or F12) to go to the previous buffer. CTRL+C then n to go to the next buffer. The function other-buffer lets you revisit the last visited buffer. By supplying a negative number you can reverse the order that it goes through the buffer list.

Emacs button doesn't work after saving, closing and re-opening the file

I added the following code to my .emacs to create a button when Shift-F10 is pressed:
(require 'button)
(global-set-key (kbd "<S-f10>")
(lambda ()
(interactive nil)
(insert-button
"Open and search file."
'action (lambda (x) (find-file "~/org/bib.org") (isearch-forward)))
)
)
When I press Shift-F10 in a buffer containing a file, the button does get inserted, the label is highlighted and it works properly. However, if I save, close and re-open the file, then the button label is not highlighted and the button doesn't work.
Why doesn't it work after the file is closed and re-opened?
A button inserted into a buffer is not persistent, even if you save the buffer in a file.

Modify Emacs tab behavior with Sweave documents

In Sweave mode pressing tab doesn't move the cursor and kills my auto-complete options (when I hit tab to complete the code the completion disappears as opposed to completing the snippet).
If I space over to the center of the buffer, pressing tab snaps the cursor back to leftmost side of the file.
When I am editing a 'foo.tex' or 'foo.r 'file tab indents and autocomplete behave normally. Is there a way in which I can define tab behavior for Sweave (.rnw) files?
You might try to set this in your init file.
(setq ess-tab-complete-in-script t)
(add-hook 'ess-mode-hook
'(lambda()
(local-set-key (kbd "<tab>") 'ess-indent-or-complete)
))
When you are inside an R code chunk, the first TAB hit will indent the line if it is not properly aligned, otherwise it will auto-complete the word. You can further customise this behaviour with the variable ess-first-tab-never-complete and decide if the completion should occur only at end of lines, end of words etc. (see variable description).

How do I close an automatically opened window in Emacs?

This is probably a very naive Emacs question - I'm new to it.
When I'm evaluating a lisp expression, if there's an error the debugger automatically comes up in another window. If I have *scratch* and *info* open (the former for trying out lisp and the latter for reading about it), then the debugger opens up in the window that *info* was in. At the moment, I have to switch to that window, then change it back to *info*, before returning to *scratch*. (The same thing happens if I do C-x C-b for a list of buffers.) I'm guessing there has to be a way to just close that window without this long sequence of commands. Can anyone enlighten me?
At least here on my emacs (22.3), when the debugger pops up, its window becomes the active one. There, pressing q just quits the debugger, if that's what you want. At that point, it also gets out of recursive editing.
From what I understand, you want to close the buffer in the other window without moving your cursor from the current window.
I don't any existing function does that, so I rolled my own.
(defun other-window-kill-buffer ()
"Kill the buffer in the other window"
(interactive)
;; Window selection is used because point goes to a different window
;; if more than 2 windows are present
(let ((win-curr (selected-window))
(win-other (next-window)))
(select-window win-other)
(kill-this-buffer)
(select-window win-curr)))
You can bind it to something like "C-x K" or some other somewhat difficult-to-press key so you won't press it by mistake.
(global-set-key (kbd "C-x K") 'other-window-kill-buffer)
I use this a LOT! (for Help buffers, Compilation buffers, Grep buffers, and just plain old buffers I want to close now, without moving the point)
I'm usually using the delete-other-windows command. C-x 1.
It's so regullar, that I rebinded to F4.
Official docs: https://www.gnu.org/software/emacs/manual/html_node/emacs/Change-Window.html#Change-Window
HTH
Using winner mode, you can use the keybinding C-C left arrow to return to the previous window configuration. Of course, this doesn't actually kill the new buffer, but it does hide it.
I think you're looking for C-x 4 C-o, which displays a buffer in the "other" window without switching to it.
As mentioned above, in the case of the backtrace buffer you probably want to exit from it with q, to get out of the recursive edit.
Try to use popwin-mode. It is really good about closing windows automatically.
I want to describe how mechanism work|
You open undo-tree-visualize than you find your correct branch, after that when you change your active window which has cursor via switch-window undo-tree related buffer will close other window automatically.
One possible solution is to use a window management package. Store your "preferred" window arrangement, and after the debugger/buffer window pops up and you're done with it, revert to your preferred arrangement.
There are a bunch of packages to choose from, see: switching window configurations on the wiki.
Additionally, you might want to figure out the actions you commonly do that trigger the extra (unwanted) window popping up, and use that to trigger saving your window configuration just before the window pops up.
If you want to roll your own (it's pretty easy), you can just save off the window configuration, and restore it like so:
(setq the-window-configuration-i-want (current-window-configuration))
(global-set-key (kbd "<f7>")
(lambda () (interactive)
(set-window-configuration the-window-configuration-i-want)))
The trick is figuring out where to put the setting of the-window-configuration-i-want.
Thanks go to #spk for a nice function. I merely modified it to also close the window too and bound it to the already nice set of window control functions found in evil-windows (via "C-w"):
(use-package evil
:bind (:map evil-window-map ("O" . delete-most-recent-window))
:preface
(defun delete-most-recent-window ()
"Kill the buffer in the most recent window."
(interactive)
;; Window selection is used because point goes to a different window
;; if more than 2 windows are present
(let ((win-curr (selected-window))
(win-other (next-window)))
(select-window win-other)
(kill-this-buffer)
(delete-window) ; <-- Added this
(select-window win-curr))))