Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

VIM running external commands

News See also Recommended Links Piping Vim Buffer Through Unix Filters: ! and !! Commands Bang! Reading command output Filtering text through external filters Using a different shell
Pipes and ! command Text buffer execution Regular Expressions Searching and replacing Syntax Highlighting Multi
Windows support
Macros Cut and Paste
External commands Vimscript Folding Indenting Your Code in VIM Ctags      
Perl support tex commands Vimrc Examples .exrc files History Tips Humor Etc

Vim is a powerful editing tool, but there are 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 tricks.

If you run :shell or just :sh while you're in the editor, Vim (or Gvim, if you like Vim's GUI) will place you in an interactive shell. You can run whatever commands you want, and resume your Vim session by exiting the shell.

As with most other applications, you can also pause Vim with Ctrl-z, which drops you to the shell. When you're finished, you can resume Vim with fg. (This is a feature of the shell, not a Vim feature.)

For example if you started editing a file, made multiple changes, and then typed :w to write your changes, only to find that the file is read-only? You can deal with this situation by opening another terminal window and changing permissions, but it is easier to invoke a shell from within Vim and change the file's permissions before you save it again.

Bang!

Vim  allows you to 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 are in the file, run

:! wc %

This tells Vim to run the file (%) through the wc utility and report the results. Vim will display the command output at the bottom of the editing window until you press Enter. Note that this command runs the file through the command, and not the contents of the buffer -- so if you haven't saved recently, it won't report your most recent word count.

Bang works best with non-interactive commands. You wouldn't want to run top or another interactive command using :! command , but you could drop to a shell and run such a command with :sh or Ctrl-z.

The bang command can be useful if you're using Vim for programming. If you're writing a PHP script, for example, you could use PHP's syntax check option (-l) to see if your script has 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 up arrow, but if you just want to rerun the last external command, you can use :!! instead.

Reading command output

Most Vim users are already familiar with using the read command, which inserts text from a specified file into the current buffer, like so:

:r textfile 

This is handy, but many users aren't aware that you can also read in the output of shell applications. For example, if you wanted to include a list of files from a specific directory, you could include them using this read command:

:r ! ls -1 /home/user/directory

This tells Vim to execute the command ls -1 /home/user/directory and then redirect the output of that command into the buffer. You could also use this feature to read the text from a Web page into the file that you're editing, using a text-mode browser 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 -dump option tells w3m to simply spit out the Web page as plain text and exit.

It's also possible to execute multiple commands to process your text before reading it into Vim. A simple example would be to list the directory contents, then pipe the output to the sort command to sort the filenames in 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 an 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 most standard *nix utilities into a file, and to pipe text from the file it's editing into standard *nix utilities.

Filtering text through external filters

While in Vim, you can select a range of text and run that text through an external command. In visual mode, just highlight the text you want to work with, then run :! commandname . The highlighted text will be replaced by the output of the command. Let's say you wanted to use weak encryption on 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 :! rot13 to replace the selected text with rot13-encoded text. This might be handy when composing an email 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, but the folks who don't like spoilers won't have any surprises ruined 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 20 through 25 of a file through rot13:

:20,25 ! rot13

Vim will run the text through rot13 and insert it in place of the existing text. If you accidentally overwrite some text in Vim that you didn't mean to, don't worry -- Vim's undo command (u) allows you to restore the original text.

Using a different shell

By default, Vim uses your default shell. you can check this with the command:

:set shell ?

Vim will display shell=/bin/bash, or whatever the shell is set to. Note that this works with other options as well, so you can use :set option ? to check the value of any option within Vim.

If you'd like to change the value of the shell temporarily, run :set shell=/path/to/shell with the correct path to your chosen shell. For instance,  if you want to use the ksh93, you can run :set shell=/usr/bin/ksh93 -- assuming you have the ksh93 installed.

To make the change of shells permanent, you need to edit your ~/.vimrc:

set shell=/path/to/shell 

The next time you start a Vim session, the new shell option will be set.

You can also experiment with using Perl as a shell.


Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News ;-)

VIM Tip of the Day: running external commands in a shell

A common sequence of events when editing files is 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, you could use vim's built-in "run a shell command"!

