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 fileIs there a shorter shortcut for this frequent task?
e.g. there is a ZZ
shortcut for :wq
, etc.
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.
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.
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.
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.
If you haven't set permissions you can run:
:! sh %
:!%: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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With