How can I more easily switch between buffers in Emacs? - emacs

I've recently started using emacs and I'm enjoying using it for the most part. The only thing I'm not enjoying, is switching between buffers. I often have a few buffers open and I've grown tired of using C-x b and C-x C-b, are there any packages that make switching between buffers easier? I've looked into emacs wiki on switching buffers and I'd appreciate insight/feedback on what are are using/enjoying. Thanks.

UPDATE: iswitchb-mode is obsolete in Emacs >= 24.4, replaced by ido.
All of the features of iswitchdb are now provided by ido. Ross provided a link to the documentation in his answer. You can activate with the following in your .emacs (or use the customization interface as Ross suggests):
(require 'ido)
(ido-mode 'buffers) ;; only use this line to turn off ido for file names!
(setq ido-ignore-buffers '("^ " "*Completions*" "*Shell Command Output*"
"*Messages*" "Async Shell Command"))
By default, ido provides completions for buffer names and file names. If you only want to replace the features of iswitchb, the second line turns off this feature for file names. ido will ignore any buffers that match the regexps listed in ido-ignore-buffers.
The behaviour described below for iswitchb-mode applies equally to ido for switching buffers.
iswitchb-mode (Emacs < 24.4)
iswitchb-mode replaces the default C-x b behaviour with a very intuitive buffer-switching-with-completion system. There are more sophisticated options, but I've never needed more than this.
After you hit C-x b, you are presented with a list of all buffers. Start typing the name of the buffer you want (or part of its name), and the list is narrowed until only one buffer matches. You don't need to complete the name, though, as soon as the buffer you want is highlighted hitting enter will move you to it. You can also use C-s and C-r to move through the list in order.
You can turn it on by default with this in your .emacs:
(iswitchb-mode 1)
You can also tell it to ignore certain buffers that you never (or very rarely) need to switch to:
(setq iswitchb-buffer-ignore '("^ " "*Completions*" "*Shell Command Output*"
"*Messages*" "Async Shell Command"))

You can use C-x <right> (next-buffer) and C-x <left> (previous-buffer) to cycle around in the buffer ring. You could bind S-<right> and S-<left> to these functions. (S is the "super-key" or windows-key). This way you can save some keystrokes.
Moreover, note that C-x b has a default entry, i.e. it displays a standard value (most of the time this is the previously viewed buffer), so that you don't always need to enter the buffer name explicitly.
Another nice trick is to open separate windows using C-x 2 and C-x 3. This displays several buffers simultaneously. Then you can bind C-<tab> to other-window and get something similar to tabbed browsing.

M-x customize-group ido then set Ido Mode to Turn on both buffer and file and set Ido Everywhere to on. Then click the Save for future sessions button at the top and enjoy ido magic for both files and buffers. Read the docs to get a sense of how to use ido.
Also, take a look at smex.

ido-mode provides an efficient way to switch buffers.
ibuffer is best for managing all opened buffers.
anything is good for selecting an interested thing from different
sources. (for eg: a single key can be used to switch to another
buffer or to open recently closed file or to open a file residing
in the same directory or ... anything you want ... )

If you've looked at the Emacs Wiki, you probably have all this information already, but here are a few other relevant Q&As:
Emacs: help me understand file/buffer management
Buffer switching in Emacs
How to invoke the buffer list in Emacs
My toolkit consists of ibuffer, windmove+framemove, winner-mode, and a custom binding to make C-xleft/right and C-cleft/right less of a hassle to use.

I have mapped the "ยง"-key to 'buffer-list and I find it to be very efficient.

I've started using anything for a couple of days and I'm really liking it: http://www.emacswiki.org/emacs/Anything .
Emacs-fu has an good intro to anything: http://emacs-fu.blogspot.com/2011/09/finding-just-about-anything.html

My favourite function for this is helm-mini which is part of helm.
As other helm functions, it allows incremental narrowing of the selection. It also searches your recently visited buffers, which is a really nice way to re-open a buffer. Helm can be a little surprising at first and as a new Emacs user, I found it visually overwhelming and I preferred ido or ibuffer which have been suggested in other replies. But now I absolutely love it and use it all the time for countless things.
Something that I realized by accident and that can be useful:
mouse-buffer-menu is by default bound to <C-mouse-1> (Control key + mouse left click) and opens a popup with a list of the current buffers.

Related

Emacs keep opening lots of buffers (Clojure)

