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 » Tue Jun 23, 2015 3:23 pm

You know me, I do presentations and I want them to look almost as good as me. Not an easy thing to do, but let's try it anyway.

What we need is reveal.js and org-reveal. I just unzipped reveal.js in my /home and did this for org-reveal:

Code: Select all

(use-package ox-reveal ; Slideshows with Reveal.js
  :ensure t
  :config (setq org-reveal-root "file:///home/manuel/reveal.js"))
Note that in MELPA org-reveal is known as ox-reveal. Confusing, but I know nothing more than this difference.

Now we are set. Org-reveal's README told you everything you need to know, but if you want to do a quick test just create a new Org file with this lines:

Code: Select all

* H1
* H2
** H2.1
*** H2.1.1
* H3
Now export it with C-c C-e R R. Simple as that.

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 Jun 24, 2015 9:29 pm

I don't know if I'm the only Magit user here, but on July 1st version 2.1.0 will be eventually released: https://github.com/magit/magit

I've been tracking the next branch lately and it is absolutely lovely. Git has never been that pretty.

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 Jul 01, 2015 7:08 am

Sometimes I just need a quick, temporary buffer to write something and then throw it away. Random notes, snippets, whatever. We have the great *scratch* buffer already, but there is a cool package out there that has been serving me better lately. It is aptly named scratch.el.

Code: Select all

(use-package scratch ; Mode-specific scratch buffers
  :ensure t
  :commands scratch)
