11

Let's say I'm in the shell (bash) and want to see output that :scriptnames would give, but don't want to start vim or maybe I want to echo that into another file.

So I execute

vim +scriptnames

Which shows me all the scripts, but how do I echo that into the shell's stdout?

5 Answers 5

10

That happens, because usually Vim switches to the alternate screen and works there. Upon exiting, Vim will switch back and therefore you don't see anything left from your Vim session, but the result of the command executed even before starting Vim.

This feature is explained at :h xterm-screens (link)

So you basically don't want that and therefore want to disable that. You can do so, by setting :set t_ti= t_te= the terminal codes that perform this to empty.

Putting it all together, you would want to start Vim like this:

vim -c ':set t_ti= t_te= nomore' -c 'scriptnames|q!'

We are resetting the 'more' option, so that you don't see the hit-enter-prompt.

5
  • 1
    This is AWESOME, thank you so much! I actually doubted this was possible.
    – hgiesel
    Jul 17, 2016 at 15:00
  • Does this makes it possible to use VimL as a full-fledged system scripting language by letting it write to stdout (for the particularly masochistic)? Jul 17, 2016 at 16:17
  • 1
    One problem with this is I can't scroll the terminal. (If the text is longer than my terminal height I can't see the top) The text also disappears if I open vim then exit.
    – FDinoff
    Jul 17, 2016 at 16:19
  • @FDinoff That's what pagers like less are there for. Just pipe it into less like so vim -c ':set t_ti= t_te= nomore' -c 'scriptnames|q!' | less. The layout maybe screwed up at first (it's in my terminal), but after pressing <C-L> everything is nice and dandy.
    – hgiesel
    Jul 17, 2016 at 16:50
  • I see no output when running vim -c ':set t_ti= t_te= nomore' -c 'scriptnames|q!', there is only a "quick flash", the command exit with code 0, no output. Am I missing something?
    – TrungDQ
    Dec 4, 2017 at 3:50
3

We can also use :redir command to redirect the results of vim commands into a file

For example,

vim -c "redir! > vimout | scriptnames | redir END | q"

This redirects the output of scriptnames into the file "vimout"

2

For neovim, use

nvim --headless -c 'command'
1
  • 1
    Welcome to Vi and Vim! Your answer was automatically flagged as low-quality due to its length. This answer could be improved with an edit to explain how this does the job or why it's different for neovim.
    – D. Ben Knoble
    Oct 13, 2020 at 12:59
1

I appreciate the answer provided by Christian Brabandt but I think a better answer would be to separate the quit command using another -c command like so:

vim -c ':set t_ti= t_te= nomore' -c 'scriptnames' -c 'q!' 

Using this method means that you could string together a number of different commands together and then use the final 'q!' to exit out of vim.

0

For situations where you need for vim to have fully loaded up as if you started it manually, this works:

vim -c "autocmd! CursorHold * <commands to run>"

For example, I wanted to redirect the output of :map to a file from the shell, but I wanted to capture the mapping that vim-airline creates only after it's displayed its tabline (line at top showing all buffernames open.) Because it seems to do this asynchronously, simply running a -c redirect to a file wasn't giving it time to make the mappings. There could be a better way, but this works for me, especially since I already have updatetime set to 100 (0.1 seconds), which affects how long until the CursorHold event fires. By default, vim sets it to 4 seconds.

vim -c "autocmd! CursorHold * set nomore | redir! > mapNew | map | redir END | q"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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