:!{cmd} Run a shell command, shows you the output and prompts you before returning to your current buffer.

Even sweeter, is to use the vim special character for current filename: %

Here's ':! %' is in action!

A few more helpful shortcuts related to executing things in the shell:

Selena Deckelmann at

6 comments:

depesz said...
you can also use "%!" to pass current buffer via some filtering command.

For example : %!nl - to number lines (different from :set nu), :%!wc to check word count on current buffer, :%!tac to reverse order of lines, and so on.

March 10, 2009 12:24:00 PM EDT
Jon Jensen said...
Yes, and you can also send line ranges rather than the whole buffer, for example, lines 4-7:

:4,7! sort -u

or line 4 through end of file:

:4,$! sort -u

And when a visual selection is active, that will automatically be the range used:

:! sort -u

March 10, 2009 12:31:00 PM EDT
Christopher Nehren said...
And in a homologous fashion, one can read the results of an external command by passing the command to run to the :r command like so:

:r !/bin/ls

March 10, 2009 12:31:00 PM EDT
Jon Jensen said...
Chris, that's the final example Selena gave in the original post, isn't it?
March 10, 2009 12:33:00 PM EDT
Christopher Nehren said...
Yes, indeed. I must have missed that part somehow. Apologies for the noise.
March 10, 2009 12:35:00 PM EDT
Selena Deckelmann said...
@depesz & Jon: Yes! Sorting is one of @gorthx's (Gabrielle Roth) favorite uses :)

Thanks for sharing!

March 10, 2009 1:14:00 PM EDT

Basic vi Skills for the Linux LPIC Exams Advanced vi

InformIT

Several tasks are part of vi that don't fit in any other section. Most of these are quite advanced, such as running external commands, joining lines, and splitting windows. This section covers these in detail.

Running External Commands in vi

A frequent question on the exams is how to run an external command inside vi, such as seeing an ls -l listing of the current directory so you can remember a filename:

:! ls -l

In this, the command ls -l executes, with the command's output displaying onscreen, and you need only press Enter or enter a command to return to the vi session. If the output scrolls more than one screen, it's piped to the more command and all the normal movement keystrokes will apply.

Joining Lines

It's quite irritating in vi to be at the front of a line and want to use the Backspace key to move that line to the end of the previous line. The Backspace key works only on the current line. If you need a line to be joined to the previous line, you can position the cursor in either line and press Shift+J to cause the second to be appended to the end of the first line.

Say you have a file named file1 that contains the following text:

This is line 1
This is a longer line 2
This is an even longer line 3

You want to join line 1 and line 2, so you position your cursor somewhere on line 1 and press the J key. The file then looks like the following:

This is line 1 This is a longer line 2
This is an even longer line 3

Putting the cursor on line 2 and pressing J joins line 2 and line 3 in a similar fashion.

Split Windows

Last, but not least, is splitting windows in vi, specifically the vim version of vi. When you're editing a particular file and want to see either another section of that same file or even another file altogether, you can use the following:

Moving between the panes is somewhat counterintuitive because you must press Ctrl+W twice to move between the windows.

To edit a completely different file, you should edit the first one in vi; then to split the screen horizontally with the other file loaded in the second pane, you can enter

:split file2

To set the height of the newly split window from the split, you could enter the following:

:10split /etc/fstab

This command splits the top 10 lines of the screen and displays the contents of the /etc/fstab file therein.

If you wanted to close the split window, you would focus on it by pressing Ctrl+W and then entering

:close

Better yet, after comparing something or getting a filename straight, you can close all the other split windows and just have the current window open by entering the following:

:only

NOTE

Many times I've opened a couple of split windows to see the difference between two files or used the diff command. The easiest way to compare two files and make edits to them is to use the vimdiff command, such as

:vimdiff file1 file2

This loads the two files into a vertically split vim session and uses color and other symbols to show you what is similar or different between the two files. This is useful for comparing a log file before and after a problem or two configuration files to see why one doesn't work.

Recommended Links

Google matched content

Softpanorama Recommended

Top articles

Sites

Top articles

Sites



Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to to buy a cup of coffee for authors of this site

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: March 12, 2019