How can I operate on more Emacs buffers at the same time? - vim

I am looking for an equivalent of the :bufdo Vim command in Emacs. :bufdo takes an argument - another command - and executes the command on all open buffers. I have not yet found a similar feature in Emacs.

Depending on what your command is, you can do:
M-: (mapc (lambda (b) (set-buffer b) (*command*)) (buffer-list))
But, I have a feeling you want something not so lispy. Take a look at keyboard macros. Namely, decide what you want to do:
C-x ( <do-your-command> C-x )
M-: (mapc (lambda (b) (set-buffer b) (kmacro-end-and-call-macro)) (buffer-list))
You'd probably want to define that last part as a function if you use it much:
(defun bufdo ()
"execute last macro on all buffers, ala bufdo from vi"
(interactive)
(mapc (lambda (b)
(with-current-buffer b
(kmacro-end-and-call-macro)))
(buffer-list)))
Note: code is untested

You can also checkout ibuffer, it allows you to mark buffers you like to operate on with m and then you can execute something on it with E. Other common operations are also available, e.g. query-replace on Q. Just check out the menu or the mode description (C-h m).
BTW, similar things are also possible from dired, although it doesn't seem to give you an eval command.

Take a look at buffer-list (function). It returns a list of all the open buffers (BUFFER objects). See the manual for a simple example of using it with mapcar (which operates on every element of the list, and accumulates the results). You would probably also find set-buffer, which programatically sets the current buffer from Emacs Lisp, useful.

Related

emacs run shell command against current buffer's file name [duplicate]

