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.
Rakefile) and if it suits your project, use it.