Now, if I am in org-mode (and since I've always got an organizer.org file open, I usually am in org-mode) using this package opens a new buffer in org-mode, which suits me better for random notes. Of course, if I'm doing Elisp the new scratch buffer will be in that mode. Handy.

I bound it to C-x t s.

Code: Select all

;; Custom keybindings activated with C^x t
(define-prefix-command 'toggle-map)
;; The manual recommends C-c for user keys, but C-x t is
;; always free, whereas C-c t is used by some modes.
(bind-keys :map ctl-x-map ("t" . toggle-map))
(bind-keys :map toggle-map
           ("v" . visual-line-mode)
           ("l" . linum-mode)
           ("s" . scratch)
           ("r" . revert-this-buffer)
           ("w" . writeroom-mode))

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 Jul 02, 2015 6:36 pm

Oh yes, Magit 2.1.0 is on MELPA. It definitely deserves your attention, because I don't think Git has never been so 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 » Mon Jul 13, 2015 5:06 pm

This is useful: https://github.com/Vifon/focus-autosave-mode.el
A global minor mode saving all the modified files when the Emacs frame loses its focus.
In my setup:

Code: Select all

(use-package focus-autosave-mode ; Autosave buffers when focus is lost
  :ensure t
  :init (focus-autosave-mode)
  :diminish focus-autosave-mode)

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 Jul 31, 2015 4:50 pm

Only for the mouse lovers: org-download.

Code: Select all

(use-package org-download
  :ensure t
  :init (with-eval-after-load 'org '(require org-download)))
Easily drag-and-drop images from your hard drive or from your browser right into your favorite Org files.

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 Aug 05, 2015 12:34 pm

Ever used a major mode so nasty that it took over you beloved custom keybindings? I did. One solution I was using, found it on Emacs StackExchange, was using a minor mode that "protected" my bindings. See this.

Now, in that Q&A, there is a very good advice: use bind-keys*. You need to have use-package properly configured, but since I already have it, here we go:

Code: Select all

(bind-keys*
 ("M-a"     . custom/backward-paragraph)
 ("M-e"     . custom/forward-paragraph)
 ("C-,"     . iedit-dwim)
 ("C-c o"   . (lambda ()
                (interactive)
                (find-file "~/org/organizer.org")))
 ("C-c M-s" . helm-do-ag))

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 Aug 07, 2015 3:34 pm

Let's talk bloat.

First, install cloc. It is available in your nearest repo.

Then evaluate these two nifty functions, or add them somewhere in your init file.

Code: Select all

(defun custom/all-init-files (&optional with-packages)
  "Return a list of all Emacs Lisp files in my configuration.

If WITH-PACKAGES is given and non-nil include 3rd party
packages."
  (append (list user-init-file)
          (directory-files-recursively (locate-user-emacs-file "lisp/")
                                       (rx ".el" eos))
          (if with-packages
              (directory-files-recursively package-user-dir
                                           (rx ".el" eos))
            nil)))

;;;###autoload
(defun custom/count-config-lines (&optional with-packages)
  "Show a buffer with LoC statistics for my Emacs config.

If WITH-PACKAGES is given and non-nil include 3rd party packages
into the count."
  (interactive "P")
  (let ((cloc (executable-find "cloc")))
    (unless cloc
      (user-error "Please install cloc"))
    (with-current-buffer (get-buffer-create " *LoC Emacs configuration*")
      (text-mode)
      (read-only-mode)
      (view-mode)
      (let ((inhibit-read-only t)
            (files (custom/all-init-files with-packages)))
        (erase-buffer)
        (goto-char (point-min))
        (apply #'call-process cloc nil t t "--quiet" files))
      (pop-to-buffer (current-buffer)))))
Then do the usual: M-x custom/count-config-lines. And now show us your bloat!

Mine:

Code: Select all

-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Lisp                            23            530            415           1928
-------------------------------------------------------------------------------
SUM:                            23            530            415           1928
-------------------------------------------------------------------------------

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 » Tue Aug 11, 2015 2:17 pm

If you keep adding modes and custom keybindings, it's hard to remember ALL the available keybindings. Well, it is for me. You probably have an SSD brain.

To help me with the plethora of keys I got there is a beautiful package: https://github.com/justbur/emacs-which-key

Code: Select all

(use-package which-key ; Show help popups for prefix keys
  :ensure t
  :init (which-key-mode)
  :config (setq which-key-idle-delay 0.5
                which-key-key-replacement-alist
                '(("<\\([[:alnum:]-]+\\)>" . "\\1")
                  ("up"                  . "↑")
                  ("right"               . "→")
                  ("down"                . "↓")
                  ("left"                . "←")
                  ("DEL"                 . "⌫")
                  ("deletechar"          . "⌦")
                  ("RET"                 . "⏎")))
  :diminish which-key-mode)
Basically it pops up, after a customizable delay, the list of available keybindings (prefix included). It really is beautiful:
Image

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 Aug 14, 2015 5:45 pm

(E)shell lovers, look no further: https://github.com/thierryvolpiatto/pcomplete-extension

This little package adds completion for commands such as ls and apt-get. Nothing fancy, but quite useful if you (like me) love your (e)shell sexy and cool.

Code: Select all

(use-package pcomplete-extension ; Enhanced completion in (e)shell buffers
  :ensure t
  :init (with-eval-after-load 'eshell '(require pcomplete-extension)))

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 Aug 17, 2015 5:55 pm

Every Lisp hacker out there needs this: http://endlessparentheses.com/a-comment ... mmand.html

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 » Tue Aug 18, 2015 6:39 pm

Profiling Emacs startup time used to be easy when Emacs Start Up Profiler worked.

A simpler solution is this neat package: http://www.randomsample.de/profile-dotemacs.el

Put it in a directory of your choice (mine is: ~/.emacs.d/various/) and then:

Code: Select all

emacs -Q -l .emacs.d/various/profile-dotemacs.el -f profile-dotemacs
The output shows the bottlenecks in your config with useful highlights.

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 Aug 24, 2015 6:59 pm

Although I usually prefer Eshell over shell and ansi-term, I sometimes fire up shell for quick builds and tests.

I don't know about you, but I hate when it echoes back to me the command I've just typed. Why you do that to me? Do you think I am so stupid I cannot read the line above to see my command?

Damn you, shell, take this and shut up already:

Code: Select all

;; Do not echo input back at me
(defun custom/shell-turn-echo-off ()
   (setq comint-process-echoes t))

(add-hook 'shell-mode-hook 'custom/shell-turn-echo-off)

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 Sep 04, 2015 3:23 pm

Two useful commands: flush-lines and keep-lines.

Say you have an awesome file like this one:

Code: Select all

INFO foo bar
TODO bar foo
TODO bar foo bar
INFO foo bar foo
INFO kung foo bar
Try M-x keep-lines RET todo RET and this is what happens:

Code: Select all

TODO bar foo
TODO bar foo bar
Or if you feel brave, try M-x flush-lines RET todo RET and oh-my look at that:

Code: Select all

INFO foo bar
INFO foo bar foo
INFO kung foo bar

User avatar
Dr_Chroot
Alfalfa
Posts: 1100
Joined: Mon Jun 09, 2014 9:49 pm
Location: among the sagebrush
Contact:

Re: Emacs tips and tricks

Unread post by Dr_Chroot » Sat Sep 05, 2015 3:03 am

I don't know if this has already been posted but...

http://blog.jakubarnold.cz/2014/06/23/e ... emacs.html
Fight internet censorship.
EFF | Tor Project | Bitcoin

"There have been times throughout American history where what is right is not the same as what is legal. Sometimes to do the right thing you have to break the law." - Edward Snowden

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 05, 2015 8:16 am

^ Oh yes, for the Vi/Vim lovers. I personally advice against using it and go straight for the Emacs way, but hey, your choice. :)

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 10, 2015 1:27 pm

I use dired a lot, so it should come with no surprises I got my reasonable configuration for it. One thing in particular, it's my custom keybindings, which I might've posted before but they're better now.

Code: Select all

    ;; Enable dired-find-alternate-file
    (put 'dired-find-alternate-file 'disabled nil)

    ;; Reuse buffers if they are directories
    (defun find-file-reuse-dir-buffer ()
      "Like `dired-find-file', but reuse Dired buffers."
      (interactive)
      (set-buffer-modified-p nil)
      (let ((file (dired-get-file-for-visit)))
        (if (file-directory-p file)
            (find-alternate-file file)
          (find-file file))))

    ;; Better keybinding for moving between directories
    (bind-keys :map dired-mode-map
               ("M-<up>"   . (lambda ()
                               (interactive)
                               (find-alternate-file "..")))
               ("M-p"      . (lambda ()
                               (interactive)
                               (find-alternate-file "..")))
               ("^"        . (lambda ()
                               (interactive)
                               (find-alternate-file "..")))
               ("RET"      . find-file-reuse-dir-buffer)
               ("M-<down>" . (lambda ()
                               (interactive)
                               (dired-find-alternate-file)))
               ("M-n"      . (lambda ()
                               (interactive)
                               (dired-find-alternate-file))))

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 Sep 14, 2015 2:47 pm

I'll just leave this here: https://github.com/pierre-lecocq/bonjourmadame
Might convince those of you who fancy women to join the Emacs party.

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 Sep 16, 2015 10:43 am

Since lately I've been mainly working with a huge screen plugged into my lappy I needed to have larger fonts. Here we go:

Code: Select all

;; Dinamically change font size based upon screen resolution
(if window-system
    (progn
      (if (> (x-display-pixel-width) 1800)
          (progn
            (set-face-attribute 'default nil
                                :family "Hack"
                                :height 150
                                :weight 'regular)
            (set-face-attribute 'variable-pitch nil
                                :family "Fira Sans"
                                :height 140
                                :weight 'regular))
        (progn
          (set-face-attribute 'default nil
                              :family "Hack"
                              :height 125
                              :weight 'regular)
          (set-face-attribute 'variable-pitch nil
                              :family "Fira Sans"
                              :height 125
                              :weight 'regular)))))

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 Sep 18, 2015 9:38 pm

With a slight hint of enthusiasm and a little tendency to a joyful behavior, I am moderately happy to announce I officially brought helm-company back from the land of the broken Emacs packages. My first lame entry into the muddy MELPA waters.

A simple fork, I know. However, nobody among the 3,336 who downloaded it seemed to be interested in keeping it alive. It's been a while since the author vanished from Github. Helm maintainer keeps upgrading and changing stuff, therefore helm-company needed to be up-to-date to work again. Not too hard, I just dealt with some necessary pull requests, so I wonder why nobody cared enough to do it before me.

Anyways. It's there, working again, available on MELPA. If you use Helm and Company, this is like a bridge that gives the best of both worlds. Enjoy.

Code: Select all

(use-package helm-company ; Show Company candidates through Helm
  :ensure t
  :defer t
  :init (with-eval-after-load 'company
          ;; Use Company for completion
          (bind-key [remap completion-at-point] #'helm-company company-mode-map)
          (bind-key "C-:" #'helm-company company-mode-map)
          (bind-key "C-:" #'helm-company company-active-map)))

Post Reply