tags:

views:

899

answers:

7

I do a lot of Python quick simulation stuff and I'm constantly saving (:w) and then running (:!!). I'm wondering, is there a way to combine these actions. Maybe a "save and run" command.

Thanks for your help.

+1  A: 

Command combination seems to work through | character, so perhaps something like aliasing :w|!your-command-here to a distinct key combination?

Dimitri Tcaciuc
+8  A: 

Option 1:

Write a function similar to this and place it in your startup settings:

function myex()
   execute ':w'
   execute ':!!'
endfunction

You could even map a key combo to it-- look a the docs.


Option 2 (better):

Look at the documentation for remapping keystrokes - you may be able to accomplish it through a simple key remap. The following works, but has "filename.py" hardcoded. Perhaps you can dig in and figure out how to replace that with the current file?

:map <F2> <Esc>:w<CR>:!filename.py<CR>

After mapping that, you can just press F2 in command mode.

imap, vmap, etc... are mappings in different modes. The above only applies to command mode. The following should work in insert mode also:

:imap <F2> <Esc>:w<CR>:!filename.py<CR>a

Section 40.1 of the VIM manual is very helpful.

gahooa
For the filename, see the options for expand. I think expand("%:p") will get you the filename.
Caleb Huitt - cjhuitt
Just tested. Works great. Add "map <F5> <Esc>:w<CR>:!%:p<CR>" to your vimrc (without the quotes).
Adam Bernier
+3  A: 

Here you go:

:nmap <F1> :w<cr>:!%<cr>

save & run (you have to be in n mode though - just add esc and a for i mode)

ldigas
A: 
  1. Consider switching to IDLE. F5 does everything.

  2. Consider switching to Komodo. You can define a command so that F5 does everything.

S.Lott
A: 

I got the following from the vim tips wiki:

command! -complete=file -nargs=+ shell call s:runshellcommand(<q-args>)
function! s:runshellcommand(cmdline)
  botright vnew
  setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
  call setline(1,a:cmdline)
  call setline(2,substitute(a:cmdline,'.','=','g'))
  execute 'silent $read !'.escape(a:cmdline,'%#')
  setlocal nomodifiable
  1
endfunction

but changed new to vnew on the third line, then for python i have the following

map <F9> :w:Shell python %<cr><c-w>

hitting f9 saves, runs, and dumps the output into a new vertically split scratch buffer, for easy yanking/saving etc ... also hits c-w so i only have to press h/c to close it / move back to my code.

dysmsyd
+3  A: 

Use the autowrite option:

:set autowrite

Write the contents of the file, if it has been modified, on each :next, :rewind, :last, :first, :previous, :stop, :suspend, :tag, :!, :make, CTRL-] and CTRL-^ command [...]

strager
A: 

Okay, the simplest form of what you're looking for is the pipe command. It allows you to run multiple cmdline commands on the same line. In your case, the two commands are write \w\ and execute current file \! %:p\. If you have a specific command you run for you current file, the second command becomes, e.g. \!python %:p\. So, the simplest answer to you question becomes:

:w | ! %:p
 ^ ^ ^
 | | |--Execute current file
 | |--Chain two commands
 |--Save current file

One last thing to note is that not all commands can be chained. According to the Vim docs, certain commands accept a pipe as an argument, and thus break the chain...

Douglas Mayle