Emacs: copy things at point without marking

This emacs config snippet is the predecessor to viking-mode: it allows me to copy thing at point without marking it beforehand. I just press a prefix key followed by an abbreviation of the thing to copy:

  • C-c w   copy word
  • C-c l    copy line
  • C-c p   copy paragraph
  • C-c a   copy all (the whole buffer)

Like viking-mode the copied thing is being highlighted shortly for visual feedback.

Put this into your .emacs config to try it out:

;; ** Easily copy word, line, paragraph or buffer without marking
;;
with ideas from:
;; http://emacs.stackexchange.com/questions/1051/copy-region-from-emacs-without-newlines
;; https://www.emacswiki.org/emacs/CopyWithoutSelection (defun my-blink(begin end) "blink a region. used for copy and delete" (interactive) (let* ((rh (make-overlay begin end))) (progn (overlay-put rh 'face '(:background "DodgerBlue" :foreground "White")) (sit-for 0.2 t) (delete-overlay rh) ))) (defun get-point (symbol &optional arg) "get the point" (funcall symbol arg) (point) ) (defun copy-thing (begin-of-thing end-of-thing &optional arg) "Copy thing between beg & end into kill ring. Remove leading and
trailing whitespace while we're at it. Also, remove whitespace before
column, if any. Also, font-lock will be removed, if any. Also, the
copied region will be highlighted shortly (it 'blinks')."
(save-excursion (let* ((beg (get-point begin-of-thing 1)) (end (get-point end-of-thing arg))) (progn (copy-region-as-kill beg end) (with-temp-buffer (yank) (goto-char 1) (while (looking-at "[ \t\n\r]") (delete-char 1)) (delete-trailing-whitespace) (delete-whitespace-rectangle (point-min) (point-max)) ;; del column \s, hehe
(font-lock-unfontify-buffer) ;; reset font lock
(kill-region (point-min) (point-max)) ) )))) (defun copy-word (&optional arg) "Copy word at point into kill-ring" (interactive "P") (my-blink (get-point 'backward-word 1) (get-point 'forward-word 1)) (copy-thing 'backward-word 'forward-word arg) (message "word at point copied")) (defun copy-line (&optional arg) "Copy line at point into kill-ring, truncated" (interactive "P") (my-blink (get-point 'beginning-of-line 1) (get-point 'end-of-line 1)) (copy-thing 'beginning-of-line 'end-of-line arg) (message "line at point copied")) (defun copy-paragraph (&optional arg) "Copy paragraph at point into kill-ring, truncated" (interactive "P") (my-blink (get-point 'backward-paragraph 1) (get-point 'forward-paragraph 1)) (copy-thing 'backward-paragraph 'forward-paragraph arg) (message "paragraph at point copied")) (defun copy-buffer(&optional arg) "Copy the whole buffer into kill-ring, as-is" (interactive "P") (progn (my-blink (point-min) (point-max)) (copy-region-as-kill (point-min) (point-max)) (message "buffer copied"))) ;; "speaking" bindings CTRL-[c]opy [w]ord, etc...
(global-set-key (kbd "C-c w") 'copy-word)
(global-set-key (kbd "C-c l") 'copy-line) (global-set-key (kbd "C-c p") 'copy-paragraph) (global-set-key (kbd "C-c a") 'copy-buffer)

27.05.2016 15:02 CC0 emacs fun opensource Source

Similar posts

Null Comment zu Emacs: copy things at point without marking



Add Comment:
Aus welchem Land kommen Sie?
Land*:
Your comment will only displayed publicly after my approval! Fields marked with * are mandatory.
If you include an email address, it will be deleted automatically after one month. Nothing else will be stored.