2

I don't know very well how to work with vimscript... Because of that, I'd like to find a way where I can easily test what I'm trying to accomplish on my vimrc file, with some prints like on other programming languages.

To demonstrate what I'd like to do, I'll give some examples with other programming languages...

In python, if I want to execute it from the terminal, I just need to create a file with any name, put some python code inside it, and run it from the terminal with the command python3 file...

In node.js, if I want to execute it from the terminal, I just need to create a file with any name, put some node.js code inside it, and run it from the terminal with the command node file...

In bash, if I want to execute it from the terminal, I just need to create a file with any name, put some bash code inside it, and run it from the terminal with the command bash file...

So, I'm wondering if there's any similar way of executing and testing vimscript files like that as well... Is it possible? I know that vimscript works inside my vimrc file but I'm struggling to find a good way of testing it...

5
  • The fundamental difference between those other examples is that interacting with a user at the command line is a standard way of doing things. Not so for vimscript. A common way to deal with this is to source the script file in batch mode and then use :p[rint] to dump all of the buffer's lines to stdout. Here's an example of one way to do that... vim -e -c 'source script.vim' -c '%p' -c 'q!' /dev/stdin <targetfile
    – B Layer
    Sep 27, 2021 at 7:19
  • VimScript is not a conventional programming language. It's only used within Vim and for Vim. So just run Vim, write script file and :sourceit. Add some :echom for debugging, etc.
    – Matt
    Sep 27, 2021 at 7:37
  • @BLayer I'm not sure if I'm missing something but when I try this command I only see the current contents of the targetfile regardless of what I write on my script.vim file...
    – raylight
    Sep 27, 2021 at 8:19
  • @Matt Ah, I think sourcing it and creating a shortcut to make it go back to normal would be enough for me... I only had a little trouble because I didn't know I had to save the file before sourcing it, but it works fine now... Thanks
    – raylight
    Sep 27, 2021 at 8:45
  • I didn't word my first comment well. I didn't mean to say that there was a substitute for user interaction; merely that there is a way to emulate the "run program from command line and see the results in standard out" model. If that's not even what you're trying to achieve then you can disregard it.
    – B Layer
    Sep 27, 2021 at 8:50

2 Answers 2

3

This is implied by the comments but I think it'd be useful to have it as a self-contained answer. Use vim --cmd ":source <filename>".

Eg. when test.vim contains

echo "Hello, world!"
quit!

and I type vim --cmd "source test.vim" the output is

Hello, world!

I think this is what you want. Alternatively you can add --cmd "quit!" to the command line so you don't need it in the script.

0
  1. If this is the file helloworld.vim

    echo "Hello world"

  2. Then running this in the directory of this file helloworld.vim:

    g:\wordproc\vim\vim90\vim.exe --cmd "source helloworld.vim" --cmd "quit!" >ddd.txt

  3. Shows on the command line:

    Vim: Warning: Output is not to a terminal

    Hello world

  4. Note: I had to add >filename, e.g. >ddd.txt to get it working with an output on the command line.

  5. Note: If you use instead gvim.exe

    g:\wordproc\vim\vim90\gvim.exe --cmd "source helloworld.vim" --cmd "quit!" >ddd.txt

    you will instead see a dialog box showing 'Hello world'.

  6. -Tested successfully on Microsoft Windows 10 Professional (64 bits) running vim version 9.0

Your Answer

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

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