Is it possible to make yasnippets to expand snippets inside an org-mode babel section in the appropriate mode?

[Origin]: https://emacs.stackexchange.com/questions/26991/is-it-possible-to-make-yasnippets-to-expand-snippets-inside-an-org-mode-babel-se

As of 2017, Jan 22, if you set org-src-tab-acts-natively and org-src-fontify-natively, then TAB in source blocks will expand snippets of the block’s language. You may want to set yas-buffer-local-condition to stop org mode snipppets from shadowing the block mode’s snippets:

(defun my-org-mode-hook ()
  (setq-local yas-buffer-local-condition
              '(not (org-in-src-block-p t))))
(add-hook 'org-mode-hook #'my-org-mode-hook)

See also https://github.com/joaotavora/yasnippet/issues/761https://github.com/joaotavora/yasnippet/pull/760

shareedit
Advertisements

Conflicts between org-mode and yasnippet

[Origin]: https://stackoverflow.com/questions/9418148/conflicts-between-org-mode-and-yasnippet

Turns out my issue was related to snippet syntax, not setup. Silly silly me…

In other words, this totally works:

;; fix some org-mode + yasnippet conflicts:
(defun yas/org-very-safe-expand ()
  (let ((yas/fallback-behavior 'return-nil)) (yas/expand)))

(add-hook 'org-mode-hook
          (lambda ()
            (make-variable-buffer-local 'yas/trigger-key)
            (setq yas/trigger-key [tab])
            (add-to-list 'org-tab-first-hook 'yas/org-very-safe-expand)
            (define-key yas/keymap [tab] 'yas/next-field)))
shareedit

Emacs: Stop Cursor Going into Minibuffer Prompt

[Origin]: http://ergoemacs.org/emacs/emacs_stop_cursor_enter_prompt.html

in emacs minibuffer prompt, when you press the left arrow key, the cursor will move back all the way over the prompt text. This is annoying because user often will hold down 【Alt+b】 to move back by word to edit, and when user starts to type something, emacs will say “This is read-only”. Then you have to manually move cursor out of the prompt. You can try it now by Alt+x query-replace.

Here’s how to set the cursor not going into prompt. Put the following in your emacs init.

;; minibuffer, stop cursor going into prompt
(customize-set-variable
 'minibuffer-prompt-properties
 (quote (read-only t cursor-intangible t face minibuffer-prompt))))

( Thanks to Clément Pit-Claudel)

Here’s alternative way to do it. Might work better if emacs changed in the future by the time you read this.

  1. Alt+x customize-group.
  2. Type “minibuffer”.
  3. scroll down to “Minibuffer Prompt Properties”.
  4. Check box “Don’t Enter”.
  5. Go to the top, click “Apply and Save”
emacs customize-group minibuffer 2016-06-16
M-x customize-group, “minibuffer”.

After that, emacs will insert the following to your init file:

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(minibuffer-prompt-properties
   (quote
    (read-only t cursor-intangible t face minibuffer-prompt))))

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

[Origin]: https://stackoverflow.com/questions/16048231/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)

(global-rainbow-mode)

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

shareedit

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 https://stackoverflow.com/a/19295380/324105). 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.

shareedit

Unset key binding in emacs

[Origin]: https://stackoverflow.com/questions/13965966/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))
shareedit

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

[Origin]: https://emacs.stackexchange.com/questions/13212/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*")))