How to invoke the buffer list in Emacs - emacs

I usually type M-x buffer-menu to switch buffers in Emacs. How can I do this with a shorter command? Its quite a long string to type.
Thanks!

You can use C-x b to change buffers. You have to enter the first few letters of the buffer name, and of course you can use completion. If you press TAB (the most useful key in Emacs), a list of (matching) buffers appears. You can click in this list to switch to a buffer.
You can bind buffer-menu to a key. Pick a key that's not used for another command — let's say f12 — and add the following line to the file ~/.emacs:
(global-set-key (kbd "<f12>") 'buffer-menu)
There are many other interfaces to changing buffers in Emacs, and they can be significantly more efficient than C-x b and C-x C-b. Since this tends to be a very personal choice, I recommend you experiment with a few and keep the one(s) you feel most comfortable with.

C-x C-b
As stated here

I'd highly recommend switching to a mode designed for efficient buffer switching.
If your version of Emacs is recent enough (22+):
M-x ido-mode
and then:
C-x b
to switch buffers, with incremental substring matching, C-s and C-r rotate forward and backwards through the matches.
If you have an older version of Emacs, it should have:
M-x iswitchb-mode
and then, as with ido-mode:
C-x b
opens up the minibuffer to let you choose the buffer to switch to.

Bind C-x C-b to buffer-menu. There is no sense leaving it bound to list-buffers. list-buffers is just a eunuch version of buffer-menu. ;-)
And you might want to try this: http://www.emacswiki.org/emacs/BufferMenuPlus

