Emacs tips and tricks
Forum rules
Share your brain ;)
Share your brain ;)
Re: Emacs tips and tricks
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
Re: Emacs tips and tricks
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.
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.
Code: Select all
curl https://raw.githubusercontent.com/DebianJoe/lisp_scripts/master/markdown.el > /home/$USER/wherever/you/put/stuff/filename.el
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
^ Interesting stuff.
I'm writing something similar to 'clean' HTML files format.
I'm writing something similar to 'clean' HTML files format.
Re: Emacs tips and tricks
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 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.
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.")))))
Re: Emacs tips and tricks
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
^ Thanks.
I'm not sure whether this is necessary or not:
I'll do more tests with stringp.
I'm not sure whether this is necessary or not:
Code: Select all
(and (not (eq (car list) nil))
- wuxmedia
- Grasshopper
- Posts: 6454
- Joined: Wed Oct 17, 2012 11:32 am
- Location: Back in Blighty
- Contact:
Re: Emacs tips and tricks
that is cool, I thought lisp was magic, then I saw the variables listed... 8)
Re: Emacs tips and tricks
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
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
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 )
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
Re: Emacs tips and tricks
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.
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
^ Good point.
It's funny how we used to be able to do real stuff with rudimentary computers, but now we can't. -- ratcheer
Re: Emacs tips and tricks
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
@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. :)
Just get your hands dirty, and think of it like learning improv jazz. :)
Re: Emacs tips and tricks
^ dear me, that is A LOT! Thank you.
Re: Emacs tips and tricks
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.
If Elisp makes you crazy, just look at this
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))
Re: Emacs tips and tricks
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):
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
because I don't want to refresh the packages at every startup.
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)))))
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)
Re: Emacs tips and tricks
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".
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.
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)
Re: Emacs tips and tricks
...that's a good trick. :)
Re: Emacs tips and tricks
^ 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
Bacon asked for this share, so here goes:
in .emacs
in ~/.emacs.d/ercmod.el
..and ~/.ercpass
NOTE!!! This is in debug, does NOT work as expected.
in .emacs
Code: Select all
(load "~/.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")))
Code: Select all
(setq djpass "mypassword")