Page 4 of 23

Re: Emacs tips and tricks

Posted: Thu Sep 04, 2014 8:18 pm
by Alad
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).

Re: Emacs tips and tricks

Posted: Tue Sep 09, 2014 6:43 am
by DebianJoe
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.

Re: Emacs tips and tricks

Posted: Tue Sep 09, 2014 8:22 am
by GekkoP
^ Interesting stuff.
I'm writing something similar to 'clean' HTML files format.

Re: Emacs tips and tricks

Posted: Tue Sep 09, 2014 1:18 pm
by GekkoP
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.

Re: Emacs tips and tricks

Posted: Tue Sep 09, 2014 1:57 pm
by DebianJoe
I like it. "Cooler" is not always better. It appears to serve a purpose, and that's all that is important in my opinion.

Re: Emacs tips and tricks

Posted: Tue Sep 09, 2014 2:38 pm
by GekkoP
^ 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.

Re: Emacs tips and tricks

Posted: Tue Sep 09, 2014 5:03 pm
by wuxmedia
that is cool, I thought lisp was magic, then I saw the variables listed... 8)

Re: Emacs tips and tricks

Posted: Tue Sep 09, 2014 7:07 pm
by GekkoP
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

Re: Emacs tips and tricks

Posted: Sun Sep 14, 2014 12:27 am
by Alad
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 )

Re: Emacs tips and tricks

Posted: Sun Sep 14, 2014 12:36 am
by DebianJoe
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.

Re: Emacs tips and tricks

Posted: Sun Sep 14, 2014 12:37 am
by Alad
^ Good point.

Re: Emacs tips and tricks

Posted: Mon Sep 15, 2014 2:07 pm
by GekkoP
This is only for DebianJoe (I suppose) and me (one day, not too far away): https://github.com/capitaomorte/sly

Re: Emacs tips and tricks

Posted: Mon Sep 15, 2014 2:14 pm
by DebianJoe
@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. :)

Re: Emacs tips and tricks

Posted: Mon Sep 15, 2014 2:17 pm
by GekkoP
^ dear me, that is A LOT! Thank you.

Re: Emacs tips and tricks

Posted: Thu Sep 18, 2014 8:45 am
by GekkoP
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

Re: Emacs tips and tricks

Posted: Wed Oct 01, 2014 9:51 am
by GekkoP
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.

Re: Emacs tips and tricks

Posted: Fri Oct 03, 2014 8:32 am
by GekkoP
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.

Re: Emacs tips and tricks

Posted: Fri Oct 03, 2014 8:54 am
by DebianJoe
...that's a good trick. :)

Re: Emacs tips and tricks

Posted: Fri Oct 03, 2014 10:48 am
by GekkoP
^ thanks. I found a package that should be better than my solution, but I don't care. I like it this way. ;)

Re: Emacs tips and tricks

Posted: Sat Oct 04, 2014 8:20 pm
by DebianJoe
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.