[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: yank without indentation?
From: |
Drew Adams |
Subject: |
RE: yank without indentation? |
Date: |
Thu, 5 Jun 2014 16:29:25 -0700 (PDT) |
> Has anyone come up with a yank-without-indentation routine? Why am I
> always killing multi-line text with enormous left-hand whitespace, and
> wanting to yank it without that whitespace in place? Has anyone dealt
> with this?
This should do pretty much what you want.
(defun yank-sans-indent (&optional arg)
"Like `yank', but remove indentation."
(interactive "*P")
(setq yank-window-start (window-start)
this-command t)
(push-mark (point))
(insert-for-yank (string-trim (current-kill (cond ((listp arg) 0)
((eq arg '-) -2)
(t (1- arg))))))
(when (consp arg)
(goto-char (prog1 (mark t)
(set-marker (mark-marker) (point) (current-buffer)))))
(when (eq this-command t) (setq this-command 'yank-sans-indent))
nil)
(defun string-trim (string)
"Trim SPC and TAB chars from ends of STRING and each of its eols."
(let* ((start (progn (string-match "\\`[ \t]*" string)
(match-end 0)))
(end (progn (string-match "[ \t]*\\'" string start)
(match-beginning 0)))
(trimmed (substring string start end)))
(replace-regexp-in-string "\\(\n\\)[ \t]+" "\\1" trimmed)))
(global-set-key "\C-y" 'yank-sans-indent)
(You can also bind `M-y' to a command that is `yank-pop' but with
references to `yank' replaced by `yank-sans-indent'.)
[If you want the string that is placed on the `kill-ring' to be the
complete selected text, instead of that text trimmed to remove
indentation, then you can use an alternative approach of placing
a `yank-handler' property on the first character, and define a
yank handler that trims the indenting whitespace at the time of
insertion.]