Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a script directly within vim/mvim/gvim

Tags:

vim

vi

macvim

TextMate has a nice feature that allows you to execute a script from within the current context and shows you the output in a separate window. This lets you write and test code on the go. I'm almost certain there is a similar feature with MacVim/gVIM, but I'm not sure what it is. Currently I save my buffers to disk, then go to the command line and execute the script in that respect. How do I improve that workflow with vim?

like image 501
randombits Avatar asked Jul 02 '10 14:07

randombits


People also ask

How do I make a vim file executable?

After making sure that Vim has detected the new file attributes using :checktime, the script removes the FileChangedShell autocmd again so that Vim acts normally for future file attribute changes. Now you can type :Xbit to make the file executable.


2 Answers

You can do this in vim using the ! command. For instance to count the number of words in the current file you can do:

:! wc % 

The % is replaced by the current filename. To run a script you could call the interpreter on the file - for instance if you are writing a perl script:

:! perl % 
like image 171
jts Avatar answered Sep 20 '22 12:09

jts


vim tutorial: Mapping keys in Vim

You can map keys so perl executes current script as suggested by jts above.

map <C-p> :w<CR>:!perl %<CR> 

will map Ctrl+P to write file and run it by perl

imap <C-p> <Esc>:w<CR>:!perl %<CR> 

lets you call the same in insert mode.

You should have .vimrc (_vimrc for Windows) file in your vim/home folder. It has instructions on how vim should behave.

map <C-p> :w<CR>:!perl %<CR> is just instruction to map Ctrl+p to:

a) write current the file :w

b) run command (perl) using % (currently open file) as parameter :!perl %

<CR> after each command stands for "carriage return": an instruction to execute specific command. imap does the same as map but listens Ctrl+p while in insert mode.

like image 41
camelotus Avatar answered Sep 21 '22 12:09

camelotus