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:
parent
8e851ef1a8
commit
78d986ab26
|
@ -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) " {{{
|
function! s:ExecuteInShell(command) " {{{
|
||||||
let command = join(map(split(a:command), 'expand(v:val)'))
|
let command = join(map(split(a:command), 'expand(v:val)'))
|
||||||
let winnr = bufwinnr('^' . command . '$')
|
let winnr = bufwinnr('^' . command . '$')
|
||||||
|
@ -9,7 +11,25 @@ function! s:ExecuteInShell(command) " {{{
|
||||||
silent! execute 'nnoremap <silent> <buffer> q :q<CR>'
|
silent! execute 'nnoremap <silent> <buffer> q :q<CR>'
|
||||||
echo 'Shell command ' . command . ' executed.'
|
echo 'Shell command ' . command . ' executed.'
|
||||||
endfunction " }}}
|
endfunction " }}}
|
||||||
|
|
||||||
command! -complete=shellcmd -nargs=+ Shell call s:ExecuteInShell(<q-args>)
|
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>
|
nnoremap <Leader><Leader>s :Shell rg '' %<Left><Left><Left>
|
||||||
|
|
Loading…
Reference in a new issue