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 » Fri May 15, 2015 6:52 pm

(I don't remember if I already talked about this, but anyway...)

I like to have my beloved Emacs setup on different machines. For Debian-based machines, I have this little script that helps to create the environment I need.

Sometimes, it's not that easy. Although most of the external dependencies I need are somewhere in the Linux world, fonts are a different matter. To avoid pain of any kind enter dynamic-fonts.
dynamic-fonts.el makes font configuration more portable between machines. When Emacs is starting up, dynamic-fonts chooses fonts for your basic faces based on which fonts are actually available on your system.

You may set a list of fonts in order of preference using customize.
Some Elisp speaks a thousand words:

Code: Select all

(use-package dynamic-fonts ; Select best available font
  :ensure t
  :config (progn
            (setq dynamic-fonts-preferred-monospace-fonts
                  '("Source Code Pro"
                    "DejaVu Sans Mono")
                  dynamic-fonts-preferred-monospace-point-size 13
                  dynamic-fonts-preferred-proportional-fonts
                  '("Fira Sans Book"
                    "DejaVu Sans Book")
                  dynamic-fonts-preferred-proportional-point-size 13)

            (dynamic-fonts-setup)))

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 May 15, 2015 7:21 pm

^ I like your 'little script' (that downloads all the goodness) - will probably add it (with a little menu) to a future Emacs-based release if you don't mind. :)
..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 » Fri May 15, 2015 7:24 pm

^ Of course I don't mind. I didn't share it before because it is tailored to my usage (LaTeX madness, Clojure, etc.). Just trash the stuff you don't need. ;)

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 » Sat May 16, 2015 5:00 am

^ The good thing about it is that it contains a lot of stuff, IMO it should just have a multi-selection menu at the end (not for ourselves, but the users), so it can put stuff into the corners that Frenchmaid will later clean up again :D
..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 May 16, 2015 7:56 am

^ True. At first it had a sort of y/n prompt (like frenchmaid), then I threw it away because I am lazy and I trust the bloat. But you're right, for users a menu would be the best 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 » Sun May 17, 2015 11:03 am

Just got rid of Dired+ and Bookmark+ (sorry, Drew Adams):

- helm-bookmarks is everything I need for bookmarks
- I was using Dired+ mainly because I like to reuse Dired buffers. All I needed for this is:

Code: Select all

(put 'dired-find-alternate-file 'disabled nil)
Now, pressing 'a' reuses buffers, pressing 'RET' opens a new buffer.

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 May 19, 2015 1:37 pm

It took me a while, but I finally have a decent ansi-term configuration that let me ditch multi-term once and for all.

Code: Select all

