0

I use vim as an editor for writing shell (bash) and Python programs. Often, I need to pass different parameters to the program I develop to test/debug in different combination. For C sources a Makefile is responsible for building and running stuff, so it can be edited in the next tab, passing new parameters to the program every time I press F9, which executes 'make run'. There is no such thing as Makefile for scripts, so I have to run them manually in the next tab in Konsole, changing parameters, which seems to be pretty inefficient.

For Python, as an example, I have the following settings in .vimrc:

autocmd FileType python call Python_source()
func! Python_source()
    setlocal number cursorline
    setlocal shiftwidth=2
    setlocal foldmethod=indent

    map <F9> :w \| :!python %<CR>
    imap <F9> <Esc> :w \| :!python %<CR>
    ...
endfunc

Is there any way to store parameters in script source code (in comment, for example) and then pass it to the script as a parameter like this:

#vimparameter='-f -a --bus 1'

in .vimrc:

map <F9> :w \| :!python % $vimparameter<CR>
imap <F9> <Esc> :w \| :!python % $vimparameter<CR>

Or any other reasonable way to easy change and then pass parameters to the script, executed by F9 shortcut?

As suggested my @Matt, modelines could be used to pass some predefined line as an arguments to the command:

autocmd FileType python call Python_source()
...
func! LWargs()
    set lw=''
    doautocmd BufRead
    if len(&lw) > 0 && len(&lw) < 512
        return ' ' . &lw
    endif
    return ''
endfunc

func! Python_source()
...
    map <F9> :w \| :exe '!python' '%:p' . LWargs()<CR>
    imap <F9> <Esc> :w \| :exe '!python' '%:p' . LWargs()<CR>
...
endfunc

And then in source code parameters can be predefined with:

# vim: lw=--bus\ 10\ -f

'modeline' should be set.

1
  • 1
    Nothing prevents you from writing a Makefile and using it in a non-C project. Build tools are commonly used in scripting languages (think Ruby's Rakefile) and if it suits your project, use it. Commented Jun 5, 2019 at 0:47

1 Answer 1

1

You can use "modeline" to set some normally unused option. 'lispwords' seems just fine (unless you're a LISP programmer): it's a buffer-local string which is only used for lisp indenting. So you can do in python:

# vim: lw=-f\ -a\ --bus\ 1

import sys
print(sys.argv)

And in vimscript something like:

update
"ensure modeline is re-read
doautocmd BufRead
let l:args = (len(&lw) < 500) ? &lw : ''
exe '!python' shellescape(expand('%:p')) l:args
Sign up to request clarification or add additional context in comments.

2 Comments

It seems that Vim reads modeline once when file is opened, therefore it will ignore all changes made in buffer, even if it saved.
I'm not a vim script programmer at all, but with your help was able to write some simple solution that does what I need. Originally, I was going to write a function, that will parse buffer for some special string and get arguments past it, but modeline work just fine for me. Thanks!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.