Emacs tips and tricks

Forum rules
Share your brain ;)
User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP » Thu Sep 24, 2015 1:40 pm

Man, I love YASnippet. Such a productivity boost. If you ever find yourself repeating a set of instructions in your code/text, a snippet might make it easier and quicker.

This is the relevant configuration to have YASnippet ready:

Code: Select all

(use-package yasnippet ; Snippets
  :ensure t
  :init (yas-global-mode)
  :config
  (progn
    (setq yas-verbosity 1 ; No need to be so verbose
          yas-wrap-around-region t))
Now all you need is some cool snippets. YASnippet uses ~/.emacs.d/snippets as its default directory. For every major mode you need snippets, just put a directory with that mode name in there. Such as this:

Code: Select all

~ % ls .emacs.d/snippets 
clojure-mode  emacs-lisp-mode  html-mode  latex-mode
And this is ~/.emacs.d/snippets/latex-mode/textbf

Code: Select all

# -*- mode: snippet -*-
# name: textbf
# key: b
# --
\textbf{$0}
After you enter a new snippet, restart Emacs or (better) just do a M-x yas-reload-all.

Now, see that "key: b" in the snippet code? Exactly. Type b and then hit TAB in your usual .tex file and voilà: magic happens.

Snippets can save a lot of typing. This is for the tables I usually need in my LaTeX documents:

Code: Select all

# -*- mode: snippet -*-
# name: table
# key: table
# --
\begin{table}[!htb]
  \centering
    \begin{threeparttable}
      \renewcommand{\arraystretch}{1.5}
      \begin{tabular}{${0:format}}
      \end{tabular}
    \end{threeparttable}
\end{table}
This is for new HTML files:

Code: Select all

# -*- mode: snippet -*-
# name: html
# key: html
# --
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>$1</title>
  </head>
  <body>
    $0
  </body>
</html>

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Emacs tips and tricks

Unread post by machinebacon » Thu Sep 24, 2015 7:09 pm

^ Thanks for the HTML yasnippet :)
..gnutella..

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP » Sat Sep 26, 2015 9:06 am

^ And if you're into Helm by any chance, this will be useful when you have many snippets and can remember the exact keyword:

Code: Select all

