tags:

views:

41

answers:

1

I have the following code in a vim file that it's auto sourced when editing php files. But I can't make it work.

"PHP config
if !exists("g:addPath")
  let g:addPath = 1
  let $PATH=$PATH.';C:\Program Files\Mozilla Firefox'
endif 

function! MakeThisUrl()
  let s:url='http://localhost/'
  let s:url=s:url. expand('%')
  return s:url
endfunction

function! MakeCustomUrl()
  let s:url='http://localhost/'
  let s:url=s:url. expand('%:p')
  return s:url
endfunction


map <F9>  :w<CR>:!firefox -new-tab MakeThisUrl()<CR>
map <F10>  :!firefox -new-tab call MakeCustomUrl()
imap <F9>  <Esc>:w<CR>:!firefox -new-tab MakeThisUrl()<CR><CR>
imap <F10>  <Esc>:!firefox -new-tab call MakeCustomUrl()

The idea is to make vim to auto generate the proper URL, so I can test the code just by presing F9. However, I can't make it execute MakeThisUrl() and all I get its

:!firefox -new-tab MakeThisUrl() <CR><CR>

instead of

:!firefox -new-tab http://localhost/filename.php <CR><CR>

Any ideas on how to make it work? Thanks in advance

+2  A: 

However, I can't make it execute MakeThisUrl() and all I get its
:!firefox -new-tab MakeThisUrl()

This doesn't work, because the command is executed as if typed. Try this instead:

map <F9>  :up<CR>:execute ":!firefox -new-tab ".MakeThisUrl()<CR>

The major change: use of the :execute which executes command resulting from the evaluation of the expression. Expression evaluation is when functions are called. Here its result is concatenated with the ":!firefox -new-tab " and it is gets executed as an Ex command (the commands starting with the :; for the :execute the leading : is optional).

P.S. The minor change/nitpick: instead of :w use :up (or :update) which writes the buffer only if it is modified.

Dummy00001
Thanks a lot, it worked just fine. Didn't thougt of using execute to generate the command. Thanks for the `update` hint, I'll apply it on other files I have on `au`
masterLoki