I'm a beginner in Clojure and for some reasons I decided that Emacs would be a good choice, because of its usage among the clojurists.
But something that's really anoying me is that Emacs (for working with Clojure projects) opens a bunch of buffers. i.e, when I click "Read a directory to operate in its files" and select the lein's project root, I need to select myproject.core.clj and Emacs keep all the super dirs openned in other buffers. When compiling with cider and working with many clj files, it's painful to keep on alternating through all those buffers.
How can I limitate the numbers of buffers that Emacs leave openned?
(I know C-x k)
(Sorry for any English mistakes)
I had a lot of trouble with this same problem when I first started using Emacs. The solution is to use something other than C-x b or C-x C-b to switch between buffers. In particular:
I use Projectile's C-c p f to switch to arbitrary files in my project. It works very nicely because it takes your .gitignore into account. This can be annoying in some cases, but for the most part it works very well.
I use project-explorer to browse through a tree view of all the files in my project. The ignore features of project-explorer aren't quite as nice as those of Projectile, but you can toggle them using M-o when you need to, which is a feature Projectile doesn't have. project-explorer-open doesn't have a default binding, so I bind it to C-x p.
I use Ido and ido-ubiquitous to make all selection commands (including C-c p f) much easier to use.
If you lose your REPL buffer in CIDER, you can get it back instantly using C-c C-z.
If you already have multiple windows in front of you, windmove provides an extremely fast way to switch between them.
You'll generally end up with a lot of open buffers anyway, but since you now have an easy way to switch between them, that's no longer a problem. You can find a full example of this sort of setup in my personal Emacs config.
Personally, I use C-x C-f to open files, and don't bother with opening buffers for directories.

Emacs: how to open dired bookmarks in the same window