(use-package helm-c-yasnippet ; Helm source for Yasnippet
  :ensure t
  :defer t
  :init (with-eval-after-load 'yasnippet
          (bind-key "C-c h y" #'helm-yas-complete))
  :config (setq helm-yas-space-match-any-greedy t))
2015-09-26-100744_998x263_scrot.png

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP » Thu Oct 01, 2015 5:10 pm

Since the interest in CHICKEN Scheme shown here, I'll share my Scheme setup.

Code: Select all

(use-package scheme ; Configuration for Scheme
  :config
  (progn
    (require 'cmuscheme)

    ;; Use CHICKEN Scheme
    (setq scheme-program-name "csi")
    (add-to-list 'interpreter-mode-alist '("chicken-scheme" . scheme-mode))

    ;; Add custom header to .scm files
    (setq auto-insert-alist
          '(("\\.scm" .
             (insert
              "#!/bin/sh\n#| -*- scheme -*-\nexec csi -s $0 \"$@\"\n|#\n"))))

    (bind-key "C-c m s" #'run-scheme scheme-mode-map)
    (bind-key "C-c m l" #'scheme-load-current-file scheme-mode-map)
    (bind-key "C-c m f" #'scheme-compile-current-file scheme-mode-map)

    (defun scheme-load-current-file (&optional switch)
      (interactive "P")
      (let ((file-name (buffer-file-name)))
        (comint-check-source file-name)
        (setq scheme-prev-l/c-dir/file (cons (file-name-directory    file-name)
                                             (file-name-nondirectory file-name)))
        (comint-send-string (scheme-proc) (concat "(load \""
                                                  file-name
                                                  "\"\)\n"))
        (if switch
            (switch-to-scheme t)
          (message "\"%s\" loaded." file-name) ) ) )

    (defun scheme-compile-current-file (&optional switch)
      (interactive "P")
      (let ((file-name (buffer-file-name)))
        (comint-check-source file-name)
        (setq scheme-prev-l/c-dir/file (cons (file-name-directory    file-name)
                                             (file-name-nondirectory file-name)))
        (message "compiling \"%s\" ..." file-name)
        (comint-send-string (scheme-proc) (concat "(compile-file \""
                                                  file-name
                                                  "\"\)\n"))
        (if switch
            (switch-to-scheme t)
          (message "\"%s\" compiled and loaded." file-name))))))

(use-package geiser ; Collection of modes for Scheme interpreters
  :ensure t
  :bind ("C-c m g" . run-geiser)
  :init (setq geiser-active-implementations '(chicken guile)))
Geiser is not needed, but it is just as good as CIDER is for Clojure.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Emacs tips and tricks

Unread post by machinebacon » Sun Oct 04, 2015 7:38 pm

A very thorough (and unique) idea of QuantifiedSelf is realized with Memacs: https://github.com/novoid/Memacs

The idea comes from here:
In 1945, Vannevar Bush wrote a famous article «As We May Think» where he develops the idea of having a «memory extender» called Memex. The memex can store all letters, books, and other information which are related to a person.
Now, Emacs with org-mode can do the same for you. I'm impressed. Will tackle this ASAP :)
..gnutella..

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP » Mon Oct 05, 2015 8:03 am

^ That is impressive.

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP » Wed Oct 14, 2015 2:41 pm

Instead of talking about random packages, I want to share a personal workflow that lately has been saving hours of annoying work. I can't stress the "personal" enough: YMMV.

Sometimes, I'd like to replace a string that I used in different files. Quite a long task had I to do it manually, especially because the files are in different directories.

Enter Dired. I use find-name-dired to find all the files I need. Here's the doc for find-name-dired:
Search DIR recursively for files matching the globbing pattern PATTERN,
and run Dired on those files.
PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
Cool, exactly what I need. Then I hit 't' to mark all the files in the Dired buffer. After that, I press 'Q' (which is dired-do-query-replace-regexp) to insert the string I need to find and the new string I want to replace the old one with.
Emacs will let you move inside every file to review every match. Usually, I am happy with everything dired-do-query-replace-regexp finds, so a simple '!' is enough to replace all the matches.

To sum up with a one-liner: M-x find-name-dired RET [insert pattern] RET t Q [insert regexp] RET [insert regexp] RET !

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP » Wed Oct 21, 2015 11:03 am

Tinkering with Bongo this morning. I find it easier to use than EMMS, but YMMV.

Code: Select all

(use-package volume ; Control audio volume from Emacs
  :ensure t
  :defer t)

(use-package bongo ; Play music with Emacs
  :ensure t
  :bind (("C-c a b" . bongo)
         ("C-c f b" . mu-bongo-add-from-dired))
  ;; Don't use the mode line
  :init (setq bongo-display-playback-mode-indicator nil
              bongo-mode-line-indicator-parent nil)
  :config
  (progn
    (defun mu-bongo-add-from-dired ()
      "Add marked files to Bongo library"
      (interactive)
      (let (file-point file (files nil))
        (dired-map-over-marks
         (setq file-point (dired-move-to-filename)
               file (dired-get-filename)
               files (append files (list file)))
         nil t)
        (save-excursion
          (set-buffer bongo-default-library-buffer-name)
          (mapc 'bongo-insert-file files))))))
Bongo automagically uses mpg123 to play music, so everything's good.

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP » Sun Oct 25, 2015 5:35 pm

I push'n'pop marks all the time, thanks to this code borrowed and adapted from Mastering Emacs:

Code: Select all

(defun push-mark-no-activate ()
  "Pushes 'point' to 'mark-ring' and does not activate the region.
Equivalent to \\[set-mark-command] when \\[transient-mark-mode] is disabled"
  (interactive)
  (push-mark (point) t nil)
  (message "Pushed mark to ring"))

(defun jump-to-mark ()
  "Jumps to the local mark, respecting the mark-ring' order.
This is the same as using \\[set-mark-command] with the prefix argument."
  (interactive)
  (set-mark-command 1))

(defun exchange-point-and-mark-no-activate ()
  "Identical to \\[exchange-point-and-mark] but will not activate the region."
  (interactive)
  (exchange-point-and-mark)
  (deactivate-mark nil))

(bind-key "C-+" 'push-mark-no-activate)
(bind-key "M-+" 'jump-to-mark)
(bind-key [remap exchange-point-and-mark]
          'exchange-point-and-mark-no-activate global-map)
Recently I found the show-marks package very useful:

Code: Select all

(use-package show-marks ; Navigate and visualize the mark-ring
  :ensure t
  :bind (("C-c n m s" . show-marks)
         ("C-c n m f" . forward-mark)
         ("C-c n m b" . backward-mark)))

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP » Fri Oct 30, 2015 9:47 am

If anyone is using Magit, version 2.3 came out recently. Now to use magit-status fullscreen (if you rock this way - I do), you just need this snippet:

Code: Select all

;; Set `magit-status' fullscreen
(setq magit-post-display-buffer-hook
         #'(lambda ()
              (when (derived-mode-p 'magit-status-mode)
                (delete-other-windows))))

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Emacs tips and tricks

Unread post by machinebacon » Thu Nov 05, 2015 4:44 pm

To quickly capture things using org, there's org-capture. Not sure if people use it, so I thought I'd share. Imagine you are working with some file, then you get a call and quickly need to jot down something. Press C-c c, then either [t] for a task or [j] for a journal entry, make your notes, press C-c c again and you are back in the file you were working on. The shit you captured will be nicely attached to your organizer file(s).

Code: Select all

;; make C-c o open the main organizer
(global-set-key (kbd "C-c o")
	(lambda () (interactive) (find-file "~/org/organizer.org")))

;; all files in ~/org will be shown in the agenda
(setq org-agenda-files (list "~/org"))

;; make this start capturing
(global-set-key (kbd "C-c c") 'org-capture)

;; these are two templates, one for tasks, one for journal entries with time stamp
(setq org-capture-templates
      '(("t" "Todo" entry (file+headline "~/org/organizer.org" "Tasks")
	 "* TODO %?\n  %i\n  %a")
	("j" "Journal" entry (file+datetree "~/org/journal.org")
	 "* %?\n %U\n  %i\n  %a")))
The examples I found online partly didn't work, and it definitely doesn't work with the org-mode from the repos, so best is to either stay with Emacs' own org version or get the one from git/elpa.
..gnutella..

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP » Thu Nov 05, 2015 4:58 pm

^ Neat. IIRC there is some trick around the web to capture URLs.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Emacs tips and tricks

Unread post by machinebacon » Thu Nov 05, 2015 5:49 pm

Maybe this one?

Code: Select all

(define-key global-map "\C-cl" 'org-store-link)
..gnutella..

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP » Thu Nov 05, 2015 5:59 pm

^ No, it was something grabbing the URL from Firefox and push it right into your organizer file.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Emacs tips and tricks

Unread post by machinebacon » Thu Nov 05, 2015 6:10 pm

^ Firewhat? :D

I guess you mean https://github.com/rexim/org-cliplink

basically something like xclip for real men.
..gnutella..

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP » Thu Nov 05, 2015 6:12 pm


machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Emacs tips and tricks

Unread post by machinebacon » Thu Nov 05, 2015 6:19 pm

^ Thanks ;)
..gnutella..

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP » Thu Nov 05, 2015 6:20 pm

^ I haven't tried it, though, so I'm not sure whether it works or not.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Emacs tips and tricks

Unread post by machinebacon » Fri Nov 06, 2015 7:11 am

And I actually never use Fireweasel, but the idea might be interesting for a browser-agnostic solution. I still like the idea of using xclip.
..gnutella..

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: Emacs tips and tricks

Unread post by GekkoP » Wed Nov 11, 2015 1:28 pm

I love TRAMP. Just used its multi-hop feature. You can easily edit as root on a remote host like so: C-x C-f /ssh:user@server|sudo:root@server:/tmp.txt

Post Reply