I can run a shell command quickly by hitting M-!. One thing I'd like to do is perform shell quick operations on the current file. An example would be checking the file out through perforce:
M-! p4 edit buffer-file-name RET
(Yes there are perforce integrations, but I'm more interested in the minishell/variable problem rather than a specific workflow)
Of course, the buffer-file-name variable is not evaluated before the command is sent to the shell.
Is there an easy on-the-fly way to do this? Or will I have to roll a custom elisp function?
It seems current Emacs has something built-in to achieve the desired result, after M-! (shell-command) press <down>, you will get the file name you are currently visiting on the prompt. Now you can edit it to add the command you want to run on it.
In dired-mode it will give you the file your cursor is currently on.
Indeed using C-u M-: is almost right. I'm not so sure about using shell-quote-argument in eval-to-shell-argument since it only works on strings making it impossible to use eval-to-shell-argument to insert a number or a symbol. You could try something like:
(defun sm-minibuffer-insert-val (exp)
(interactive
(list (let ((enable-recursive-minibuffers t))
(read-from-minibuffer "Insert: "
nil read-expression-map t
'read-expression-history))))
(let ((val (with-selected-window (minibuffer-selected-window)
(eval exp)))
(standard-output (current-buffer)))
(prin1 val)))
and then bind this function in your minibuffer with (define-key minibuffer-local-map [?\M-:] 'sm-minibuffer-insert-val).
Of course, if the only thing you ever want to insert is the buffer-file-name, then your execute-shell-command-on-buffer is simpler.
I did roll my own elisp function, and it looks like this:
(defun execute-shell-command-on-buffer (shell-command-text)
(interactive "MShell command:")
(shell-command (format shell-command-text (shell-quote-argument buffer-file-name)))
)
https://gist.github.com/2367513
I bound it to M-", so now my example can be completed with:
M-"p4 edit %sRET
I won't accept this as the answer, because I did ask for solutions that don't require a function.
You can use C-u M-: (eval-expression with a universal prefix argument) to evaluate any Lisp expression and insert its value at point in the current buffer (including minibuffers, as long as you have enable-recursive-minibuffers set to a non-nil value).
In your example: C-u M-: buffer-file-name RET.
Note that the result of the expression is printed in Lisp form: that is, quoted in such a way that a subsequent call to read would construct an equal Lisp value. For strings, this means enclosing in double quotes, which will probably be interpreted as you expect by the inferior shell. However, you may run into problems with strings that contain special characters, which need different escaping by Elisp and the shell.
The more correct way uses shell-quote-argument, as in phils' solution. Here's a quick defun that reads a Lisp expression and inserts its value at point as a properly quoted shell word:
(defun eval-to-shell-argument (form)
(interactive "XEval: ")
(insert (shell-quote-argument form)))
The read-and-evaluate step happens automatically by using an "X" as the argument to interactive.
Edited to add: As #tenpn notes, the above solution doesn't work for inserting buffer-local variables like buffer-file-name in a minibuffer like the one M-! pops up (more precisely, it inserts the buffer-local value of the minibuffer, which is unlikely to be useful). Here is a revised version which seems to work. If the minibuffer is active, it makes the buffer of the previously-selected window temporarily active while reading and evaluating an expression.
Final edit: From #Stefan's answer I see that I should have used (minibuffer-selected-window) to find the previously-selected window. I've also added a (format "%s" ..) to allow inserting non-string values, while still quoting special characters in strings. Here's the final version:
(defun eval-to-shell-argument ()
(interactive)
(let* ((buffer
(if (minibufferp)
(window-buffer (minibuffer-selected-window))
(current-buffer)))
(result
(with-current-buffer buffer
(eval-minibuffer "Eval: "))))
(insert (shell-quote-argument (format "%s" result)))))
You can't do that with M-!, but you can evaluate arbitrary elisp from the minibuffer, so writing a function isn't strictly necessary:
M-: (shell-command (format "p4 edit %s" (shell-quote-argument buffer-file-name))) RET
In this case however, I think eshell is what you want to use:
M-x eshell-command RET p4 edit (eval buffer-file-name) RET
Edit: Except unfortunately that doesn't work, as the *eshell cmd* buffer is selected when that is evaluated. One solution would be:
M-x eshell-command RET p4 edit (eval buffer-file-name (other-buffer nil t)) RET
(Not quite as elegant, sorry.)
Everyone seems to be rolling their own version, so here's mine -- it will substitue the current filename or marked dired-files or current dired file wherever a % is in the shell command. It follows the same conventions as M-! so I bind it to that.
(defun my-shell-command (command &optional output-buffer error-buffer)
"Run a shell command with the current file (or marked dired files).
In the shell command, the file(s) will be substituted wherever a '%' is."
(interactive (list (read-from-minibuffer "Shell command: "
nil nil nil 'shell-command-history)
current-prefix-arg
shell-command-default-error-buffer))
(cond ((buffer-file-name)
(setq command (replace-regexp-in-string "%" (buffer-file-name) command nil t)))
((and (equal major-mode 'dired-mode) (save-excursion (dired-move-to-filename)))
(setq command (replace-regexp-in-string "%" (mapconcat 'identity (dired-get-marked-files) " ") command nil t))))
(shell-command command output-buffer error-buffer))

Emacs: Is it possible to list all matching lines for a certain query string for marked files in dired?

I found out M-x occur the other day.
(How to achieve code folding effects in Emacs?)
I wonder if I could list all matching lines in multiple files(or buffers) preferably marked in dired mode.
Files
This can be done using package noccur which can be installed from MELPA.
It provides two functions:
noccur-dired that will perform multi-occur on dired marked files
noccur-project that will perform multi-occur on all files in current project. This is recursive.
From documentation a typical usage is: M-x noccur-project RET foo RET
The occur buffer's content can then be edited with occur-edit-mode (bound to e). To save changes in all modified buffer and go back to occur-mode press C-c C-c.
Buffers
This can be done using the built-in ibuffer. Mark buffers with m key, then key O to launch ibuffer-do-occur on marked buffers. I personally activate ibuffer using (defalias 'list-buffers 'ibuffer) in my .emacs.
You can also use the built-in multi-occur-in-matching-buffers that will perform multi-occur on buffers matching a regexp. Typical usage is M-x multi-occur-in-matching-buffers RET ext$ RET regexp RET where ext$ is regexp for buffers already opened in Emacs, and regexp is what to match.
M-x multi-occur
M-x multi-occur-in-matching-buffers
and also:
M-x multi-occur-in-this-mode
(defun get-buffers-matching-mode (mode)
"Returns a list of buffers where their major-mode is equal to MODE"
(let ((buffer-mode-matches '()))
(dolist (buf (buffer-list))
(with-current-buffer buf
(if (eq mode major-mode)
(add-to-list 'buffer-mode-matches buf))))
buffer-mode-matches))
(defun multi-occur-in-this-mode ()
"Show all lines matching REGEXP in buffers with this major mode."
(interactive)
(multi-occur
(get-buffers-matching-mode major-mode)
(car (occur-read-primary-args))))
You may try
(defun dired-do-multi-occur (regexp)
"Run `multi-occur' with REGEXP on all marked files."
(interactive (list (read-regexp "Regexp: ")))
(multi-occur (mapcar 'find-file-noselect (dired-get-marked-files)) regexp))
Run it in a dired buffer with M-x dired-do-multi-occur or bind it to a key of your liking.
Warning: all marked files will be opened by emacs.
In Icicles, use M-s M-s m (command icicle-search-dired-marked-recursive) to search the marked files in the current Dired buffer and in all marked subdirs, recursively.
Similarly, in your bookmark-list display the same key, M-s M-s m, searches the targets of all marked bookmarks. And similarly for Ibuffer and Buffer Menu: M-s M-s m searches the marked buffers.
This is Icicles search, which is a different kind of incremental search (and on-demand replace). You can confine searching within particular contexts (defined by a regexp). The search hits are updated incrementally as you change your search pattern. You can combine multiple search patterns, to refine your search progressively. You can cycle through any set of search hits or access them directly. You can change cycle orders --- you are not limited to buffer-occurrence order.
http://www.emacswiki.org/emacs/Icicles_-_Search_Commands%2c_Overview
Let me improve on the answer of mk1 since I think it is the best one so far. This keeps the same history of previous searches of occur and allows for the optional argument for displaying more lines after or before the match (using C-u followed by a number before calling the function), just as in standard occur.
(defun dired-do-multi-occur (regexp &optional nlines)
"Run `multi-occur' with REGEXP on all dired marked files."
(interactive (occur-read-primary-args))
(multi-occur (mapcar 'find-file-noselect (dired-get-marked-files)) regexp nlines))
Wrt Dired:
Not sure whether you are asking (a) to regexp-search the files marked in Dired or (b) to mark files (in Dired) whose contents match a regexp. - or (c) something else.
You can do the former (search marked files) using A (or M-S a C-M-s for incremental regexp search). And this answer lets you search all files marked here and in marked subdirs (recursively).
You can do the latter (mark the files whose contents match) using %q (dired-mark-files-containing-regexp).

Better window navigation in Emacs?

When I split window in emacs several times (under terminal) the whole screen is divided into several parts with hierarchical arrangement. In vim it is easy to switch between windows intuitively with Control-w + (h,j,k,l), but in Emacs I can only use Control-x + o to switch to "the other window", which probably would require several iterations to finally get to the window I intend. I wonder if there is a better way similar to that in Vim to easily navigate between windows?
Have you tried WindMove? It comes bundled with Emacs 21+. You move around with Shift-up, Shift-down, Shift-left, and Shift-right, though you can change the modifier. From the docs:
;; Installation:
;;
;; Put the following line in your `.emacs' file:
;;
;; (windmove-default-keybindings) ; shifted arrow keys
;;
;; or
;;
;; (windmove-default-keybindings 'hyper) ; etc.
;;
;; to use another modifier key.
;;
;;
;; If you wish to enable wrap-around, also add a line like:
;;
;; (setq windmove-wrap-around t)
I find the default binding for other-window to be really tedious, too. I've defined the following in my .emacs:
(global-set-key [(control ?,)] (lambda () (interactive) (other-window -1)))
(global-set-key [(control ?.)] (lambda () (interactive) (other-window 1)))
Just find some easy-to-reach keybindings (I use a Dvorak layout, so C-, and C-. may not be as easy for you to reach), preferably right next to each other, to bind to those lambdas.
Also, I found the Emacs wiki a few months ago. Nifty Tricks has a nice list of ways to make Emacs easier to use!
In Icicles, by default C-x o is bound to the multi-command icicle-other-window-or-frame, which works this way:
With no prefix arg or a non-zero numeric prefix arg:
If the selected frame has multiple windows, then this is
other-window. Otherwise, it is other-frame.
With a zero prefix arg (e.g. C-0):
If the selected frame has multiple windows, then this is
icicle-select-window with windows in the frame as candidates.
Otherwise (single-window frame), this is icicle-select-frame.
With plain C-u:
If the selected frame has multiple windows, then this is
icicle-select-window with windows from all visible frames as
candidates. Otherwise, this is icicle-select-frame.
Well then, what are icicle-select-window and icicle-select-frame?
They are multi-commands that let you choose a window or frame to select by name. (You can bind them separately, if you want -- they each change their behavior based on their own prefix args.)
Window and frame names are taken from their displayed buffers, with [N] (N=1,2,...) appended if needed for a unique name if the same buffer is displayed in more than one window/frame.
Being multi-commands, you can choose by completing and/or cycling. Completion can be prefix, substring, regexp, or fuzzy.
http://www.emacswiki.org/emacs/Icicles_-_Multi-Commands
See switch-window. It will number windows to let you switch directly to the one you want.
That was also my first experience with emacs. But, using windmove, I can suite it, they way I want it. I use this as the modifier for windmove :
(windmove-default-keybindings 'meta)
I use ALT for the navigation of windmove

continuously execute an emacs lisp function

Is there a way to trigger the execution of an emacs lisp function other than M-x myfun? I would like to have the function re-called every time the buffer is changed.
Background: I have a table of numbers with some mistakes. The table has column totals and other features which can be used to identify the mistakes. My elisp function highlights suspicious columns of numbers. What I would like is for the highlighting to disappear as soon as the numbers are corrected, without repeated calling of the highlight-errors function.
The analogous feature in Excel is called, I believe, "conditional formatting"
The concept you're looking for in your first paragraphs is hooks. A hook variable is a list of functions that are executed when a certain event happens. Most hook variables have a name ending in -hook. The hook after-change-functions is executed each time you type something or otherwise change the buffer. Hooks are discussed in the Emacs Lisp manual under the heading "Hooks".
However, given what you're trying to do, it would be easier to use Emacs's highlighting mechanism. The solution may be as simple as adding a regexp in the right place.
Most files containing structured text (especially programming languages) are highlighted with the font locking mechanism. This is documented in both the Emacs and Emacs Lisp manuals under "Font Lock"; see in particular the function font-lock-add-keywords, for which the Emacs manual gives an example that is pretty much what you're after. There is also some information on the Emacs wiki.
ADDED:
Font lock can go beyond regexps; unfortunately the documentation is limited to the terse explanation in the docstring of font-lock-keywords. There are a few simple examples in cperl-mode.el (though they're somewhat buried in the mass). The wiki also references ctypes.el which uses this feature. Here is an example that highlights wrong integer additions.
(defun maybe-warn-about-addition ()
(let ((x (string-to-int (match-string 1)))
(y (string-to-int (match-string 2)))
(z (string-to-int (match-string 3))))
(if (/= (+ x y) z)
font-lock-warning-face)))
(font-lock-add-keywords
nil
'(("\\s-\\([0-9]+\\)\\s-*\\+\\s-*\\([0-9]+\\)\\s-*=\\s-*\\([0-9]+\\)\\s-"
(3 (maybe-warn-about-addition) t))))
Even the regexp can be replaced by arbitrary code that looks for the bounds of what you want to highlight (a function name as MATCHER, using the vocabulary from the docstring). There is an advanced example of font lock keywords in the standard C mode (cc-fonts.el).
Add your function to the variable after-change-functions.

Emacs equivalent of Vim's foldmethod = indent

Question: Does Emacs have a canonical equivalent of Vim's Folding with Foldmethod=indent?
I am particularly interested in something that can work alongside any Emacs major mode and any file. The Emacs searches have not turned up a definitive answer.
Seems like it can, though I don't use folding myself, so I've not tried it. Not surprisingly, the Python folks are all about this feature. See the following:
http://mail.python.org/pipermail/tutor/2002-September/017482.html
http://www.nabble.com/Code-folder-with-Emacs-td16189193.html
http://groups.google.ca/group/comp.lang.python/msg/956f1c2d37f93995
maybe selective-display? I have the following function bound to [f2]
;; http://emacs.wordpress.com/2007/01/16/quick-and-dirty-code-folding/
(defun jao-toggle-selective-display (column)
(interactive "P")
(set-selective-display
(if selective-display nil (or column 1))))
That's pretty bare-bones, though, and you'd really want it to be Pythony-indentation sensitive....
UPDATE: I was staring at this last night, and realized that I was tired of C-u entering the column I was on (plus 1).... so I coded it up:
(defun toggle-selective-display-column ()
"set selective display fold everything greater than the current column, or toggle off if active"
(interactive)
(set-selective-display
(if selective-display nil (or (+ (current-column) 1) 1))))
Further elaboration should combine the two functions.
See also: How to achieve code folding effects in emacs
I tried all of the suggestions by Joe Casadonte and Michael Paulukonis, but none works as nicely as the vim's one. So it seems that the more accurate answer to the OP's question may be NO at the moment.

Resources