switch to buffer with C-x C-b without changing window - emacs

I usually have 2-3 windows open in emacs, and am particular about which buffer is shown where. While C-x C-f shows the buffer (file visited) in the current window, C-x C-b (and recentf-open-files from the recentf package) sometimes end up displaying it in another window, which then makes complicated rearrangements necessary.
Is there a way to force these commands to end up displaying the buffer in the window that was active when the C-x C-f or C-x C-b where issued?

The function at issue list-buffers is a one-liner -- so just change display-buffer to switch-to-buffer and redefine the keyboard shortcut to point to the new function:
(defun my-list-buffers (&optional arg)
"Display a list of existing buffers.
The list is displayed in a buffer named \"*Buffer List*\".
See `buffer-menu' for a description of the Buffer Menu.
By default, all buffers are listed except those whose names start
with a space (which are for internal use). With prefix argument
ARG, show only buffers that are visiting files."
(interactive "P")
(switch-to-buffer (list-buffers-noselect arg)))
(define-key ctl-x-map "\C-b" 'my-list-buffers)

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: (next-line) and (previous-line) behaves incorrectly

I have added the following code in my .emacs file:
(global-set-key (kbd "C-i") 'previous-line)
(global-set-key (kbd "C-k") 'next-line)
After that I start Emacs and push C-x C-b to open *Buffer List* buffer. Then I push C-x o to make it active.
After these actions I try my new keybindings and they work strangely! C-i prints No buttons! at the bottom of the frame. C-k moves the point to the next line but prints D at the previous one.
Any suggestions?
If you ask emacs to tell you what the keys are bound to, using C-h c C-k RET and C-h c C-i RET in your Buffer List buffer, you will be told that
C-k runs the command Buffer-menu-delete
TAB runs the command forward-button
Just because you bound those keys to other functions in the global key map does not mean that those bindings are active in any particular buffer, because there may be other keymaps in force that take precedence over the global map.
This is a large subject and you must proceed with caution (in particular, note that C-k is somewhat destructive: it marks the buffer for deletion - but it is conceivable that in a different buffer with a different mode, the map in force might make it much more destructive).
You can read about keymaps in the emacs manual.

How to set keyboard s-up to go move cursor to start of buffer in emacs (and s-down to move cursor to end of buffer)?

What can I add to my init.el to move the cursor to the very first character of the open buffer when I press s-up (i.e. hold "command" key, press "up" key on macOS), and equivalent for s-down?
I know it should be something like (global-set-key (kbd "s-<up>") ... )
(global-set-key (kbd "s-<up>") 'beginning-of-buffer)
(global-set-key (kbd "s-<down>") 'end-of-buffer)
You can find out what command a given key is bound to by using C-h k.
In this case, M-< takes you to the beginning of the buffer, and C-h k M-< tells you this:
M-< runs the command beginning-of-buffer, which is an interactive
compiled Lisp function in simple.el.
It is bound to begin, C-home, M-<, menu-bar search goto beg-of-buf.
(beginning-of-buffer &optional ARG)
Move point to the beginning of the buffer.
With numeric arg N, put point N/10 of the way from the beginning.
If the buffer is narrowed, this command uses the beginning of the
accessible part of the buffer.
If Transient Mark mode is disabled, leave mark at previous
position, unless a C-u prefix is supplied.
Don't use this command in Lisp programs!
(goto-char (point-min)) is faster.
Similarly, for M->, which is bound to end-of-buffer.

Emacs ido start with open buffers first in cycle list

When switching buffers with emacs ido mode enabled, a list of completions are displayed in the minibuffer. It appears there is a "feature" that buffers that are already open are put to the end of the list. I, however, often open the same buffer in multiple panes.
Is there a way to either turn this "feature" off, or alternatively do the opposite: have the buffers that are already open be at the front of the completion list?
The main point of ido mode is that you don't use arrows to navigate between buffers in the minibuffer. Instead you type the part of the buffer's name. In this case it doesn't matter where the buffer is in the list.
This is not possible unless you want to wade deep in ido's intestines.
As eGlyph already said: You're likely using ido wrongly (and there's also C-s for <right> and C-r for <left>; no need for arrow keys).
But you can define command for choosing among the already shown buffers (here only from the current frame, if you want all shown buffers you have to collect the windows first via `frame-list):
(defun choose-from-shown-buffers ()
(interactive)
(let ((buffers (mapcar (lambda (window)
(buffer-name (window-buffer window)))
(window-list))))
(pop-to-buffer (ido-completing-read "Buffer: " buffers))))

How do I save multiple buffers (of my choosing) at the same time in Emacs?

When I press C-x s (save-some-buffers) or C-x C-c (save-buffers-kill-terminal), Emacs displays the names of modified buffers one by one and asks what to do with each (save, diff, pass, ...). Pressing y one by one is slow. Pressing ! doesn't let you see what buffers are being saved.
How can I have the names of all modified buffers displayed first so that I can mark off some of them and save all the other quickly?
C-x C-b (M-x list-buffers) displays a list of all the buffers. Modified ones will be shown with a * next to them. You can mark a buffer for saving by pressing s. When you're done, press x to save all the buffers you marked.
Unfortunately, as far as I know, there's no way to show only the unsaved buffers or to sort them so they're all at the top.
(I actually prefer M-x ibuffer to M-x list-buffers, but ibuffer provides a similar feature.)
In emacs 23, with ibuffer :
'M-x ibuffer' (to open a list of buffers)
'*u' (start and u at the same time) to marked all unsaved buffers
'S' to save all marked buffers
Strangely enough, *u does not mark 'special' buffers like scratch, compilation, etc... I suppose i regexps on the name ...
Use ibuffer, which should come with all late-model emacsen. Put the following in your .emacs file:
(autoload 'ibuffer "ibuffer" "" t)
(global-set-key (kbd "C-x C-b") 'ibuffer)
(defun my-ibuffer-load-hook ()
"Hook for when ibuffer is loaded."
(define-ibuffer-filter unsaved-file-buffers
"Only show unsaved buffers backed by a real file."
(:description "unsaved file buffers")
(and (buffer-local-value 'buffer-file-name buf)
(buffer-modified-p buf)))
(define-key ibuffer-mode-map (kbd "/ *") 'ibuffer-filter-by-unsaved-file-buffers)
)
;; (add-hook 'ibuffer-load-hook 'my-ibuffer-load-hook)
(eval-after-load 'ibuf-ext '(my-ibuffer-load-hook))
Then use C-x C-b to bring up the ibuffer list, and / * to show just unsaved buffers backed by a real file (so you don't see *scratch* in the list, for example). Mark the desired buffers with m and then save them with S.
The answer to the question in the title is to pass an argument to save-buffers-kill-emacs (or -kill-terminal), ie. use the key combo C-u C-x C-c which will silently save all buffers and exit (or C-u C-x s to just silently save all buffers).
In emacs 23
C-x C-b (M-x list-buffers) to view buffer list
m to mark buffers to save
S to save marked buffers
u to unmark buffers individually or M-x dired-unmark-all-marks for all
I have googled now for this question and found the solution here
http://johntellsall.blogspot.com.es/2013/03/emacs-save-all-modified-buffers.html
You have to add this config to your ~/.emacs.d/init.el emacs configuration
(global-set-key
(kbd "M-*")
(lambda ()
(interactive)
(save-some-buffers t)))
save and eval the buffer (M-evalb-buffer) of the init.el file, and then when you want to save all the modified files you only have to press Meta key with "*" as is indicated in the second line
I hope this solution works!
Juan