On Wed, 12 Apr 2006, Gerald Lai wrote:

On Wed, 12 Apr 2006, Steve Hyatt wrote:

Hi All.

I would like to map a key such that it 'executes' the current file. Is this a no brainer?

For example, I have the following in my .vimrc file:

"map <F12> :!php tester.php<CR>
"map <F12> :!./tester.py<CR>
map <F12> :!tester<CR>

I have to change the mapping based on what type of app I am working on (PHP script, python script, binary from a C++ compilation, etc) and then make sure the app is called 'tester'. There has to be a better way.

What I would like is to be able to just hit F12 and have the file that I am currently editing be executed, and hot hardcode it to run 'tester' from the current directory.

This will do what you want:

 nnoremap <F12> :!%:p<CR>

See ":help cmdline-special".

Hope this helps.
--
Gerald


Perhaps what you're also looking for is to set your mapping based on the
filetype of the file you're editing.

  autocmd FileType,BufWinEnter * call F12map()

  function! F12map()
    if &filetype == "php"
      nnoremap <buffer><F12> :!php %:p<CR>
    else
      nnoremap <buffer><F12> :!%:p<CR>
    endif
  endfunction

One more thing, when it comes to specifying the current file, "%:p" will
define the absolute file path, whereas "./%" will only define the file
in the current directory with the same filename as the one being edited.
The two are subtly different. It will become apparent when Vim is made
to switch between directories.

--
Gerald

Reply via email to