From 78d986ab26e3deb495e370cec280c9b1339ae050 Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Fri, 1 Apr 2022 18:59:26 +0530 Subject: [PATCH] nvim: plugin/shell: Add key mapping to run jq on visual selection While at it, add some explanation to the functions for posterity. --- nvim/.config/nvim/plugin/shell.vim | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/nvim/.config/nvim/plugin/shell.vim b/nvim/.config/nvim/plugin/shell.vim index a6b84ab..af4760e 100644 --- a/nvim/.config/nvim/plugin/shell.vim +++ b/nvim/.config/nvim/plugin/shell.vim @@ -1,3 +1,5 @@ +" Helps with running a shell command and opens it result in another tab. Most +" helpful with GStreamer logs to run filters like rg 'pipeline|udpsink' %. function! s:ExecuteInShell(command) " {{{ let command = join(map(split(a:command), 'expand(v:val)')) let winnr = bufwinnr('^' . command . '$') @@ -9,7 +11,25 @@ function! s:ExecuteInShell(command) " {{{ silent! execute 'nnoremap q :q' echo 'Shell command ' . command . ' executed.' endfunction " }}} - command! -complete=shellcmd -nargs=+ Shell call s:ExecuteInShell() +" Make a visual selection first. This function then opens a scratch buffer +" in a new tab, pastes the visual selection and runs jq on the buffer +" replacing its contents. Helps with analysing portions of pino logs processed +" by pino-pretty. +function! s:JqScratch() + tabnew + noswapfile hide enew + setlocal buftype=nofile + setlocal bufhidden=hide + setlocal nobuflisted + file scratch + norm p + silent! execute '%!jq' + silent! execute 'set ft=json' + silent! execute 'nnoremap q :bw' +endfunction +command! JqScratch call s:JqScratch() + +nnoremap j :JqScratch nnoremap s :Shell rg '' %