Emacs key bindings with multiple keystrokes - emacs

I would like to open my five (or so) favorite files with two keystrokes in emacs. I am thinking of F9-a for file a, F9-b for file b, etc.
I have the org-mode agenda at F5 (instead of the "default" C-a), so I hit F5-a for today's agenda. I'd love to have the same for my org-file, my contacts file and so on.
Is there a way (or rather: What is the way) to make F9 open a sub-menu with a list of my favorite files to choose with another keystroke?

This setup does what you are asking for, at least in terms of keymaps. It doesn't actually pop up a menu.
(setq my-file-menu-keymap
(let ((keymap (make-sparse-keymap)))
(dolist (pair '(("a" . "~/.emacs")
("b" . "~/.phones")
("c" . "~/org/daily.org")))
(define-key keymap (car pair)
`(lambda () (interactive) (find-file ,(cdr pair)))))
keymap))
(global-set-key (kbd "<f9>") my-file-menu-keymap)
I use a single key to cycle through my favorite files (works well for a small number of files and I don't care about the random access). You can find code for that in this answer to a different SO question (see cycle-special-files).

You may get better results with a package which allows you to complete file names from recent files. This is less static than binding file names explicitly to keys and you can easily access much more recently used files with this method than only 5.

Related

in Emacs, how to maintain a list of recent directories?

In Emacs, I use recentf extensively. Rather than calling find-files, I usually call a custom function xsteve-ido-choose-from-recentf instead which allows me to choose from my recentf files.
How do I create and maintain a separate list of recent directories, separate from the list of recent files? So that instead of calling dired, I could call something like ido-choose-from-recent-directories?
You ask, in your comment replying to #Stefan's answer: And how do I get from the above to viewing a list of recent directories? -
The answer is that you use the little-known fact that if the DIRNAME argument to dired is a list of (a) the new Dired buffer name followed by (b) file (or directory) names, then Dired is opened for just those files/dirs. IOW:
M-: (dired (cons DIRED-BUFNAME YOUR-LIST-OF-RECENT-DIRECTORIES))
For example:
M-: (dired '("My Dired Buffer" "/a/recent/dir/" "/another/recent1/" "/another/"))
If you use library Dired+ then you can provide such a list interactively, by using a non-positive prefix arg with dired.
But in this case you want to write a command that first gathers the list of recent directories and then opens Dired for them. This should do it:
(defun dired-recent (buffer)
"Open Dired in BUFFER, showing the recently used directories."
(interactive "BDired buffer name: ")
(let ((dirs (delete-dups
(mapcar (lambda (f/d)
(if (file-directory-p f/d)
f/d
(file-name-directory f/d)))
recentf-list))))
(dired (cons (generate-new-buffer-name buffer) dirs))))
That works for me. However, vanilla Emacs does not let you use i to insert the listing for any directory that is not in the same directory tree as the default-directory of the Dired buffer. That means that the above code will work OK, but you will not be able to insert any of the listed directories.
To be able to do that, load library dired+.el. Dired+ also fixes a couple of other inadequacies wrt the vanilla handling of a cons arg to dired.
The above code, together with Dired+ should give you what you want.
UPDATE
I just added this to Dired+. These are the commands added: diredp-dired-recent-dirs and diredp-dired-recent-dirs-other-window.
UPDATE 2
I made it simple to choose which of the recently used directories to include or exclude. Use a prefix arg to initiate such choosing. With no prefix arg you get all recent dirs. I also made it possible to use a prefix arg to be prompted for the ls switches. Here is the doc s tring of diredp-dired-recent-dirs:
Open Dired in BUFFER, showing recently used directories.
You are prompted for BUFFER.
No prefix arg or a plain prefix arg (`C-u', `C-u C-u', etc.) means
list all of the recently used directories.
With a prefix arg:
* If 0, `-', or plain (`C-u') then you are prompted for the `ls'
switches to use.
* If not plain (`C-u') then:
* If >= 0 then the directories to include are read, one by one.
* If < 0 then the directories to exclude are read, one by one.
When entering directories to include or exclude, use `C-g' to end.
Finally, I added bindings for the commands: C-x R (same window) and C-x 4 R (other window), where R is Shift + r.
You don't need to maintain a separate list (which would be a lot of work). Instead, you can extract that list from the recentf list. E.g.
(delete-dups
(mapcar (lambda (file)
(if (file-directory-p file) file (file-name-directory file)))
recentf-list))
Pragmatic Emacs found the solution.
Here is a function to give you a list of recent directories, using ivy
(part of swiper) to narrow it dynamically, and then open the selected
one in dired.
;; open recent directory, requires ivy (part of swiper)
;; borrows from http://stackoverflow.com/questions/23328037/in-emacs-how-to-maintain-a-list-of-recent-directories
(defun bjm/ivy-dired-recent-dirs ()
"Present a list of recently used directories and open the selected one in dired"
(interactive)
(let ((recent-dirs
(delete-dups
(mapcar (lambda (file)
(if (file-directory-p file) file (file-name-directory file)))
recentf-list))))
(let ((dir (ivy-read "Directory: "
recent-dirs
:re-builder #'ivy--regex
:sort nil
:initial-input nil)))
(dired dir))))
(global-set-key (kbd "C-x C-d") 'bjm/ivy-dired-recent-dirs)
Source:
Open a recent directory in dired: revisited | Pragmatic Emacs

emacs switch buffer per window

All,
I'm looking for a way to switch buffers in multiple windows similar to other IDEs. Let's say I split my frame into two windows (C-x 3). I'd like to handle buffers for .c filesalways in the left window and .h files in the right. I'm looking for a way to couple a list of buffers to a specific window, and cycle buffers only belonging to the current window. So if I'm editing a .h file on the right side, and press a key combination I get the next .h file.
Thanks for any advice,
Janos
I use buffer-stack (available via MELPA) for something similar. buffer-stack allows you to cycle between buffers in a very flexible way (it remembers your most recent used buffers, and it uses separates stacks per frame).
You can add filter functions to buffer switching commands. So you could add a filter function for considering only buffers with filenames with same extension than the current one. I use a filtering function to switch among buffers of the same mode. Here is it as an example that shows my keybindings to switch filtering by current mode and also swith to dired buffers. It would be easy to add another filter based on file extension:
(defmacro command (&rest body)
`(lambda ()
(interactive)
,#body))
(defvar buffer-stack-mode)
(defun buffer-op-by-mode (op &optional mode)
(let ((buffer-stack-filter 'buffer-stack-filter-by-mode)
(buffer-stack-mode (or mode major-mode)))
(funcall op)))
(defun buffer-stack-filter-by-mode (buffer)
(with-current-buffer buffer
(equal major-mode buffer-stack-mode)))
(global-set-key [(meta kp-7)]
(command (buffer-op-by-mode 'buffer-stack-up)))
(global-set-key [(meta kp-9)]
(command (buffer-op-by-mode 'buffer-stack-down)))
(global-set-key [(meta kp-3)]
(command (buffer-op-by-mode 'buffer-stack-down 'dired-mode)))
(global-set-key [(meta kp-1)]
(command (buffer-op-by-mode 'buffer-stack-up 'dired-mode)))
You can use ff-find-other-file. With a prefix argument
C-u, it will open another window.
As for your idea of getting the next .h file, you're just limiting
yourself by trying to bring a bad idea from IDE into Emacs.
Use projectile or ido-switch-buffer or helm-buffers-list: these
tools can manage hundreds of files at a time, while the approach of "getting the next file"
(i.e. tabs) fails around 20.

In Emacs, is there a command to visit the most recently opened file?

In Emacs, is there a command to open the most recently opened file? Like a visit-most-recent-file?
Note that I don't want to view a LIST of recent files and then select one. I want to automatically visit the most recently opened one.
It would be great to visit a recently-opened file (that's no longer in a current buffer) with a single keystroke. And then be able to visit the next most recently opened file by invoking the keystroke again, and so on.
So that by pressing that keystroke four times (e.g. A-UP), I could automatically open the top four files on my recent files list.
I can kind of sort of do this already by browsing through recent files, by pressing C-x C-f UP RET, but it would be cool to do it in a single keystroke (e.g. A-UP).
Building off of pokita's answer,
(defun visit-most-recent-file ()
"Visits the most recently open file in `recentf-list' that is not already being visited."
(interactive)
(let ((buffer-file-name-list (mapcar 'buffer-file-name (buffer-list)))
most-recent-filename)
(dolist (filename recentf-list)
(unless (memq filename buffer-file-name-list)
(setq most-recent-filename filename)
(return)))
(find-file most-recent-filename)))
I haven't tested this very much.
You can do this by using the recentf package:
(require 'recentf)
(defvar my-recentf-counter 0)
(global-set-key (kbd "<M-up>")
(lambda ()
(interactive)
(setq my-recentf-counter (1+ my-recentf-counter))
(recentf-open-most-recent-file my-recentf-counter)))
You will probably face the problem that there are files in the "recent" list which you are not interested in (temporary stuff that was saved upon exiting Emacs). Use the recentf-exclude variable to exclude those files from the list of recently opened files.

Sublime Text 2's "Goto Anything" (or instant search) for Emacs?

I tried out Sublime Text 2 recently, and I found Goto Anything superbly useful for navigating source code (Ctrl-P file#symbol seems to work really well). Is there something similar for Emacs? Preferably something that just works, without a ton of custom elisp.
What I've tried so far:
I've seen Helm and Anything, but as far as I understand neither of them is capable of actual "instant" search (see edit below).
I've used multi-occur-in-matching-buffers, but it too seems unable to satisfy the "instant" criterion.
imenu / idomenu works well for single files, but doesn't work across files.
I currently use #2 and #3 together, as a poor substitute for Goto Anything.
If not an exact clone of Goto Anything, then I could make do with a naive instant search solution (one that searches for a given string across all open buffers and displays results dynamically). So that's acceptable too.
I use Emacs 24.2, so any v24-only elisp is also fine.
EDIT: I gave Helm another shot, at event_jr's suggestion, and I found that it does support instant searching across all open buffers. helm-multi-occur + helm-follow-mode comes surprisingly close to meeting my needs, the only minor issues being (at the risk of sounding nit-picky):
I haven't found a way to turn on helm-follow-mode automatically when I run helm-multi-occur. I have to invoke it manually with C-c C-f. Anyone care to take a shot at this with a snippet of elisp? (see edit #2 below)
it isn't "intelligent" like ST2's Goto Anything (i.e., it doesn't understand "symbols" in source code, like Goto Anything does).
EDIT #2: Now I've got most of Goto Anything, thanks to event_jr's answer below (and of course, thanks to Helm's creator, Thierry Volpiatto). I recommend it heartily to anyone looking for a similar feature. Below is the elisp I'm currently using:
;; instant recursive grep on a directory with helm
(defun instant-rgrep-using-helm ()
"Recursive grep in a directory."
(interactive)
(let ((helm-after-initialize-hook #'helm-follow-mode))
(helm-do-grep)))
;; instant search across all buffers with helm
(defun instant-search-using-helm ()
"Multi-occur in all buffers backed by files."
(interactive)
(let ((helm-after-initialize-hook #'helm-follow-mode))
(helm-multi-occur
(delq nil
(mapcar (lambda (b)
(when (buffer-file-name b) (buffer-name b)))
(buffer-list))))))
;; set keybindings
(global-set-key (kbd "C-M-s") 'instant-search-using-helm)
(global-set-key (kbd "C-M-S-s") 'helm-resume)
(global-set-key (kbd "C-M-g") 'instant-rgrep-using-helm)
Just use helm.
It is perhaps more configuration than you asked for, but once you get it
configured how you like, it should be quite comfortable. Very much like Emacs
;).
And you should file a bug with Thierry for getting some more newbie friendly
defaults. He is quite responsive with issues.
helm-multi-occur
Primarily multi-buffer interactive "occur" is provided through
helm-multi-occur. If you execute the command, you'll notice that you have
to pick some buffers first (use C-SPC to select from the list,
M-SPC to select all). Then you can enter your query at the next
prompt. It's easy to make your own version that skips the buffer selection
like so:
(eval-after-load "helm-regexp"
'(setq helm-source-moccur
(helm-make-source "Moccur"
'helm-source-multi-occur :follow 1)))
(defun my-helm-multi-all ()
"multi-occur in all buffers backed by files."
(interactive)
(helm-multi-occur
(delq nil
(mapcar (lambda (b)
(when (buffer-file-name b) (buffer-name b)))
(buffer-list)))))
helm-buffers-list
Often you don't care about the exact occurrences of the query string, but want a
list of all buffers that contain it.
helm-buffers-list has some tricks up its sleeve. The first symbol you
specify is filtering by major-mode, and you can use the "#" prefix to narrow
the list to buffers that contain a string.
To wit, "ruby #prompt" will show you a list of buffers whose major-mode
contains "ruby" and whose contents contains "prompt". Or you can just use "#prompt" to show all buffers that contain "prompt".
Powerful and comfortable once you get used to it.
EDIT modified my-helm-multi-all to enable helm-follow-mode.
EDIT 2 update helm-follow-mode code to reflect helm changes.
EDIT 3 updated again to reflect helm changes
Emacs has Projectile satisfy your need:
jump to a file in project
multi-occur in project buffers
Heml is far from the fuzzy searching of ST3.
Fiplr looks promising but doesn't work on my laptop (see first issue on the github)
Simp.el looks like Fiplr but doesn't work either on my end.
Projectile works for me! Here's your solution!
I used also ido-mode and flx-ido for the fuzzy searching,
and for the vertical way of displaying results I use this in my .emacs:
;; Display ido results vertically, rather than horizontally
(setq ido-decorations (quote ("\n-> " "" "\n " "\n ..." "[" "]" " [No match]" " [Matched]" " [Not readable]" " [Too big]" " [Confirm]")))
(defun ido-disable-line-truncation () (set (make-local-variable 'truncate-lines) nil))
(add-hook 'ido-minibuffer-setup-hook 'ido-disable-line-truncation)
(defun ido-define-keys () ;; C-n/p is more intuitive in vertical layout
(define-key ido-completion-map (kbd "C-n") 'ido-next-match)
(define-key ido-completion-map (kbd "C-p") 'ido-prev-match))
(add-hook 'ido-setup-hook 'ido-define-keys)
Icicles offers some features that are similar to what it seems you are looking for.
C-x b and C-x C-f, to choose buffers or files, allow multi-completion: you can type a pattern to match the buffer/file name and/or a pattern to match content in the buffer/file. Candidates are filtered incrementally as you type (what you call "instant" is what Emacs calls "incremental"). You can refine either or both search patterns progressively, narrowing the choices in different ways. You can visit any number of buffers/files that match, at the same time. You can also use the same method to search the marked files in Dired: C-F.
C-c `(icicle-search) incrementally searches across multiple buffers or files. Again, progressive refinement etc.
The main difference between #1 and #2 is this:
For #1, you just want to find matching buffers or files. You don't care immediately about finding particular occurrences --- any match suffices.
For #2, you provide the buffers or files to search, and you want to navigate among search hits.
You can also use #1 to locate the buffers and files you want, then search their contents: The content-matching pattern you last used is available as the search pattern for Isearch (C-s).
for emacs I customize and modify this solution (for use install helm):
(defun helm-occur-from-point (initial-value)
"Invoke `helm-occur' from point."
(interactive)
(let ((input initial-value)
(bufs (list (buffer-name (current-buffer)))))
;; (isearch-exit)
(helm-occur-init-source)
(helm-attrset 'moccur-buffers bufs helm-source-occur)
(helm-set-local-variable 'helm-multi-occur-buffer-list bufs)
(helm-set-local-variable
'helm-multi-occur-buffer-tick
(cl-loop for b in bufs
collect (buffer-chars-modified-tick (get-buffer b))))
(helm :sources 'helm-source-occur
:buffer "*helm occur*"
:history 'helm-grep-history
:input input
:truncate-lines t)))
(defun get-point-text ()
"Get 'interesting' text at point; either word, or region"
(if mark-active
(buffer-substring (mark) (point))
(thing-at-point 'symbol)))
(defun helm-occur-1 (initial-value)
"Preconfigured helm for Occur with initial input."
(helm-occur-from-point initial-value))
(defun bk-helm-occur ()
"Invoke helm-occur with initial input configured from text at point"
(interactive)
(helm-occur-1 (get-point-text)))
(global-set-key (kbd "M-s-o") 'bk-helm-occur)
primary it based on
#see https://news.ycombinator.com/item?id=6872508 but on last helm versions not work but fixed with my changes (just copy/paste from some internal helm modules)

Can I change Emacs find-file history?

When I call find-file to open a new file, it generally happens that the file I'm looking for is in one of the directories I've already loaded from.
Ideally, I'd like to scroll through the history using the up/down arrows.
The problem with this is that if I've already loaded 10 files from a directory, I first have to pass through those ten files, all in the same directory, before I see a new directory where my file might be.
In the end, I often just type in the directory again or cut/paste it from an xterm.
in the find-file command, can I change the behavior of the up/down arrows to iterate over directories instead of files.
Alternatively, can I change the file order to match the order of buffers most recently visited instead of when I loaded the file?
My first answer suffered from the behavior that TAB completion no longer worked as expected in 'find-file. But the technique still seems useful (and if you like it, preferable).
However, this solution has the same history behavior, but maintains the TAB completion as expected inside 'find-file.
I'd be interested to know if there were a way to avoid advising find-file, but I couldn't find any introspection that gave me the knowledge that 'find-file was called.
(defadvice find-file (around find-file-set-trigger-variable protect activate)
"bind a variable so that history command can do special behavior for find-file"
(interactive (let (inside-find-file-command) (find-file-read-args "Find file: " nil)))
ad-do-it)
(defadvice next-history-element (around next-history-element-special-behavior-for-find-file protect activate)
"when doing history for find-file, use the buffer-list as history"
(if (boundp 'inside-find-file-command)
(let ((find-file-history (delq nil (mapcar 'buffer-file-name (buffer-list))))
(minibuffer-history-variable 'find-file-history))
ad-do-it)
ad-do-it))
I suggest IDO. You can search in the buffer list or in find-file. When searching in find-file and it has no matches in the current folder it searches through history.
not what you want, but
have you tried (electric-buffer-list) Ctrl-x Ctrl-b?
or (dired)?
This will use the buffer-list's order for the history, which is what you want.
(setq read-file-name-function 'my-read-file-name)
(defun my-read-file-name (prompt dir default-filename mustmatch initial predicate)
(let ((default-directory dir)
(files (directory-files dir))
(history (delq nil (mapcar 'buffer-file-name (buffer-list)))))
(completing-read prompt files predicate mustmatch initial 'history)))
Hmmm... This changes the behavior of find-file's TAB completion because the TAB completes over the history (already opened files), and not over the files available in the directory you're specifying.
Getting both to work at the same time is a bit trickier...
With Icicles you can cycle among candidate file names, and you can sort them in many ways. You can access them in order of last use etc.
http://www.emacswiki.org/emacs/Icicles_-_History_Enhancements
http://www.emacswiki.org/emacs/Icicles_-_Sorting_Candidates
http://www.emacswiki.org/emacs/Icicles_-_File-Name_Input