0

I use

:enew|pu=execute('scriptnames')

for dumping output into a buffer.

How do I write a mapping with this command line?

For example, I press <F6> and it auto write the command line and jump to placeholder ('scriptnames') for me to type and i just need press Enter.

4
  • what did you try? Sep 15 at 6:33
  • i tried nnoremap <leader>d :vnew | :enew\|pu=('version')<CR>
    – Pham Hung
    Sep 15 at 6:47
  • Not bad. Try using this: nnoremap <leader>d :vnew +pu=execute(':version')<cr> Sep 15 at 6:56
  • tpope's scriptease has :Scriptnames which is, IMO, sometimes more useful
    – D. Ben Knoble
    Sep 15 at 19:16
2

Do not use <CR> in the mapping as it executes the command you provide. Instead add enough number of <left>s to position cursor between '' where you can provide your command and press enter to get result:

nnoremap <F6> :vnew +pu=execute('')<left><left>

PS, I use custom :Redir command for that:

" Redirect the output of a Vim or external command into a scratch buffer
" https://gist.github.com/romainl/eae0a260ab9c135390c30cd370c20cd7
" Usage:
" Add command to your vimrc
" command! -nargs=1 -complete=command Redir silent call s:redir(<q-args>)
" To use:
" :Redir version
" Vim version would be in a new window
func! s:redir(cmd) abort
    for win in range(1, winnr('$'))
        if getwinvar(win, 'scratch')
            execute win . 'windo close'
        endif
    endfor
    if a:cmd =~ '^!'
        let cmd = a:cmd =~' %'
                    \ ? matchstr(substitute(a:cmd, ' %', ' ' . expand('%:p'), ''), '^!\zs.*')
                    \ : matchstr(a:cmd, '^!\zs.*')
        let output = systemlist(cmd)
    else
        if version > 704
            let output = split(execute(a:cmd), "\n")
        else
            redir => redir_out
            exe a:cmd
            redir END
            let output = split(redir_out, "\n")
        endif
    endif
    vnew
    let w:scratch = 1
    setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile
    call setline(1, output)
endfunc

command! -nargs=1 -complete=command Redir silent call s:redir(<q-args>)
1
  • Thanks, it works
    – Pham Hung
    Sep 15 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.