Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: execute current file?

Tags:

vim

If I have a file with a shebang line (e.g. #!/bin/bash) open in Vim and the file has execute permissions (i.e. chmod +x) I know I can type this to execute it without leaving the editor:

:! %:p 
  • : for command mode
  • ! to run a shell command
  • % to refer to the file in the current buffer
  • :p to use the full path of the current file

Is there a shorter shortcut for this frequent task?

e.g. there is a ZZ shortcut for :wq, etc.

like image 592
Robottinosino Avatar asked Mar 16 '13 13:03

Robottinosino


People also ask

How do I run a file in Vim?

Open a new or existing file with vim filename . Type i to switch into insert mode so that you can start editing the file. Enter or modify the text with your file. Once you're done, press the escape key Esc to get out of insert mode and back to command mode.

How do I run a script in Vim?

You can always execute Vimscript by running each command in command mode (the one you prefix with : ), or by executing the file with commands using a :source command. Historically, Vim scripts have a . vim extension. Here, :so is a short version of :source , and % refers to the currently open file.

How do I run a file in vi editor?

To use vi on a file, type in vi filename. If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.

How do I run a .Vim file in Linux?

To start using vim, just run the "vim" command on the Linux shell followed by the path of the file that you want to edit. [enter] means to press the return or enter key on your keyboard. The word --insert-- will appear at the bottom of the editor window to show that you are in insert mode now.


2 Answers

If you haven't set permissions you can run:

:! sh % 
like image 29
James Dunmore Avatar answered Sep 17 '22 18:09

James Dunmore


:!%:p 

,without the spaces, is shorter.

If you want an even shorter shortcut, you can create a custom mapping:

nnoremap <F9> :!%:p 

or the more "mnemonic":

nnoremap <leader>r :!%:p 
like image 119
romainl Avatar answered Sep 17 '22 18:09

romainl