nvim: plugin/shell: Add key mapping to run jq on visual selection

While at it, add some explanation to the functions for posterity.
This commit is contained in:
Sanchayan Maity 2022-04-01 18:59:26 +05:30
parent 8e851ef1a8
commit 78d986ab26
1 changed files with 21 additions and 1 deletions

View File

@ -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 <silent> <buffer> q :q<CR>'
echo 'Shell command ' . command . ' executed.'
endfunction " }}}
command! -complete=shellcmd -nargs=+ Shell call s:ExecuteInShell(<q-args>)
" 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 <silent> <buffer> q :bw<CR>'
endfunction
command! JqScratch call s:JqScratch()
nnoremap <Leader><Leader>j :JqScratch<CR>
nnoremap <Leader><Leader>s :Shell rg '' %<Left><Left><Left>