https://github.com/rolandwalker/simpleclip
It uses either Emacs internal API or third party CLI tool to insert the text. So syntax highlighting thing is skipped.
You can use simpleclip's default setup. But I recommend re-assign a more convenient key for pasting.
simpleclip
doesn't work in certain scenario. See https://github.com/rolandwalker/simpleclip/issues/6 for details.
So my current solution combines simpleclip
and my own code. It covers more scenarios than simpleclip
(For example, access Windows 10 clipboard inside Linux Subsystem).
Insert below code into ~/.emacs
and run M-x paste-from-x-clipboard
to paste from clipboard:
(unless (featurep 'simpleclip) (require 'simpleclip))
(defun test-simpleclip ()
(unwind-protect
(let (retval)
(condition-case ex
(progn
(simpleclip-set-contents "testsimpleclip!")
(setq retval
(string= "testsimpleclip!"
(simpleclip-get-contents))))
('error
(message (format "Please install %s to support clipboard from terminal."
(cond
(or (eq system-type 'gnu/linux)
(eq system-type 'linux)
(eq system-type 'usg-unix-v)
(eq system-type 'berkeley-unix))
"xsel")
((or (eq system-type 'cygwin) (eq system-type 'windows-nt))
"cygutils-extra from Cygwin")
(t
"CLI clipboard tools"))))
(setq retval nil)))
retval)))
(setq simpleclip-works (test-simpleclip))
(defun my-gclip ()
(cond
(simpleclip-works
(simpleclip-get-contents))
((eq system-type 'darwin)
(with-output-to-string
(with-current-buffer standard-output
(call-process "/usr/bin/pbpaste" nil t nil "-Prefer" "txt"))))
((eq system-type 'cygwin)
(with-output-to-string
(with-current-buffer standard-output
(call-process "getclip" nil t nil))))
((memq system-type '(gnu gnu/linux gnu/kfreebsd))
(let* ((powershell-program (executable-find "powershell.exe")))
(cond
(powershell-program
;; PowerLine adds extra white space character at the end of text
(string-trim-right
(with-output-to-string
(with-current-buffer standard-output
(call-process powershell-program nil t nil "-command" "Get-Clipboard")))))
(t
(with-output-to-string
(with-current-buffer standard-output
(call-process "xsel" nil t nil "--clipboard" "--output")))))))))
(defun paste-from-x-clipboard()
"Paste from clipboard."
(interactive "P")
;; paste after the cursor in evil normal state
(when (and (functionp 'evil-normal-state-p)
(functionp 'evil-move-cursor-back)
(evil-normal-state-p)
(not (eolp))
(not (eobp)))
(forward-char))
(insert (my-gclip)))
The setup I post here is simplified version. See https://github.com/wangchen/redguardtoo-emacs.d/blob/master/lisp/init-clipboard.el for my real world setup.