2

I'm following the page where there is an example how to set-up Vim as a system interpreter for vimscript by setting the following shebang line:

#!/usr/bin/vim -nNesc:let&verbose=1|let&viminfo=""|source%|echo""|qall!

so I did the test by adding %print, but it prints the current script file.

I obviously don't want to parse my current file which consist commands for Vim, but I want to parse the file from the argument.

What shebang line should I use, so I can use the script as a parser for the file which I will specify in the argument?

For example to execute commands from parser.vim on file my_file.txt I would run:

./parser.vim my_file.txt

1 Answer 1

2

source% should give you a hint what's going on: the first argument is the current script, while my_file.txt is the second argument. Adding args or ls to the script confirms that. This means you have to add bnext to the top of your script to edit the text file rather than the script itself. It also means you'll have to manage arguments in your script. The wiki page is misleadingly optimistic about that.

Edit: One way to overcome the problem of the current buffer pointing to the script itself is to add bnext to the shebang line. However, running bnext before source % is too early (after bnext the script can no longer be sourced), and running it after source % is too late (the script has already run). A workaround is to add it to a SourcePre autocommand. But since an autocommand would consume the rest of the command line, it has to be wrapped in an exec. And you probably also want to limit SourcePre to the script itself. This leads to something like this, which mostly works:

#! /usr/bin/vim -nNesc:let&verbose=1|let&viminfo=""|exec "autocmd SourcePre ".fnameescape(expand("%:p",1))." bnext"|source%|qall!

You'd still need to do something about args if your script uses argdo and friends.

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.