Inspecting Command History in Emacs Buffer - emacs

I am wondering if there is any way to inspect all commands stored in the Emacs history. That is, suppose I am using SLIME for Common Lisp development. In SLIME's REPL, I can press M-p and M-n to navigate through command history.
Quite often, the history becomes extensive, and one would like to easily access commands from much earlier. What is a way to achieve that? What is the most optimal way to achieve that aside from scrolling through commands or through the buffer?

helm is awesome for a lot of tasks, concerning history there are these commands:
helm-eshell-history
helm-projects-history
helm-minibuffer-history
helm-reset-adaptive-history
helm-complex-command-history
In peculiar I use M-x helm-complex-command-history quite often. I hope that you can use these commands in your context.
Extra notes:
Not directly related to your problem, but maybe useful.
helm:
If you do not use helm yet you can start by rebinding M-x by helm-M-x and C-x b by helm-buffers-list
(global-set-key (kbd "M-x") 'helm-M-x)
(global-set-key (kbd "C-x b") 'helm-buffers-list)
to see how it is helpful.
configuration (history stored between sessions)
You can store extra history and reuse it even if you restart Emacs by putting something like:
(savehist-mode 1)
(setq savehist-additional-variables
'(compile-command kill-ring search-ring regexp-search-ring))
in your .emacs file.

Related

Better defaults for emacs

I've been using Vim for a several years. And now I want to give a try to Emacs.
For Vim I have a general config file (here) where I'm overriding defaults (e.g. hey, Vim, show me the line numbers; save more history, don't create these stupid backup files, etc...)
I want the same thing for Emacs. While searching, the best thing I've found is better-defaults.el from technomancy. I'm still digging in Prelude and Emacs-Starter-Kit sources, but there are too many overrides and plugins.
So, what I want:
ability to see a list of variables, which I can customize (e.g. indent-tabs-mode or newline-and-indent). I know about C-h v variable-name but this command requires me to know a name of variable, but I want a list of them
sample config file for Emacs which sets helpful defaults with comments for each command
For your first question: M-x customize-option.
C-h v TAB is not what you want, as it shows you also non-option variables (e.g., internal variables).
However, if you load library help-fns+.el then C-u C-h v TAB shows you only the user options (in buffer *Completions*).
My advice would be to not look for an existing "sample config file", if you intend to start with it, as opposed to just seeing how another user redefines things. And for help with the latter, I would still recommend the Emacs manual over looking at someone elses init file. Especially to start with.
However, if you really want to look at init files from other users then this is the place to start. (And this is a good place to start, other than the manual (which is the best place), to learn about customizing Emacs.)
Finally, my (unsolicited) advice wrt learning Emacs, including customizing, is to start by not customizing it at all. I say that without irony as one who has heavily customized Emacs.
If you want to "get it", i.e., to get a feel for the Emacs design and what makes it different, then let yourself get used to Emacs as it is out of the box -- for maybe a month or so. At that point you can think about customizing, and your customizations are likely to be much wiser (in your own terms, i.e., for whatever it is that you want).
Another way of putting this is that until you know Emacs a bit, you really do not know what it is that you want or need in terms of customization. In particular, it would be a mistake, IMO, to start out by trying to think of Emacs in terms of Vim or trying to make Emacs do what you've done in Vim. There is plenty of time for that later, if, based on understanding Emacs, you really do want to do that.
Welcome to Emacs. Enjoy.
I'm going to take a reasonable dissent from Drew's excellent answer, there are some things you really ought to set in your emacs-file immediately, that aren't set out of the box that you really ought to set.
Issue number 1: THAT $(generate-swearing) BELL!
The bell will ding like a madman. That's annoying. You can turn it off.
In your init-file, do this:
(setq visible-bell 1)
Issue number 2: Emacs has an interesting view of backup files.
If you edit a file, say "foo.txt", emacs will create little backups of the file with the name "foo.txt~" in the same directory.
This is annoying as all hell, and you can fix it by doing this:
(setq backup-directory-alist '(("" . "~/.emacs.d/emacs-backup")))
Issue number 3: Emacs uses C-w differently than bash does, and that's a bit annoying.
C-w usually deletes a word backwards. By standard in emacs, it deletes the marked region. That's a bit silly.
It is better to do something like this:
;; This is my preference, your mileage may vary.
(global-set-key (kbd "C-x C-k") 'kill-region)
(global-set-key (kbd "C-x k") 'kill-buffer)
(global-set-key (kbd "C-w") 'backward-kill-word)
Issue number 4: Alt-X is a clunky way of running an interactive command.
It is better to do something like this instead, avoid your hand cramping up all the time.
(global-set-key (kbd "C-x C-m") 'execute-extended-command)
(global-set-key (kbd "C-x C-m") 'execute-extended-command)
You also may want to check out Steve Yegge's Effective Emacs: https://sites.google.com/site/steveyegge2/effective-emacs
It's pretty amazing. One thing to note though is that the caps-lock to ctrl thing is also available through a microsoft tool here:
https://learn.microsoft.com/en-us/sysinternals/downloads/ctrl2cap
This is better than the manual hack Yegge suggests, and you can turn it off if you don't like it.

Shortcut to open a specific file in Emacs

I want to be able to use a keyboard shortcut to edit my .emacs file instead of typing Ctrl-XCtrl-F.emacsEnter every time (here's an analogous question regarding Vim). There's probably an obvious way of doing this, but I can't find the Emacs Lisp command to open a file. I'd think it would be something beginning with "open" or "file" but typing M-x and those terms doesn't seem to bring up anything relevant. I tried
(global-set-key (kbd "<f6>") (find-file "~/.emacs"))
but that doesn't work.
According to the documentation
(global-set-key KEY COMMAND)
Give KEY a global binding as COMMAND. COMMAND is the command
definition to use; usually it is a symbol naming an
interactively-callable function.
So you have to use an interactively-callable function:
(global-set-key (kbd "<f6>") (lambda() (interactive)(find-file "~/.emacs")))
Personally I prefer to use emacs registers to store files which I use often.
I would store '~/.emacs' in a register:
(set-register ?e (cons 'file "~/.emacs"))
and open it with C-x r j e
Bookmarking is an excellent solution for this purpose that is packaged with emacs.
That way if it's ok for you to see the list of files that you want to open, you can easily browse them. There is also BookmarkPlus which offers variety of options.
M-x list-bookmarks

How to bind Emacs key for delete whole word backword as the same in a shell?

I have just started learning Linux and Emacs. It was pleasant to have the same key bindings in both Emacs and a shell (bash/tcsh) for the most frequently used cursor movements, so that I do not have to consciously think which one I have to use. Still worse, use the wrong command and undo the mistake. There were two exceptions, though.
One often used command was the equivalent of backspace, delete a character backwards. In a shell, it was C-h. I got the same behaviour in Emacs, thanks to this tip from Janos, who probably felt the same way.
http://www.math.rutgers.edu/~komlos/emacs.htm
Now the mistake I do often in Emacs is trying to delete words backwards with the command M-C-h, as in a shell.
Can somebody please give a binding that will make Emacs delete words backwards with 'M-C-h'? Currently, this command selects the whole of the text in a buffer, which is quite an useful thing (C-a in windows), but not so frequently used as deleting words backwards.
Moreover, any binding to replace the current binding of M-h (from the link above) to help will be appreciated.
Thank you,
Elan.
Below binds C-M h to backward-kill-word. You can put it in your .emacs file.
(global-set-key (kbd "C-M-h") 'backward-kill-word)
You can use M-<backspace> in terminal and emacs to delete word backward.
It's best to use key translation so C-M-h works exactly the same as M-backspace would in any minor mode (regardless of whether M-backspace is bound to backward-kill-word or not).
;; bind C-h to <backspace>
(define-key key-translation-map [?\C-h] [?\C-?])
;; bind C-M-h to M-<backspace>
(define-key key-translation-map [?\C-\M-h] [?\C-\M-?])

How to switch between visible buffers in emacs?

If I am in split-screen viewing 2 different buffers on Emacs and the cursor is on the top buffer, what's a quick way to move the cursor to the bottom buffer?
Bonus question: if I know a command, is there an easy way to identify what key-combo it's bound to, if any?
To switch to other buffer use: C-x o.
Describe key: C-h k.
Here is a better solution when you open more than two windows(buffers) in one frame:
(global-set-key (kbd "C-x <up>") 'windmove-up)
(global-set-key (kbd "C-x <down>") 'windmove-down)
(global-set-key (kbd "C-x <left>") 'windmove-left)
(global-set-key (kbd "C-x <right>") 'windmove-right)
Now, you can use C-x UP/DOWN/LEFT/RIGHT to go to the above/nether/left/right buffer when you have three or more in one frame, they are more precise than 'other-window and you don't need to install any package.
You even CAN make it to cycle the buffers in the direction(vertically/horizontally) with one of the above shortkeys with configuration in .emacs/init.el file, but I don't recommend it(besides I don't remember it anymore, you can google it if you want).
Of course, you can use other shortkeys other than the ones I use in my .emacs.
You may also be interested in WindMove, which enables "directional" window navigation with <S-up>, <S-right> etc.
With respect to the bonus question, if you know the command (other-window), and you invoke it with M-x other-window, Emacs will show a brief message in the minibuffer stating: "You can run the command `other-window' with C-x n".
There is also M-x where-is which prompts for a command and gives you the current bindings that result in that command (if any).
There is a tutorial that's shipped with Emacs. It actually has the answer to your question (see the section MULTIPLE WINDOWS about 80% into the tutorial). The tutorial can be accessed via C-h t, or M-x help-with-tutorial. There's also a link to the tutorial on the initial splash screen of Emacs. Right below the link to the tutorial is a link to the on-line Emacs Guided Tour. The tutorial walks you through basic editing/movement commands, the guided tour is more of an introduction to what Emacs has to offer.
If you want to navigate among only buffers that are currently displayed, then you really want to navigate among the windows they are displayed in. This gives you a way to do that, using window/frame names that are the same as the buffers:
See Better window navigation in Emacs?

How to do use C-x k to kill an Emacs buffer opened in server mode?

I use windows batch file to open files in an already-running instance of Emacs using emacsclientw.exe. However, any file opened that way is opened in server mode, which means that I have to use C-x # to kill it, instead of the usual C-x k. How do I change this behavior?
My solution was to rebind it (well M-w actually) to:
(lambda ()
(interactive)
(if server-buffer-clients
(server-edit)
(kill-this-buffer)))
[edit: having now read the code for server-edit, it might be better to use server-done (depending on what you want). server-edit will switch you to a server-edited buffer (if any still exist) but server-done will simply switch you to the next buffer. You could also use the output of server-done to see if the buffer was actually killed (not the case if the file was open before invoking emacsclient) and then kill it if not. Or use server-kill-buffer as suggested elsewhere.]
Here is what i put in my .emacs to do this :
(add-hook 'server-switch-hook
(lambda ()
(local-set-key (kbd "C-x k") '(lambda ()
(interactive)
(if server-buffer-clients
(server-edit)
(ido-kill-buffer))))))
Like this C-x k work the usual way when i'm not finding a file from emacsclient (which for me is ido-kill-buffer), and if i'm using emacsclient, C-x k does server-edit if the client is waiting, or run ido-kill-buffer otherwise (if i used emacsclient -n).
use:
D:\> emacsclientw -n foo.txt
the -n says "no wait". This is in GNU Emacs 22.2.1 (i386-mingw-nt5.1.2600) of 2008-03-26 on RELEASE (and many prior versions, IIRC).
You know, I hate to suggest workarounds instead of a real solution... but after reading the server code, I am confused as to how emacs even determines that a buffer is a server buffer.
With that in mind, why not open up files like:
emacsclient --eval '(find-file "/path/to/file")'
This way emacs doesn't know that your buffer was opened via emacsclient, which sounds like what you really want.
Edit:
I'm not really happy with this, but it seems to work:
(global-set-key (kbd "C-x k") (lambda () (interactive) (server-kill-buffer (current-buffer))))
Okay, THIS did work for me:
(global-set-key (kbd "C-x k") '(lambda ()
(interactive)
(if server-buffer-clients
(server-done)
(kill-this-buffer))))
(this is the code from IvanAndrus's answer with the explicit changes from edits and comments, and using jrockway's keybinding.)
And, yes -- I am rebinding a standard keybinding. BUT it's a functional replacement NOT something completely different (eg, replacing kill-ring-save with kill-buffer stuff).
BTW, EmacsWiki has a couple of pages on this topic-- KillKey and KillingBuffer -- neither of which shed better light for me than the above (although the first-function on KillKey also used "server-edit"...).
I am not sure if that will work on windows, but on linux, emacs -daemon is just great.
With it, you don't have to run a different program, and your bindings are the same. I guess there are other advantages, but since I could never learn those emacsclient bindings, I never really used it, and can't say.
I don't think -daemon had been released yet, I am using 23.0.60.1 from CVS.