(use-package ansi-term
  :bind ("<f2>" . ansi-term)
  :init (progn
          ;; Always use Zsh
          (defvar my-term-shell "/usr/bin/zsh")
          (defadvice ansi-term (before force-bash)
            (interactive (list my-term-shell)))
          (ad-activate 'ansi-term)

          ;; Disable hl-line-mode in ansi-term
          (add-hook 'term-mode-hook (lambda ()
                                      (setq-local global-hl-line-mode
                                                  nil)))

          ;; Close buffer with 'exit'
          (defadvice term-sentinel (around my-advice-term-sentinel (proc msg))
            (if (memq (process-status proc) '(signal exit))
                (let ((buffer (process-buffer proc)))
                  ad-do-it
                  (kill-buffer buffer))
              ad-do-it))
          (ad-activate 'term-sentinel)

          ;; Always use UTF-8
          (defun my-term-use-utf8 ()
            (set-buffer-process-coding-system 'utf-8-unix 'utf-8-unix))
          (add-hook 'term-exec-hook 'my-term-use-utf8)

          ;; Paste with C-y
          (defun my-term-paste (&optional string)
            (interactive)
            (process-send-string
             (get-buffer-process (current-buffer))
             (if string string (current-kill 0))))

          (defun my-term-hook ()
            (goto-address-mode) ; Clickable URLs
            (define-key term-raw-map "\C-y" 'my-term-paste))
          (add-hook 'term-mode-hook 'my-term-hook)))
Not that multi-term is wrong, but I was using it only because it closes the term buffer on "exit" (like it should be by default, imho)

User avatar
stark
MILF
Posts: 521
Joined: Sat Sep 27, 2014 6:38 pm
Location: Arpanet
Contact:

Re: Emacs tips and tricks

Unread post by stark » Tue May 19, 2015 3:32 pm

^ I don't understand all the code because I'm a n00b, but the comments made the end goal very clear. Thank You gekko :)
If you can do it go ahead and do it, if you can't do it then don't even criticize it. - gingerdesu

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 May 19, 2015 3:37 pm

^ You're welcome. I try to comment my code whenever possible. :)

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 May 23, 2015 3:06 pm

Shameless spam, but I feel like it is worth it: https://www.masteringemacs.org/book

I had the pleasure of editing the first draft and help the author a bit, so I am happy to see it out eventually.
I cannot recommend this book enough. For the newcomer and intermediate user there is a lot to learn here, but I am sure even the more experienced knight could also find something useful in these pages.

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 » Sat May 23, 2015 3:48 pm

^ Nice! Let's see if I can download a free PDF copy of it :D
..gnutella..

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Emacs tips and tricks

Unread post by DebianJoe » Sun May 24, 2015 3:44 pm

GP, that's a pretty sweet ansi-term config!

I've been stuck writing short segments to get eshell to behave more like my zsh config for a while, but the interaction of the terminal has some pretty severe limitation when interfacing with 3rd party programs.

I may take inspiration from (read: 'steal') your setup. :)
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

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 May 24, 2015 4:34 pm

^ Cool, glad you like it.

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 » Mon May 25, 2015 3:53 am

emacs + tmux = emamux
https://github.com/syohex/emacs-emamux

turnip does something similar
https://github.com/kljohann/turnip.el
..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 » Tue May 26, 2015 5:57 pm

Need a nice English dictionary?

Code: Select all

sudo apt-get install wordnet
And then use the very good helm-wordnet:

Code: Select all

(use-package helm-wordnet ; Helm interface for Wordnet dictionary
  :ensure t
  :bind ("C-c h w" . helm-wordnet)
  :config (setq helm-wordnet-wordnet-location "/usr/share/wordnet"))

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 May 29, 2015 2:47 pm

Two random things:

- I worked on a decent README.md for those of you who want to run Emacs trunk on our spins. I know my config is bloated, but you can just comment out whatever you want from init.el so to avoid loading packages you don't need: https://github.com/manuel-uberti/.emacs.d

- if you want intelligent word transformations, check this package: https://github.com/mrkkrp/fix-word (although some of the things can be done with the negative argument, if you will)

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 Jun 04, 2015 7:28 pm

In the rare occasions, when in Dired, I need to open a file with another application:

Code: Select all

(defun custom/open-in-external-app ()
  "Open the current file or dired marked files in external app.
The app is chosen from your OS's preference."
  (interactive)
  (let* ((file-list
          (if (string-equal major-mode "dired-mode")
              (dired-get-marked-files)
            (list (buffer-file-name)))))
    (mapc
     (lambda (file-path)
       (let ((process-connection-type nil))
         (start-process "" nil "xdg-open" file-path))) file-list)))

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 Jun 12, 2015 2:49 pm

I use Org so often now I really don't use anything else for prose. Ok, I use LaTeX, but only for professional-looking documents.

Unfortunately, I have to deal with a lot people who are crazy enough to prefer a .docx or an .odt file instead of my well-done .org. I know Org lets you export in many formats, but I was never satisfied with the output.

Enter Pandoc, a cool (and bloated) command-line utility for this kind of workflows. It's in your favorite repos, don't need me to tell you what to do here.

Within Emacs, it is possible to interact with Pandoc via pandoc-mode, but there is another way I've been using lately. I find it simpler and quicker. It's called ox-pandoc.

Code: Select all

(use-package ox-pandoc ; Export Org documents via Pandoc
  :ensure t
  :defer t
  :config (progn
            (setq org-pandoc-options '((standalone . t)) ; default options
                  ;; special settings for beamer-pdf and latex-pdf exporters
                  org-pandoc-options-for-beamer-pdf
                  '((latex-engine . "lualatex"))
                  org-pandoc-options-for-latex-pdf
                  '((latex-engine . "lualatex")))

            ;; Use external css for html5
            (let ((stylesheet (expand-file-name
                               (locate-user-emacs-file "etc/pandoc.css"))))
              (setq org-pandoc-options-for-html5
                    `((css . ,(concat "file://" stylesheet)))))))
This is my setup. I use lualatex for pdfs, and the output is definitely better than original Org export to pdf, although both go through LaTeX. Exporting in .odt and .docx works like a charm, but the nice trick is using an external css to fine-tune html5 export.

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 Jun 12, 2015 3:15 pm

^ Pandoc is excellent, one of my must-install packages (together with whizzytex) when working with LaTex in Emacs.
..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 Jun 13, 2015 8:44 am

You know I am a sucker for language tools. If you didn't, now you do.

Besides spell-checking with Flyspell, I usually use a couple of packages when I write in English (99% of the time I write in English):

- Synosaurus: synonims, with a cool name
- helm-wordnet: I wrote about this before

This morning I found out about writegood-mode. A fascinating package. It uses Flesh-Kincaid readability tests to check your writings. Maybe not as useful as an English dictionary, but I can see teachers and educators pretty happy with an utility such as this.

Post Reply