Skip to content

Instantly share code, notes, and snippets.

@mrnugget mrnugget/pipe_to_jq.el
Last active Jun 27, 2018

Embed
What would you like to do?
This binds `<leader>jq` to a command that pipes the current visual selection in evil-mode to `jq` and replaces it with the output.
;; Taken from here: https://emacs.stackexchange.com/questions/34894/how-to-execute-a-command-on-the-selection-shell-command-on-region-in-evil-mode
(defun shell-command-on-region-and-select
(start end command
&optional output-buffer replace
error-buffer display-error-buffer
region-noncontiguous-p)
"Wrapper for 'shell-command-on-region', re-selecting the output.
Useful when called with a selection, so it can be modified in-place"
(interactive)
(let ((buffer-size-init (buffer-size)))
(shell-command-on-region
start end command output-buffer replace
error-buffer display-error-buffer
region-noncontiguous-p)
(setq deactivate-mark nil)
(setq end (+ end (- (buffer-size) buffer-size-init)))
(set-mark start)
(goto-char end)
(activate-mark)
;; needed for evil line mode
(when (string= evil-state "visual")
(when (eq (evil-visual-type) evil-visual-line)
(evil-visual-select start end 'line)))))
(defun pipe-to-jq()
(interactive)
(shell-command-on-region-and-select
(region-beginning) (region-end)
"cat | jq ." nil t))
(evil-leader/set-key "jq" 'pipe-to-jq)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.