Skip to content

Instantly share code, notes, and snippets.

@WaYdotNET
Created November 15, 2010 14:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save WaYdotNET/700416 to your computer and use it in GitHub Desktop.
Save WaYdotNET/700416 to your computer and use it in GitHub Desktop.
Align function emacs
;; Align command !!!
;; from http://stackoverflow.com/questions/3633120/emacs-hotkey-to-align-equal-signs
;; another information: https://gist.github.com/700416
;; use rx function http://www.emacswiki.org/emacs/rx
(defun align-to-colon (begin end)
"Align region to colon (:) signs"
(interactive "r")
(align-regexp begin end
(rx (group (zero-or-more (syntax whitespace))) ":") 1 1 ))
(defun align-to-comma (begin end)
"Align region to comma signs"
(interactive "r")
(align-regexp begin end
(rx "," (group (zero-or-more (syntax whitespace))) ) 1 1 ))
(defun align-to-equals (begin end)
"Align region to equal signs"
(interactive "r")
(align-regexp begin end
(rx (group (zero-or-more (syntax whitespace))) "=") 1 1 ))
(defun align-to-hash (begin end)
"Align region to hash ( => ) signs"
(interactive "r")
(align-regexp begin end
(rx (group (zero-or-more (syntax whitespace))) "=>") 1 1 ))
;; work with this
(defun align-to-comma-before (begin end)
"Align region to equal signs"
(interactive "r")
(align-regexp begin end
(rx (group (zero-or-more (syntax whitespace))) ",") 1 1 ))
# Ruby example
# FROM:
class Demo
property :id, Ciao
property :demo, String
property :foo, Bar
end
# align-to-comma:
class Demo
property :id, Ciao
property :demo, String
property :foo, Bar
end
@bdevel
Copy link

bdevel commented Dec 4, 2014

The align to colon wasn't working quite right for ruby hashes like this:

foo = {
  x: 3,
  abcd: 12
}

Here is a modification that seems to work better:

(defun align-to-colon (begin end)
  "Align region to colon"
  (interactive "r")
  (align-regexp begin end
       (rx ":" (group (zero-or-more (syntax whitespace))) ) 1 1 ))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment