Skip to content

Instantly share code, notes, and snippets.

@benhsu
Created February 18, 2012 14:40
Show Gist options
  • Save benhsu/1859587 to your computer and use it in GitHub Desktop.
Save benhsu/1859587 to your computer and use it in GitHub Desktop.
emacs code to operate on a region or line
;; stab at generalizing this
(defun line-or-region-wrapper (func)
(interactive)
(if (region-active-p)
(funcall func (region-beginning) (region-end))
(funcall func (line-beginning-position) (line-end-position))))
(defun comment-or-uncomment-line-or-region ()
(interactive)
(line-or-region-wrapper 'comment-or-uncomment-region))
(defun indent-line-or-region ()
(interactive)
(line-or-region-wrapper 'indent-region))
(defun kill-line-or-region ()
(interactive)
(if (region-active-p)
(kill-region)
(kill-line )))
(global-set-key (kbd "C-;") 'comment-or-uncomment-line-or-region)
(global-set-key (kbd "C-w") 'kill-line-or-region)
;; from http://stackoverflow.com/questions/916797/emacs-global-set-key-to-c-tab
(global-set-key (kbd "<C-tab>") 'indent-line-or-region)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment