1

I am going to write a program which can be run from the command line, at least on *nix systems. I considered using Python or Perl, but why not Vimscript?

Has anybody done this?

What is the best practice for writing this kind of application?

Is there a bootstrap or library?

User interaction will be minimal in my case. The input will be provided as arguments to the executable, and Vim will display text output and do some processing.



My initial attempt

Outputs this when run:

$ ./my_new_app arg1 "broken word" arg3

Retrieved arguments: arg1 broken word arg3
Press ENTER or type command to continue
Argument 0 is: arg1
Press ENTER or type command to continue
Argument 1 is: broken
Press ENTER or type command to continue
Argument 2 is: word
Press ENTER or type command to continue
Argument 3 is: arg3
Press ENTER or type command to continue

There are three issues:

  1. It breaks the second argument into two arguments due to the space.
  2. Every time I print something, I must Press ENTER.
  3. If I pipe the result as in ./myapp 1 2 3 > output.file then I get "Warning: Output is not to a terminal", and I can't see the Press ENTER prompts, although I still need to fulfil them.

my_new_app (executable file)

#!/bin/bash
# Pass the arguments to Vim in a variable, and exec my_new_app.vim
vim -c ":let g:arguments_string='$*'" -c ":so $0.vim"

my_new_app.vim

function! s:Print(str)
  exec '!echo "' . a:str . '"'
  " This doesn't work so well: I only see the first line.
  "call system('echo "' . a:str . '"')
endfunction

call s:Print("Retrieved arguments: " . g:arguments_string)

let s:args = split(g:arguments_string, ' ')

for i in range(0, len(s:args))
  call s:Print("Argument " . i . " is: " . s:args[i])
endfor

exit
  • 3
    One of my tools does this by pre-processing the arguments in a shell script, rather than VimScript. I don't know if this works well for your specific use-case, but it certainly saves you from re-implementing getopt in VimScript... – Martin Tournoij Oct 7 '15 at 23:15
  • 6
    Much as I love vim, it is a text editor, not a true scripting language. The best hydraulic cutting machine in the world still makes a poor three-ring hole punch. – Wildcard Oct 8 '15 at 5:23
  • 2
    You probably don't want to use VimL as a general-purpose scripting language. It's orders of magnitude slower than Python and Perl, and the API has a lot of limitations that make it unsuitable for many tasks. – Sato Katsura Oct 9 '15 at 5:16
  • @Wildcard Please make that an answer! – dessert Dec 12 '18 at 16:47

Your Answer

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

Browse other questions tagged or ask your own question.