I managed to make dired work in a single window, when I am navigating through the file system.
Improving ergonomics, I decided to create bookmarks for my most frequent dirs with short names like: 'lwt', 'eve', etc. But every time I open the bookmark, the new dired buffer is created, even if the old one exists.
How to make it open the bookmark in the existing dired buffer?
Edit:
The original answer was actually a non-answer. I apologise for not having tested it properly. I will leave it here so other potential answerers aren't misled like I was.
I have meanwhile taken a good look at the source code of bookmark.el and the dired+ modifications don't have any effect on it. By default the bookmark-jump function uses the switch-to-buffer function as its display function. bookmark-jump however has an optional display-func argument, so a possible solution (involving a bit of elisp hacking) would be to create a function that reuses the current dired buffer (based on the dired+ source code) and invoking bookmark-jump with it, and if the concept works, then bind that to a keyboard short-cut.
Original answer:
The behaviour you are observing is just a side effect of the general "create a new buffer when navigating" behaviour of dired. This fact makes this question an almost duplicate of How do I stop emacs dired mode from opening so many buffers?.
Of the solutions proposed there and at the Dired Reuse Directory Buffer Emacs Wiki page, probably the simplest one is installing the Dired+ package and toggling directory buffer reuse with:
(toggle-diredp-find-file-reuse-dir 1)
in your .emacs file.
If this can help - that's what I use to open my bookmarks in the same buffer.
(defun my-bookmarks-list-same-buffer ()
"Open *Bookmarks* in current buffer."
(interactive)
(bookmark-bmenu-list)
(switch-to-buffer "*Bookmark List*"))
(global-set-key (kbd "s-b") 'my-bookmarks-list-same-buffer)
With Bookmark+, at least, bookmark-jump (C-x j j) to a Dired bookmark does reuse the Dired buffer if it already exists.

Using tabs in GNU Emacs 23

I have been looking into packages that would enable me to have tabs in my emacs editor. Would like to open multiple files in a single emacs window. Any help/link would be appreciated
Emacs has this functionality built in. They are called buffers C-x C-f opens a new file of a name you provide. C-x b lets you change between buffers. C-x C-b lists all open buffers. C-x k closes the current buffer.
If you want multiple buffers in one window then C-x 2 splits the windows horizontally. C-x 3 will split it vertically. C-x 1 will close all the windows that are not the current one. C-x o cycles between open panes. &c.
GNU the organization that manages the code base for GNU Emacs has a wonderful tutorial
Note: the the above segment C- means while holding the <CTRL> key
post script: the ido library makes working with emacs in generally much easier.
You can already open multiple files in buffers. However, if you want a visual guide, then this is what you might want: http://emacswiki.org/emacs/TabBarMode
Caveat emptor: You might have to configure it a bit.
or you can use elscreen
and use Gnu/Linux too, so i remap the Winkey to manage my tabs.
emacs doesn't contain "tabs" like an internet browser its best to understand the concept of buffers like the others have said but if you want to have a tab bar at the top anyway just to give a quick reminder of what buffers are open I suggest looking at the TabBarMode in the emacs wiki.
When using buffers i like to use m-x ibuffer since you can filter out which buffers your interested in. say you only want to see the buffers which are associated with files in a /home/flood/coding/epicgame/ directory, i simple go to ibuffer, type / f then type epicgame and a filter will be applied, then to remove the filter simply press / twice and remember that C-h m is your friend.
This technique has helped me alot, you can even OR filters together and merge them into groups. I love emacs XD

Emacs buffer shown after closing a buffer

How can I modify the way emacs picks which buffer to show after closing a buffer?
When I have multiple columns showing the same buffer, and then open another file in one of the buffers and then close the newly opened buffer, it doesn't switch back to the previous buffer, but to another buffer.
I'll try to explain with an example:
Start with a new emacs at *scratch*
C-x 2 (split into two columns)
C-x C-f 1 (find file 1)
C-x o (switch to other frame)
C-x b 1 (find file 1)
C-x C-f 2 (find file 2)
C-x k (kill buffer)
Now it switches to scratch but I would like it to show 1 in both windows again, is it possible to make emacs behave this way?
This may not be a direct answer to your question, but it might help.
Emacs manages its buffer list, including deciding which buffer gets displayed when you kill one (via kill-buffer). I haven't looked into how it's done, but the documentation is "out there". Lots of people have created custom buffer-stack management magic to change the way emacs does things, maybe some of them are based on bayesian analysis, or whatever. You can imagine the possibilities.
I've never looked into changing the way emacs manages its buffers. Instead I just bind other-window and switch-to-buffer to easy keystrokes (C-x o, C-x b) and I get really good at using them.
you could create a simple function for what you want: it should destroys all other windows, then split the window so that the current buffer is displayed in both. Luckily, emacs has functions that do exactly those things.
(defun cheeso-show-buffer-two-windows ()
"Close all other windows; then split, and show the current
buffer in both windows."
(interactive)
(delete-other-windows)
(split-window-vertically))
Bind that to a keystroke, and badda-bing, you're there. This is a vertical split - the windows are displayed in a vertical stack. If you want it horizontally split (the windows are side-by-side), then replace ... well, you know.
This also doesn't quite help directly, but Winner mode might help you get where you want to get.
Are you using tabbar-mode? I had the same problem and for me tabbar was the cause. Tabbar adds the function tabbar-buffer-kill-buffer-hook to kill-buffer-hook. You can remove it with (remove-hook 'kill-buffer-hook 'tabbar-buffer-kill-buffer-hook).
If you don't use tabbar try M-x describe-variable kill-buffer-hook. One of the functions in this list should be responsible for messing with your buffers.

Does Emacs has word and line completion (like Vim's insert mode completion)?

Vim completes words and lines with CTRL-X P and CTRL-L. There's a Emacs plugin called Company mode but this plugin interfere and cause conflicts with lots of things within Emacs (with global linum and yasnippets). I know that I can complete words with CTRL-/ in Emacs. But it is possible to take previously written lines to complete code?
Maybe you're looking for hippie-expand? From that web page (as of this writing, anyway):
HippieExpand looks at the word before
point and tries to expand it in
various ways including expanding from
a fixed list (like expand-abbrev),
expanding from matching text found in
a buffer (like dabbrev-expand) or
expanding in ways defined by your own
functions. Which of these it tries and
in what order is controlled by a
configurable list of functions.
For a comprehensive list of completion options visit the emacs wiki page on completion.
There are a gazillion ways to do completion in Emacs. Some are mode specific, some inline, some configurable and what not. Here is a list of modes that might help you.
Use numberic argument to complete by line, say M-5 M-/ will complete by line, while M-/ alone still complete the normal way.
hippe-expend function has a very useful feature which is :
With a positive numeric argument, jumps directly to the ARG next function in this list. With a negative argument or just C-u, undoes the expansion.
Customize the expansion functions in hippie-expand-try-functions-list and put the function try-expand-line as 5th list element, then you could use M-5 M-/ to complete by line.
This tip is very handy and useful and I highly recommend it.
Also worth noting: if your window manager does not steal Alt-tab, emacs will auto-complete with Alt-tab (I set up my window manager to user the "windows key" instead of alt for this very reason).
If you are using evil, this is the most vim-like solution I use:
(defun my-expand-lines ()
(interactive)
(let ((hippie-expand-try-functions-list
'(try-expand-line-all-buffers)))
(call-interactively 'hippie-expand)))
(define-key evil-insert-state-map (kbd "C-x C-l") 'my-expand-lines)
This way you can use our old friend C-x C-l in insert mode for whole line all buffers completion.
Thanks #ymln for the suggestion of using try-expand-line-all-buffers.