How to enable a non-global minor mode by default, on emacs startup?


I want to enable rainbow-mode everytime I start emacs, rather than having to use M-x rainbow-mode.

I guess there is some command I put in my .emacs file.

I tried all of the following, but none of them worked:

(require 'rainbow-mode)   

(rainbow-mode initialize)


More generally, how do I load any mode/package automatically on startup?


rainbow-mode isn’t a global minor mode, so it needs to be enabled on a per-buffer basis.

I only use it for CSS, so I have:

(add-hook 'css-mode-hook 'my-css-mode-hook)
(defun my-css-mode-hook ()
  (rainbow-mode 1))

If you genuinely want it to be global, everywhere, you can easily define a global minor mode yourself:

(define-globalized-minor-mode my-global-rainbow-mode rainbow-mode
  (lambda () (rainbow-mode 1)))

(my-global-rainbow-mode 1)

You can add any arbitrary logic to that (lambda () (rainbow-mode 1)) function (which will be evaluated in every buffer) in order to decide whether or not to actually call (rainbow-mode 1) for a given buffer, so if you’re comfortable with elisp then you can easily extend this approach to cover your specific requirements for the mode in question.

More generally, how do I load any mode/package automatically on startup?

It can vary, but the approaches I’ve shown would suffice for most minor modes: Either you want them enabled whenever MODE is enabled (being some specific other mode name), in which case you can use the MODE-hook variable (which will always be available) as per the css-mode-hookexample; or else you want the mode enabled permanently, in which case a global minor mode is a good approach (because you can toggle it on and off globally). Some minor modes are global by default (or provide global variants), but you can create your own if necessary, as per the my-global-rainbow-mode example.

Also be aware that modes can be derived from other modes, in which case all relevant MODE-hookhooks will be run (for details see A common use-case is to use prog-mode-hook to enable functionality wanted for all the programming modes which are derived from it (which is most programming modes).

Remember that many (hopefully most) libraries and packages will provide usage instructions. If you can’t find documentation, be sure to try M-x find-library to visit the library file, and then read through the comments at the top. There is often a very informative “Commentary” section, and sometimes this is the primary source of end-user documentation, and explain how to enable its functionality.


Unset key binding in emacs


The general way to unbind a key (for any keymap) is to define a binding of nil:

(define-key KEYMAP KEY nil)

For convenience, there are also two standard functions for unbinding from the global keymap and from the local keymap (which is usually the major mode’s keymap).

(global-unset-key KEY)
(local-unset-key KEY)

Those ones are interactive commands, as per their respective complements global-set-key and local-set-key.

As to your specific example, you probably want something like this:

(eval-after-load "zencoding-mode"
  '(define-key zencoding-mode-keymap (kbd "C-j") nil))

How to make occur mode select the window of buffer `*Occur*`?


Is it possible to make the occur mode grab the cursor (point)?, right now when I do M-x occur and search anything in a buffer, it will open the new buffer with the findings but the cursor stays in the buffer instead going to the findings.

I have to do C-x 0 to go to the other buffer everytime I do a search.

I tried doing it with a blank init.el just in case is helm or any other package, but I haven’t been able to make it focus the cursor on the new buffer.

( I know that there’s the helm-occur that does that, but is it possible with any command or function to make the cursor change to the active buffer when that command opens a new buffer? )

shareimprove this question

Occur has a hook, occur-hook, that contains a list of functions to run after a match is found. So we can add a hook to jump to the *Occur* window there:

(add-hook 'occur-hook
          '(lambda ()
             (switch-to-buffer-other-window "*Occur*")))

How to adjust dired-omit-mode



  • When dired is opened by default hidden files should be hidden.
  • When the user clicks “M-q” hidden files must be shown except for “.” and “..”

I tried the following:

;don't show hidden files
(setq dired-omit-files "^\\..*$")
(defun swap-omit-files ()
  "Show/hide hidden files except . and .."
  (if (string= dired-omit-files "^\\..*$")
      (setq dired-omit-files "^\\.\\.?$")
    (setq dired-omit-files "^\\..*$")

(add-hook 'dired-mode-hook
          (lambda ()
            (setq dired-omit-mode t)
            (define-key dired-mode-map (kbd "M-q") 'swap-omit-files)
Hidden file are indeed hidden by default, but something is wrong with swap-omit-files --- the hidden files are not shown.

How to fix?

shareimprove this question


This worked. We needed to revert-buffer after changing dired-omit-files:

(defun swap-omit-files ()
  "Show/hide hidden files except . and .."
  (if (string= dired-omit-files "^\\..*$")
      (setq dired-omit-files "^\\.\\.?$")
    (setq dired-omit-files "^\\..*$")
shareimprove this answer

How do I byte-compile everything in my .emacs.d directory?


C-u 0 M-x byte-recompile-directory

will compile all the .el files in the directory and in all subdirectories below.

The C-u 0 part is to make it not ask about every .el file that does not have a .elc counterpart.


Add change to a previous commit with Magit


I have 2 commits, A then B, ready to be pushed. I realize I forgot to add something in A.

How can I add this change to A using Magit? I don’t even know which part of the Git documentation I should look at.

shareimprove this question

Let’s pretend for a moment that you want to add something to the HEAD commit, i.e. “the second commit B” in your example.

The commit popup on c features a binding “a Amend”. Pressing that key will “amend” the staged changes to the HEAD commit. Since commits are not mutable in Git, this will actually replace the old commit with a new commit. A buffer with the old commit message will pop up, so that you can modify it in case the added change also requires that you adjust the message. As always, press C-c C-c when you are done editing the message. This is equivalent to running git commit --amend on the command line.

  • a Amend — add the staged changes to HEAD and edit its commit message

Because it often happens that you only have to adjust the change or the message, Magit provides two additional variants:

  • e Extend — add the staged changes to HEAD without editing the commit message
  • w Reword— change the message of HEAD without adding the staged changes to it

When you want to edit a commit that isn’t HEAD, then the above won’t work. These commands always “modify” (i.e. replace) the HEAD commit. Git doesn’t provide a single command for modifying a commit other than HEAD so this is a bit more involved.

Magit does provide such a command, but because there are situations in which it is preferable to do this in multiple steps, we will discuss that first.

Modifying a commit other than HEAD can be broken down into three steps:

  1. Temporarily make that other commit (A) the HEAD.
  2. Modify the HEAD (as described above), resulting in commit A'.
  3. Tell Git to reapply the commits that followed A, but on top of A'.

This can be done using an interactive rebase. Type r to show the rebase popup. Then type mto invoke the “edit a commit” rebase variant. A buffer with recent commits appears. Move to the commit you want to modify and type C-c C-c to select it. Git then rewinds history to that commit and shows information about the ongoing rebase in the status buffer.

Modify HEAD as described above. Then tell Git that you are done by typing r r. If A' and Bconflict then rebase will stop at B and you have to resolve the conflict. After you have done so press r r to continue.

If you know that your changes to A will result in conflicts with B, then proceed as describe above, otherwise use the following approach.

Git allows creating “fixup commits” using git commit --fixup A. This creates a new commit, which records changes which “should have been made in another commit”. That commit becomes the new HEAD. There also exists a --squash variant. For information about the differences see the git-commit man page.

To actually combine the A commit and the new commit A' and then reapply B on top of that you have to use rebase. Magit provides a convenient command for doing so on r f.

The main difference to the above approach is that here we first create a new commit and then we rebase to combine that with the “target” and reapply B. Above we began with rebasing instead of committing.

In Magit both the --fixup and the --squash variants are available from the commit popup, on fand s. But Magit also provides “instant” variants of the fixup and squash commands on F and S. These variants create a new commit like the “non-instant” variants, but then they instantly combine the fixup commit with the target commit using rebase, without you having to invoke another command.

“Instant fixup” (c F) is essentially the same thing as “extend HEAD” (c e), except that it works for any commit, not just HEAD.

Further reading:

shareimprove this answer

After enabling ivy-mode, `dired-create-directory’ does not allow me to choose an entry not in the completion list


Yes, obviously.

There are 2 cases. The name you want to create doesn’t match anything in the completion list, or when it does.

When it doesn’t, you can just do the totally obvious thing, type in the name and press enter.

If it does match, then pressing enter will get ivy to expand the name to the completion. Here you want to call ivy-immediate-done which is bound to C-M-j by default after you have typed the name.

shareimprove this answer