Better defaults for emacs - 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.

Related

Inspecting Command History in Emacs Buffer

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.

Emacs / Slime Key Binding / Sending command to Swank Server

I'm familiar with scheme, but new to emacs (switching over from VIM) and elisp.
I know how to do the following:
make a simple key binding
C-c iwb = indent whole buffer
F2 = turns folding on/off
use slime from emacs
some basic keys, like C-x 2, paredit keys, some basic movement keys
I need help doing something a bit more advanced:
I want F3 to equal:
put emacs into C-x 2 mode
in bottom window, switch to "slime-repl" buffer
in the "slime-repl" buffer, send the command "(test/run)" <-- note, this is meant to be sent to the swank server, NOT to elisp
I realize it's terrible form to ask people to write a script for me; however, if anyone could do that, I would learn rather quickly from it. [And it would allow me to do more complicated types of scripting through studying your example.]
Thanks!
This is not exactly what you want, but should be a good starting point for further tweaking:
(defun slime-run-test ()
(interactive)
(slime-interactive-eval "(test/run)")
(slime-pop-to-buffer (slime-output-buffer) t))
(global-set-key (kbd "<f3>") 'slime-run-test)
I don't use slime, but assuming it uses comint-mode then I would think the following might do the trick:
(defun my-slime-test-run ()
(interactive)
(delete-other-windows)
(split-window-below)
(with-selected-window (next-window)
(switch-to-buffer "slime-repl")
(goto-char (point-max))
(insert "(test-run)")
(comint-send-input)))
(global-set-key (kbd "<f3>") 'my-slime-test-run)
There is probably a better way to do this, but hopefully that gives you a little insight into how you can write elisp functions to carry out tasks in the editor (and note how the function reads very much like a set of editor instructions -- you can do a lot simply by converting the keystrokes you would use into equivalent code -- or even not writing code at all, and simply recording & saving keyboard macros).
Use C-hf name-of-the-function RET to get documentation on any of the function/macro calls in that function.
For the keybinding, I used C-hkF3 to check how Emacs referred to that key, and then used that string as the argument to kbd (and note how you can use that sequence to find the name of the function bound to any given key sequence, which you can then utilise in code if desired).
Many things are far less obvious if you don't already know them, but that's only to be expected with a code base as large as this (and dating back as long as this).
The great thing is that if you don't know what you're looking for, you can always search for function names matching patterns with C-uC-ha (and similarly for variables, values, libraries, and documentation; see M-: (info "(emacs) Apropos") RET for more about this facility). Plus the info manuals (complete with indexes -- press I or i within any particular manual, or use the info-apropos command to search all info manuals at once).
Truly one of the very best things you can do is to learn how to use the self-documenting nature of Emacs to find answers to the things you don't already know.

Debugging emacs LaTeX config file without trial-and-error binary search

I have an extensive emacs configuration. Unfortunately, auto-fill-mode is broken within LaTeX-mode for some reason. How can I debug this without binary searching my emacs configuration for the error?
Alternatively, how can I make this function not set an undo point? It's annoying to have to press undo once per word and space in the document.
(local-set-key (kbd "SPC")
(lambda () (interactive) (fill-paragraph)
(insert " ")))
Well, you can try M-x debug-on-entry auto-fill-mode.
But my advice would be to use do the binary search you're trying avoid. There is nothing faster. My init setup is probably at least as extensive as yours, and I sometimes think there is a quicker way to guess what the problem is, and time and again I've been taught the lesson that binary search is the way to go. It just seems slow and silly at first.
Remember the parable of the wise man who convinced a ruler to pay him with one grain rice on the first chessboard square, 2 on the second square, 4 on the third, and so on. It really doesn't get any better than binary search when you have no idea where the problem is, and even often when you think you can guess the general location of the problem. Just do it.

Refactoring a big file in emacs

I'm refactoring a big piece of code in one file in Emacs.
What is the best way to simplify jumping to several places in a big emacs buffer?
Currently I'm using search (C-S) and custom comments - "markers".
This becoming quiclkly unreliable.
Ideally I would like to have the same file open in several buffers, so I can switch between them using C-X B.
What are your solutions?
It seems that what you are looking for is Indirect-Buffers.
Personally, I find that splitting my window (C-x 2) is a great help.
Also bookmarks come to mind.
See Emacs Bookmarks.
Going to a particular bookmark switches to the correct buffer automatically (a bookmark is associated to a buffer).
Registers are useful for marking and jumping to positions. If you only have a small number of spots to mark and remember at any time, it may be faster to use single-character registers than named bookmarks.
C-xrSPC runs point-to-register
C-xrj runs jump-to-register
When prompted for the register, you can type any character.
Use autonamed bookmarks. No need to specify a name each time you create a bookmark -- just hit a key. Like using C-SPC to set a mark, but bookmarks are (by default) persistent; marks are not.
With Bookmark+ you can also organize bookmarks (including autonamed bookmarks) into sets etc. Tag them in various ways, for instance.
Visual bookmarks is extremely useful for this case. You can get it from here or install it using
M-x package-install RET bm
and add this to your config.
(global-set-key (kbd "<f5>") 'bm-toggle)
(global-set-key (kbd "<f7>") 'bm-next)
(global-set-key (kbd "<f6>") 'bm-previous)
Now, any where in your file press f5 and it creates a mark there. If you want to remove it, just press f5 again.
You can create any number of markers and now using f6 & f7 you can quickly go to any point you want.

Emacs define-key, Viper-mode key binding

I'm trying to learn emacs, getting vi custom key bindings.
Using Viper-mode, what is the correct way to re-bind a key? (I'm using Colemak keyboard layout(instead of qwerty) so have to change things like n->j) But would like it to work in viper-mode.
From this key binding guide on GNU.org:
http://www.gnu.org/software/emacs/manual/html_node/viper/Key-Bindings.html
It says the command to put in your .viper file is:
(define-key viper-vi-global-user-map "\C-v" 'scroll-down)
It doesn't work for me... in fact not sure I even have the function "define-key"...
M-x define-key [No match]
I'm not sure if 'define-key' is available on my version of emacs?
This works, but not in viper-mode
(global-set-key "n" "j")
Any help would be much appreciated. This is my first day using Emacs, to it's a pain getting Colemak & Viper-mode to work properly.
Thank for any help...
Hopefully some useful answers here:
First, having that line in the .viper works for me. Note that the viper-vi-global-user-map applies when you're in command mode, not insert mode.
Secondly, define-key isn't a command, it's a regular function, which just means that it cannot be called using M-x. See this Emacs wiki page for a little more detail on that distinction. But that was a good attempt.
Third, the global-set-key is a command, you could have tried making a change using M-x global-set-key. But, that sets the key in the current global map, which isn't the same as viper-vi-global-user-map. Viper-mode uses a bunch of different keymaps to make Emacs behave like vi, but all of the maps are overlaid on top of the global map.
I'm guessing that you found that C-v wasn't bound like you want when you're in insert mode. And that can be solved by adding this to your .viper:
(define-key viper-insert-global-user-map "\C-v" 'scroll-down)
Lastly, scroll-down may not be what you want. The down refers to the text moving down (given the perspective of a fixed window). C-v is generally bound to 'scroll-up. But,maybe it is exactly what you want.
Caveat: I'm not a viper-mode user, I don't even know how to use vi. So my terminology may be off. But I find the challenge of changing things in viper-mode very interesting.
Edited to add
From your comment it sounds like you want n to be the same as what j is bound to by default. Try adding this:
(define-key viper-vi-global-user-map "n" 'viper-next-line)
In "normal" mode I did M-x describe-key j, which told me that j is bound to 'viper-next-line, and the above line will bind n to the same routine. Repeat for the rest of the bindings you want to shift around.
in modern times evil-mode is the vim emulation layer for emacs, and to tweak it for colemak, my https://github.com/wbolster/evil-colemak-basics package helps a lot.