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)

Update 2017-02-12:

Sometimes during programming in emacs I need to copy a whole function. If a function contains empty lines, copy-paragraph is insuffcient. So, I added copy-defun, which copies the whole function (at point, that is, point must be inside the function). This works with almost all programming modes, as long as it implements beginning-of-defun and end-of-defun.


(defun copy-defun (&optional arg) “Copy function at point into kill-ring” (interactive “P”) (my-blink (get-point ‘beginning-of-defun) (get-point ‘end-of-defun)) (kill-ring-save (get-point ‘beginning-of-defun) (get-point ‘end-of-defun)) (message “function at point copied”))

(global-set-key (kbd “C-c f”) ‘copy-defun) </pre>

27 May 2016 | #emacs


Emacs screen reader - novel mode

I'm using Xah's novel function for a couple of weeks and found myself enhancing it time and again. Today - yet another day on my way south using the train - I took the opportunity and made a separate mode of the stuff. The result is novel-mode and I'm quite satisfied with it so far.

Novel mode is a minor mode. It makes the current buffer quasi read-only, disabled all distractions (like widgets, mode-line etc), enlarges the font size, line spacing, adds a margin left and right and adds an one key key-map which makes it possible to make dynamic adjustments. There's a GIF demo on the github page. Here are some screenshots:

Bare bones emacs before novel mode is active:

Novel mode activated:

After pressing "h" or "?"

After pressing [right] a couple of times: margins increased

After pressing [+] a couple of times: font size increased

After pressing "i": video display inverted

While I wrote it mostly for myself, I published the mode on github anyway. So if you want to try it out: installation and configurations instructions can be found there.

24 May 2016 | #emacs


Emacs viking mode

While I'm a long term emacs user (since 2000), I did not write much emacs lisp over the years. In fact I kept my config unchanged for more than a decade. Happy and lazy, you see.

But lately I stumbled upon a lot of emacs enthusiasts like Sacha Chua. And so, a couple of weeks ago, I started to re-organize my .emacs config. I turned it into an outshine file for better overview, added a version, made it portable (now I use the very same config on Windows, Linux and FreeBSD with various emacs versions). Also I added LOTs of convenient stuff to my emacs. Some are external modules I discovered, some developed myself.

One of the things I added was expand-region, which I love! But I also wanted to be able to delete stuff the same way: just press a key and delete something, press it again and delete more, press it again and delete even much more until finally the whole buffer gets deleted. So I started to implement it on my own. And I learned a lot about lisp in the process. 

However, the code got bigger and bigger and I decided to put it into an extra file, but don't get me wrong: viking-mode is pretty small compared to other modes. While I was at it, I made a minor mode of it. This is not the first time, I created a mode, cisco-mode is my fabrication as well, but this time I tried to make it really good, conforming to all specs and guidelines. Finally my submission to melpa have been accepted and so here it is: my first minor mode on melpa: http://melpa.org/#/viking-mode.

The code itself is hosted on Github. And here is a small demo/screencast I made so you can see how it works:

A word about the name: I choosed this name because viking-mode just deletes things, you do not have to mark them, there's no "Are you sure?" annoyances. It is even possible to disable putting deleted stuff into the kill-ring (which would make it possible to yank it back somewhere). So - it just kills. Like a Viking, hence the name.

16 May 2016 | #emacs


... denn Personal kostet Geld, wissen Sie

Selten wird neoliberales Geschäftsebaren so offen sichtbar wie heute, als meine Pizzabestellung von "Joeys Pizza" geliefert wurde. Der Lieferbote unterbreitete mir das Angebot, dass ich bei der nächsten Bestellung irgendein Bonusessen kostenlos dazu bekommen würde. Wenn ich Online bestellen würde (ich hatte telefonisch bestellt). Er wiederholte das mehrmals und betonte, das bekäme man ausschliesslich bei Onlinebestellung, denn (Zitat):

Personal kostet Geld, wissen Sie.

Weiss ich. Und ich weiss auch, dass das der Sinn der Sache ist. Ich bezahle den Pizzaservice und der bezahlt davon die Menschen, die die Arbeit mit meiner Pizza hatten (die dafür sowieso viel zu wenig bekommen). Das schliesst den freundlichen Mensch am Telefon natürlich mit ein.

Und den will Joeys Pizza wohl einsparen. Hätte sich der Lieferant nicht verplappert, wäre mir das gar nicht aufgefallen. Womöglich, nein sogar recht sicher, hätte ich beim nächsten Mal tatsächlich online bestellt. Ist ja auch praktischer. Im Leben wäre mir nicht in den Sinn gekommen, dass ich damit meinen Beitrag zum Stellenabbau bei "Joeys Pizza" geleistet hätte.

Statt dessen werde ich dort weiterhin telefonisch bestellen.

Liebe Betreiber von "Joeys Pizza": wenn Ihr schon Leute auf die Strasse setzen wollt, dann habt gefälligst die Eier, denen die tatsächlichen Gründe ehrlich ins Gesicht zu sagen: Ihr wollt mehr Profit machen. Und nicht "Leider ruft kaum noch jemand an, sorry...".

Verdammts Gesocks. 

Update 2016-04-17:

Nachtrag zu “die Betreiber”: ein US Konzern. Wer hätte das gedacht.

17 April 2016 | #gesellschaft


Was darf Satire?


12 April 2016 | #geschwätz