A common sequence of events when editing files are to make a change and then need to test by executing the file you edited In a shell. If you're using VIM, you could suspend your session (CTRL-Z), and then run the command in your shell.
That's a lot of keystrokes, though.
So, instead, your could use VIM ' s built-in "run a shell command"!
:!{cmd}Run A shell command, shows you the output and prompts your before returning to your current buffer.
Even sweeter, is-use the VIM special character for current filename:%
Here ' s ' are in:! %action!
A few more helpful shortcuts related to executing things in the shell:
- :!By itself, runs-external command (from your shell history)
- :!!Repeats the last Command
- :silent !{cmd}Eliminates the need to hits enter after the command was done
- :r !{cmd}Puts the output of $cmd into the current buffer
--------------------------------------------------------------------------------------------------------------- --------------------------------------
Vim is a powerful editing tool, but there was some things it just can ' t do. However, Vim lets you access shell commands and utilities without leaving Vim, and that lets you perform some amazing tric Ks.
If you run:shellor just when you:sh' re in the editor, Vim (or Gvim, if you ' re partial to Vim's GUI) would place your in An interactive shell. You can run whatever commands your want, and resume your Vim session by exiting the shell.
As most of other *nix applications, you can also pause Vim withCtrl-z, which'll drop you back to the shell. When you ' re finished, you can resume the Vim withfg. (This was a feature of the shell, not a Vim feature.)
Ever started editing a file, made a bunch of changes, and then typed to:wwrite your changes, only to find th At the file is Read-only? You can deal with this in a couple of ways, but one of the easiest things to do are to invoke a shell from within Vim and C hange The file ' s permissions before you save it again.
bang!
Vim also allows-execute a command directly from the editor, without needing to drop-to-a shell, by using bang () followed by the command to be run. For instance, if you ' re editing a file in Vim and want to find out how many words is in the file, run
:! wc %
This tells Vim to run the file (%) through thewcutility and the results. Vim would display the command output at the bottom of the editing window until you pressEnter. Note that this command runs the file through the command, and not the contents of the buffer – so if you haven ' t saved re cently, it won ' t report your most recent word count.
Bang works best with non-interactive commands. You wouldn ' t want to runtopor another interactive command using:! command, but do could drop to a shell and run such a co Mmand with:shorCtrl-z.
The Bang command can be useful if you ' re using a Vim for programming. If you ' re writing a PHP script, for example, you could use PHP's syntax check option () to see-lIf your script have any Syntax errors:
:! php5 -l %
If you ' re working on a script or project, you might want to check it regularly, and with minimal typing. You can scroll through Vim's command history by using the-arrow, but if you just want to rerun the last external Comman D, you can use:!!instead.
Reading command output
Most Vim users is already familiar with using the Read command, which inserts text from a specified file to the current Buffer, like so:
:r textfile
This was handy, but many users aren ' t aware so can also read in the output of shell applications. For example, if wanted to include a list of files from a specific directory, you could include them using this read co Mmand:
:r ! ls -1 /home/user/directory
This tells Vim-to-execute the command and then redirect the output of that command into thels -1 /home/user/directorybuffer. You could also use this feature to read the text from a Web page into the file which you ' re editing, using a text-mode brow Ser such as w3m. It's pretty simple to grab a page using w3m and dump it right into your editing session without leaving Vim:
:r ! w3m http://en.wikipedia.org/wiki/Vi -dump
The-dumpoption tells W3m to simply spit out the Web page as plain text and exit.
It's also possible to the execute multiple commands to process your the text before reading it into Vim. A Simple example would is to list the directory contents and then pipe the output to the sort command to sort the filenames I n reverse order, before inserting the text into the current buffer:
:r ! ls -1 /home/user/directory | sort -r
The read command can also come in handy if you ' re prepping a incident report from server logs. Let's say you wanted to include all of the errors in an Apache log that include a specific string. You could grep the log and insert the input into your Vim session:
:r ! grep string /var/log/apache2/site-error.log
Vim makes it easy to redirect the output of more than *nix utilities into a file, and to pipe text from the file it ' s Editing into the standard *nix utilities.
Filtering text through external filters
While in Vim, you can select a range of text and run the text through an external command. In visual mode, just highlight the text of your want to work with, then run :! commandname . The highlighted text would be replaced by the output of the command. Let's say you wanted to use weak encryption in part of a file by running it through the ROT13 utility. use v, v, or ctrl-v to Enter the visual mode of your choice and select the text you want to encrypt. Then run ,:! rot13to replace the selected text with rot13-encoded text. This might is handy when composing an e-mail containing a spoiler about the latest episode of Battlestar Galactica for sending to a mailing list. It's trivial for recipients to decode rot13-encoded text and the folks who don ' t like spoilers won ' t has any surprises R Uined by skimming over the text.
You can also specify a range of lines to process, rather than selecting lines in visual mode. For instance, to filter lines through of a file through ROT13:
:20,25 ! rot13
Vim would run the text through ROT13 and insert it in place of the existing text. If you accidentally overwrite some text in vim so you didn ' t mean to, don ' t worry--Vim ' s Undo Command (u) allows Y OU to restore the original text.
Using a different shell
By default, the Vim should use your default shell. If you want to make sure that Vim's using your default shell, you can check the value of the shell option by running this Query
:set shell ?
Vim would displayshell=/bin/bash, or whatever the shell is set to. Note that this is works with other options as well and so you can use it to check for the value of any:set option ?option within Vim.
If you ' d like to change the value of the shell temporarily, run with the:set shell=/path/to/shellcorrect path to your chosen shell. For instance, on Ubuntu, if you want to use the Korn shell, you can run:set shell=/usr/bin/ksh--assuming you have the Korn shell Installe D.
Shells permanent, open your ~/.VIMRC and add this line:
set shell=/path/to/shell
The next time you start a Vim session, the new shell option would be set.
Get in the habit of using Vim's:shellcommand:!and the utility, and you'll find that you can is even more productive The while using the Vim than you might has thought possible.
Vim tips--working with external commands