Try bs-show (in my opinion a way better than C-x C-b). You can bind it to F9 by adding this to .emacs:
(global-set-key (kbd "<f9>") 'bs-show)

Related

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.

emacs get rid of C-x

Is there a way I can stop needing C-X before any shortcut and, for example, just use C-c to leave?
Also, if possible, what disadvantages could this have?
I'm looking for a simpler way of using GNU Emacs, but not sure if I can find one.
(this is too long for a comment)
Not really an answer but I know that some people hate the "C-x anything" in Emacs not just because you have to "type a lot of keys" but also because from a touch-typing point of view C-x doesn't make that much sense.
But of course in Emacs everything is configurable. I'm using "C-," instead of C-x and I honestly find it much easier this way.
I use my left pinky to hit CTRL, which is the key physically located at the left of 'a' on a QWERTY keyboard: i.e. the key that used to be CTRL but that now often is labelled CAPS-Lock [and you can remap it to CTRL].
I then touch-type ',' with my right hand.
To this end I added this to my config:
(define-key global-map [(control ,)] ctl-x-map)
You won't have less keys to type to do, say, a C-x C-c (which you can now do both by doing C-x C-c or by doing C-, C-c), but at least you won't be distorting your fingers as much ; )
Also note that as explained here (see user "scottfrazer"'s +40+ upvoted and accepted answer) it may be better to create a minor mode for all your key mappings instead of directly define a global mapping:
Globally override key binding in Emacs
My recommendation is to first learn the standard Emacs key bindings, then, after a while change whichever ones you think will do you the most good to change.
See also CUA mode, in the Emacs docs -- I don't use or recommend CUA mode, but a lot of people (esp. newbies?) use it to keep their cut, copy, paste key habits.
After looking at the source code of boon.el, I found out about ctl-x-map. C-x is a prefix key, prefix keys often have their own maps.
To use M-m for C-x shortcuts and unbind C-x, use this
(global-set-key (kbd "M-m") 'ctl-x-map)
(global-set-key (kbd "C-x") nil)
I think it is a very bad idea, because the C-x prefix can handle such situations like C-x s vs C-x C-s. And: you are really f*cked when you read emacs tutorials and have your own keybindings
If you want some other keybindings read ErgoEmacs Keybinding or How to Set Emacs's User Interface to Modern Conventions

How to scroll up in Emacs ansi-term

I've been Googling around and looking at Emacs built-in help but I have yet to determine how to scroll up (or down) in Emacs ansi-term.
I'm using Emacs 23.3.1, OS X, in iTerm2. Thanks!
Edit: I've noticed most the advice people give me doesn't work in ansi-term but does work in eshell. I have since moved to eshell.
In general, if you don't need full screen terminal emulation, shell or eshell are better choices.
However, if you decide to stick with ansi-term, press C-c C-j to go into line mode. Then you can move around normally with the usual cursor movement keys. Press C-c C-k to get back into char mode to interact with the terminal.
Alternatively, you can scroll backwards a screen at a time with C-c C-v and just enter text to scroll back to the terminal input point.
Take a look at the Emacs documentation on term-mode (most of which applies equally to ansi-term) for more information.
Shift-page up/down (in Emacs-speak, S-prior/S-next) will work using the default bindings.
(While the normal C-h m/C-h b don't work to see mode information and bindings in this mode, you can still use C-c M-x describe-mode/describe-bindings, or depending on your setup, use F1 or the help key instead of C-h.)
install evil-mode at first, press C-z to switch to vim key binding.
You can use C-f, C-b to scroll up and down
you can use 20% to jump to to the top 20% of the buffer
you can use /, ?, #, * to search the text in the buffer.
all the grep/filter commands now usable (occur, swiper, helm-swoop, .... just name a few)
you can narrow/widen the buffer
you can yank text
Have you tried: Page up, up arrow, Ctrl-V,Alt-V
For ansi-term, I have this in my .emacs:
(add-hook 'term-mode-hook
(function
(lambda ()
(define-key term-raw-map [?\C-c prior] 'scroll-down)
(define-key term-raw-map [?\C-c next] 'scroll-up))))
Then I can use C-c pgup and C-c pgdn to scroll.
I was having the same issue but with multi-term (zsh), and after reading the response from #muffinista (the C-v did not work for me) but the Alt-v worked to go 1 page, after that you can use the normal C-p and C-n to scroll up and down.
This worked for me, but it depends a lot on which term you are using and key bindings you might have.
Up and down are Ctrl-P and Ctrl-V. There's a whole long list here

Emacs copy with regex

I have a text file. Can Emacs select text based on regex and put it in kill-ring, so I can copy it somewhere else? Something like regex-kill-ring-save?
inspired by the already given comments (the Charles answer doesn't work as I would want it), I added a new function to the isearch/isearch-regexp mode map which puts only the matching string into the kill ring (whereas Charles proposal kills from current point to end of matching string):
(defun hack-isearch-kill ()
"Push current matching string into kill ring."
(interactive)
(kill-new (buffer-substring (point) isearch-other-end))
(isearch-done))
(define-key isearch-mode-map (kbd "M-w") 'hack-isearch-kill)
The nice thing about the isearch/isearch-regexp approach (which you can enable with C-s and C-M-s respectively) is that you can see your search string growing and you can copy it with M-w as soon as you are satisfied (and go back to where you have been before with C-u C-Space).
This works for me with Emacs 23.1. Don't know if it will work in all situations. Anyway I hope you find it useful :)
UPDATE: going through the emacswiki I stumbled over KillISearchMatch which suggests more or less the same (plus some more tips ...).
Cheers,
Daniel
I'm not sure if there is such a function already, but what you can do it with a keyboard macro:
Start recording a kbd macro: C-x (
Search for your regexp with search-forward-regexp
Move to the beginning of your match (the text you want to kill) with the various emacs navigation commands, e.g. search or backward-word etc.
Mark: C-spc
Move to the end of your match
Kill the text: C-w
You can then name the keyboard macro with M-x name-last-kbd-macro so that you can execute the macro with a name rather than with C-x e.
If you want to save the macro for future sessions, you can open your .emacs and insert the macro into the buffer with M-x insert-kbd-macro. After than you can bind a key to the macro just like you bind keys to normal emacs functions, e.g. (global-set-key "\C-c m" 'funky-macro-macro).
More about emacs keyboard macros
Isearch+ does this already. It optionally sets the region around the search target. You can use C-SPC C-SPC or M-= C-SPC at any time during Isearch to toggle this.
isearchp-deactivate-region-flag is a variable defined in isearch+.el.
Its value is t
Documentation:
Non-nil means isearching deactivates the region.
See also option isearchp-restrict-to-region-flag.
You can toggle this option using M-= C-SPC during Isearch.
You can customize this variable.

emacs list-buffers behavior

In GNU emacs, every time I hit Ctrl-x Ctrl-b to see all of my buffers, the window is split to show the buffer list, or if I have my window already split in 2 (for instance, I will have a shell running in the lower window), the buffer list appears in the other window.
My desired behavior is for the buffer list to appear in my active window so that I can select the buffer I want and continue to working in the same window, rather than having to Ctrl-x Ctrl-o to the other buffer, selecting the buffer (with enter) and editing that buffer in the other window... I've googled for it but it doesn't seem to be a common desire? I wonder if anyone has an elispy (or other) solution?
You might want to rebind C-x C-b to invoke buffer-menu rather than list-buffers:
(global-set-key "\C-x\C-b" 'buffer-menu)
Just customize the variable same-window-regexps. display-buffer will display any buffer whose name matches a regexp there in the currently-selected window.
(You will want to add "[*]Buffer List".)
not exactly a solution, but ido-mode provides a different and powerful way to interact with buffers. C-x b will then show a list of all the open buffers, and the one you select will open in the current window.
Strangely, there isn't an answer here about ibuffer.
I would recommend this as a standard change for the majority of Emacs users:
(global-set-key (kbd "C-x C-b") 'ibuffer)
ibuffer is a very advanced replacement for the default buffer listing, and not only features the exact behaviour requested, but provides a wealth of other functionality.
I listed a few ibuffer filtering and grouping basics in
Emacs: help me understand file/buffer management, but be sure to read the documentation for details.
Try to add
(ido-mode 1)
to your .emacs, and enjoy the result :)
If you like the original buffer list (as opposed to the 'buffer-menu solution proposed by others), you can use this:
(global-set-key (kbd "C-x C-b") 'my-list-buffers)
(defun my-list-buffers (&optional files-only)
"Display a list of names of existing buffers.
The list is displayed in a buffer named `*Buffer List*'.
Note that buffers with names starting with spaces are omitted.
Non-null optional arg FILES-ONLY means mention only file buffers.
For more information, see the function `buffer-menu'."
(interactive "P")
(switch-to-buffer (list-buffers-noselect files-only)))
Which is the same function as before, only in the current window.
I highly recommend bs.el from http://www.geekware.de/software/emacs/ Install it and:
(require 'bs)
(add-hook 'bs-mode-hook 'turn-on-font-lock)
(global-set-key "\C-x\C-b" 'bs-show)
It manages buffers and window configuration in the right way, so everything requires minimum number of keystrokes.
Not sure where I got this but:
;;; Faster buffer switching
(global-set-key [(C tab)] 'buffer-menu)
This makes Ctrl-Tab display all buffers in the current window. You can then navigate to a buffer and hit Enter to visit it.
Another not-what-you-asked-for solution: don't select the desired buffer with the mouse, rather finish typing its name (use tab-completion to reduce keystrokes and increase accuracy), then hit return. The buffer list will disappear, and the new file will be open in the previously active window.