Skip to content

Instantly share code, notes, and snippets.

@mrnugget
Last active June 27, 2018 11:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrnugget/5d25d5231ef2275ddec51a54efd8af8f to your computer and use it in GitHub Desktop.
Save mrnugget/5d25d5231ef2275ddec51a54efd8af8f to your computer and use it in GitHub Desktop.
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