Skip to main content
deleted 24 characters in body
Source Link
Maxim Kim
  • 13.9k
  • 2
  • 20
  • 48

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 -bar -range 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 -bar -range Redir silent call s:redir(<q-args>)

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 -bar -range 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 -bar -range Redir silent call s:redir(<q-args>)

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>)
added 1286 characters in body
Source Link
Maxim Kim
  • 13.9k
  • 2
  • 20
  • 48

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 -bar -range 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 -bar -range Redir silent call s:redir(<q-args>)

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>

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 -bar -range 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 -bar -range Redir silent call s:redir(<q-args>)
Source Link
Maxim Kim
  • 13.9k
  • 2
  • 20
  • 48

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>