Skip to content

Instantly share code, notes, and snippets.

@JEG2
Created March 26, 2012 16:51
Show Gist options
  • Select an option

  • Save JEG2/2206455 to your computer and use it in GitHub Desktop.

Select an option

Save JEG2/2206455 to your computer and use it in GitHub Desktop.
This works when there is an active region, but fails to leave the selection when it defaults to using the whole line.
;; Vim's o and O
(defun jeg2s-newline-below (skip-eol)
"Insert a new line below the current line and indent it."
(interactive "P")
(unless (or (eolp) skip-eol)
(end-of-line))
(newline-and-indent))
(global-set-key (kbd "C-c o") 'jeg2s-newline-below)
(defun jeg2s-newline-above ()
"Insert a new line above the current line and indent it."
(interactive)
(unless (bolp)
(beginning-of-line))
(newline)
(previous-line)
(indent-according-to-mode))
(global-set-key (kbd "C-c O") 'jeg2s-newline-above)
;; TextMate's C-D
(defun jeg2s-duplicate-line-or-region ()
"Duplicate the current region, or line, and leave it selected."
(interactive)
(unless (region-active-p)
(if (= (line-number-at-pos) (count-lines (point-min) (point-max)))
(progn (call-interactively 'jeg2s-newline-below)
(previous-line)))
(unless (bolp)
(beginning-of-line))
(call-interactively 'set-mark-command)
(next-line))
(call-interactively 'kill-region)
(yank)
(yank)
(setq deactivate-mark nil)
(activate-mark))
(global-set-key (kbd "C-c d") 'jeg2s-duplicate-line-or-region)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment