Emacs tips and tricks

Forum rules
Share your brain ;)
User avatar
Alad
should take a shower
Posts: 447
Joined: Wed May 21, 2014 12:52 am

Re: Emacs tips and tricks

Unread post by Alad » Thu Sep 04, 2014 8:18 pm

Re RSI, at least for me, my hands didn't like the regular emacs keybinds. YMMV. I've got rid of ergoemacs - too invasive and bugged - so I might take a look at this godmode (right now I just map CAPS to Ctrl).
It's funny how we used to be able to do real stuff with rudimentary computers, but now we can't. -- ratcheer

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 » Tue Sep 09, 2014 6:43 am

Assuming that you write code, and you like github-flavored markdown (because it's pretty) to go with your git projects, I wrote a short extension that gives a few cool functions to emacs to help with quickly formatting plaintext READMEs.

Code: Select all

curl https://raw.githubusercontent.com/DebianJoe/lisp_scripts/master/markdown.el > /home/$USER/wherever/you/put/stuff/filename.el
Load it with autoload, or load-file (I use this for single sessions) or however, and enjoy a few new tricks:
M^x strikethrough : adds markdown notation to make the selected region strike-through text.
M^x bold : adds markdown notation to make the selected region bold
M^x italic : like the above, but italics
M^x code : takes the entire selected region and sets markdown for it to appear as a code box
M^x blockquote : makes the cursor's current line into a block quote.
M^x linebreak : inserts markdown notation for a graphical line BELOW the cursor's current line.
M^x H1 (H1-H6, actually) : adds markdown notation for the cursor's current line to be formatted as an HTML header of specified size.
|>>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 » Tue Sep 09, 2014 8:22 am

^ Interesting stuff.
I'm writing something similar to 'clean' HTML files format.

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 Sep 09, 2014 1:18 pm

My first steps in Emacs Lisp. I mean, something I wrote from scratch instead of editing others code.
The idea is to replace some html tags for WordPress blog posts. What I like of this solutions is:

- new tags can be easily added
- replace-string-matches-recursively and only-strings-p could be used somewhere else

Code: Select all

(defvar oldtags '("<i>" "</i>" "<b>" "</b>"))
(defvar newtags '("<em>" "</em>" "<strong>" "</strong>"))

;; Replace HTML tags with the ones used by WordPress editor
(defun custom/replace-html-tags ()
  "Replace HTML tags with the ones used by WordPress editor."
  (interactive)
  (custom/replace-string-matches-recursively oldtags newtags))

;; Replace OLDTAGS elements with NEWTAGS elements recursively
(defun custom/replace-string-matches-recursively (oldtags newtags)
  "Replace OLDTAGS elements with NEWTAGS elements recursively."
  (custom/only-strings-p oldtags)
  (custom/only-strings-p newtags)
  (and (not (eq (length oldtags)
		(length newtags)))
       (error ("Lists must have same length.")))
  (and (not (eq (car oldtags) nil))
       (not (eq (car newtags) nil))
       (save-excursion
	 (save-restriction
	   (save-match-data
	     (goto-char (point-min))
	     (replace-string (car oldtags)
			     (car newtags))
	     (custom/replace-string-matches-recursively (cdr oldtags)
						 (cdr newtags)))))))

;; Check if the given list contains only strings
(defun custom/only-strings-p (list)
  "Check if LIST contains only strings."
  (and (not (eq (car list) nil))
       (if (stringp (car list))
	   (not (custom/only-strings-p (cdr list)))
	 (error ("List must only contain strings.")))))
Code is beginner level, I know. I did a couple of tests and it is working, but I'm sure it can be cooler than this.

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 » Tue Sep 09, 2014 1:57 pm

I like it. "Cooler" is not always better. It appears to serve a purpose, and that's all that is important in my opinion.
|>>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 » Tue Sep 09, 2014 2:38 pm

^ Thanks.
I'm not sure whether this is necessary or not:

Code: Select all

(and (not (eq (car list) nil))
I'll do more tests with stringp.

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Emacs tips and tricks

Unread post by wuxmedia » Tue Sep 09, 2014 5:03 pm

that is cool, I thought lisp was magic, then I saw the variables listed... 8)
"Seek, and Ye shall find"
"Github | Chooons | Site"

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 Sep 09, 2014 7:07 pm

There is a little bit of dkeg in any single one of us.
So wouldn't it be nice to have a preview of all the colors we are about to set in our term? Enter rainbow-mode: https://julien.danjou.info/projects/ema ... inbow-mode

User avatar
Alad
should take a shower
Posts: 447
Joined: Wed May 21, 2014 12:52 am

Re: Emacs tips and tricks

Unread post by Alad » Sun Sep 14, 2014 12:27 am

Got a file or quote full of rogue white space? Enter whitespace-cleanup.

http://batsov.com/articles/2011/11/25/e ... e-cleanup/

edit: or rather delete-trailing-whitespace (see below )
Last edited by Alad on Sun Sep 14, 2014 12:37 am, edited 1 time in total.
It's funny how we used to be able to do real stuff with rudimentary computers, but now we can't. -- ratcheer

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 Sep 14, 2014 12:36 am

The above in some variation used to be in my .emacs, but I found it will totally fuck you when saving Markdown, as single and double spaces at the end of lines are used for formatting.

M^x delete-trailing-whitespace is not as complicated as having to revert a commit because your editor decides to auto-magically destroy documentation formatting, IMHO.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
Alad
should take a shower
Posts: 447
Joined: Wed May 21, 2014 12:52 am

Re: Emacs tips and tricks

Unread post by Alad » Sun Sep 14, 2014 12:37 am

^ Good point.
It's funny how we used to be able to do real stuff with rudimentary computers, but now we can't. -- ratcheer

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 15, 2014 2:07 pm

This is only for DebianJoe (I suppose) and me (one day, not too far away): https://github.com/capitaomorte/sly

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 » Mon Sep 15, 2014 2:14 pm

@Gekko: Here's jump-starting your playing with it: http://pastebin.com/1gkvbxCs
Just get your hands dirty, and think of it like learning improv jazz. :)
|>>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 » Mon Sep 15, 2014 2:17 pm

^ dear me, that is A LOT! Thank you.

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 18, 2014 8:45 am

When I'm writing text and I don't need distractions I usually hide the mode line. But when I'm writing code and need frames, the mode line helps me quite a lot.
There is nothing wrong with the default setup, but since there is a way to customize the mode line to fit your needs, why not give it a try?

It all started with this blog post: http://amitp.blogspot.com/2011/08/emacs ... -line.html
I don't need fancy colors nor silly nyan cats in my Emacs, so I make it simpler and now I love it.

Code: Select all

(setq-default
 mode-line-format
 '(; Position
   "%4l:"
   "%3c"
   ; emacsclient [default -- keep?]
   mode-line-client
   "  "
   ; read-only or modified status
   (:eval
    (cond (buffer-read-only
           " RO ")
          ((buffer-modified-p)
           " ** ")
          (t "      ")))
   "    "
   ; directory and buffer/file name
   (:eval (shorten-directory default-directory 30))
   "%b"
   ; narrow [default -- keep?]
   " %n "
   ; mode indicators: vc, recursive edit, major mode, minor modes, process, global
   (vc-mode vc-mode)
   "  %["
   mode-name
   "%] "
   (:eval (format-mode-line minor-mode-alist))
   mode-line-process
   (global-mode-string global-mode-string)
   "    "
   "%p"
   ))

;; Helper function
(defun shorten-directory (dir max-length)
  "Show up to `max-length' characters of a directory name `dir'."
  (let ((path (reverse (split-string (abbreviate-file-name dir) "/")))
        (output ""))
    (when (and path (equal "" (car path)))
      (setq path (cdr path)))
    (while (and path (< (length output) (- max-length 4)))
      (setq output (concat (car path) "/" output))
      (setq path (cdr path)))
    (when path
      (setq output (concat "../" output)))
    output))
If Elisp makes you crazy, just look at this

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 » Wed Oct 01, 2014 9:51 am

If you happen to add many packages, it is going to be hard to make your Emacs configuration easily portable to different machines (true story).
One handy solution could be let Emacs take care of all the packages you are pretty sure you want on every machine. Here's what I do (I actually included more packages than I usually need, but this is on my main laptop):

Code: Select all

;; Default packages
(defvar custom/packages '(ido-ubiquitous
			  ido-vertical-mode
			  flx-ido
			  smex
			  browse-kill-ring
			  hungry-delete
			  smartscan
			  org
			  magit
			  auctex
			  latex-pretty-symbols
			  ebib
			  latex-preview-pane
			  latex-extra
			  adaptive-wrap
			  clojure-mode
			  cider
			  pandoc-mode
			  multifiles
			  rainbow-delimiters
			  bookmark+)
  "Default packages")

(defun custom/packages-installed-p ()
  (loop for pkg in custom/packages
        when (not (package-installed-p pkg)) do (return nil)
        finally (return t)))

(unless (custom/packages-installed-p)
  (when (not package-archive-contents)
    (message "%s" "Refreshing package database...")
    (package-refresh-contents)
    (dolist (pkg custom/packages)
      (when (not (package-installed-p pkg))
	(package-install pkg)))))
This is cool. If you don't have the "default packages" already installed, they will be installed when you open Emacs. If they are already there, nothing happens. Either way you are good to go with your configuration ready. And you could also add more packages to that list, if you are into such things as bloat.

The idea comes from this page: http://www.aaronbedra.com/emacs.d/
I fixed it to my needs adding

Code: Select all

when (not package-archive-contents)
because I don't want to refresh the packages at every startup.

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 03, 2014 8:32 am

Working with hooks lately, there is always something new to learn and that makes me happy.
One thing I was missing in Emacs is a decent solution to open files as root. I know there are many different tricks to do that, but what I need is a way to do something like C-x C-f /etc/apt/source.list in a quick and "transparent" way.

I found this: http://emacsredux.com/blog/2013/04/21/e ... es-as-root
Option B worked as the author says so I should be happy with it right? No, couldn't miss a chance to learn something new.
In the comments find-file-hook is suggested and the author of that blog post never came up implementing that solution.

I did, because as Joe would say "I'm just gangsta like that".

Code: Select all

;; Edit file with root privileges
(defun open-with-sudo ()
  "Find file as root if necessary."
  (unless (and buffer-file-name
	       (file-writable-p buffer-file-name))
    (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))

(add-hook 'find-file-hook 'open-with-sudo)
This solution works with standard find-file and with ido-find-file (if you use ido-mode like me). I did a couple of quick tests, enabling and disabling ido just to be sure everything's working.

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 » Fri Oct 03, 2014 8:54 am

...that's a good trick. :)
|>>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 » Fri Oct 03, 2014 10:48 am

^ thanks. I found a package that should be better than my solution, but I don't care. I like it this way. ;)

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 » Sat Oct 04, 2014 8:20 pm

Bacon asked for this share, so here goes:
in .emacs

Code: Select all

(load "~/.emacs.d/ercmod.el")
in ~/.emacs.d/ercmod.el

Code: Select all

;; REQUIRES in ~/.ercpass the format ;;
;; (setq variable "password") ;;

(load "~/.ercpass")
(require 'erc-services)
(erc-services-mode 1)

(setq erc-prompt-for-nickserv-password nil)
(setq erc-nickserve-passwords
      `((freenode ("DebianJoe" . ,djpass))))
(setq erc-autojoin-channels-alist '((".*" "#linuxbbq" "#<otherchannel")))
..and ~/.ercpass

Code: Select all

(setq djpass "mypassword")
NOTE!!! This is in debug, does NOT work as expected.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

Post Reply