External Commands via Vim

Get Started
30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

One of the greatest things about Linux is the ability to have multiple commands work together to accomplish more than they could separately. Using Vim right along with those other commands allows you to do things not otherwise possible.

In this hands-on lab, you’ll invoke external programs from inside Vim (such as the Bash shell), import the contents of files into Vim, send buffer contents out to external programs and back into the buffer, and write Vim buffers to external files and programs to accomplish tasks.

Lastly, we’ll record a set of tasks, edit the recordings and set Vim to load them on start.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Running an External Shell, Running External Commands in Vim, and Reading File Contents in to a Buffer
  1. Create a working file (and open it for editing) with:

    :help windows
    :w ~/vimwindows.txt
    :e ~/vimwindows.txt
  2. Open a shell session from inside Vim with:

  3. Run the ls command and inspect the output.

  4. Display the user information with:

  5. Exit the bash shell and return to Vim with:

  6. Now run a command from within Vim to display the files in local directory with:

    :!ls -l
    ENTER   (To return to Vim)
  7. Execute a command that will write output to a file on disk and return without the ENTER prompt with:

    :silent !ls -l > lsoutput.txt

    IMPORTANT: You may have to issue the :redraw command or use Ctrl-l to refresh the Vim session screen.

  8. Grab some text from a help file and write it to a file:

    :h toc
  9. Highlight the block that starts with usr_01.txt and ends with usr_12.txt by pressing Shift-v and using the cursor keys to highlight all 12 lines, then write those 12 lines to a file with:

    :'<,'>w block.vim   (Vim will add the '<,'>)
  10. Exit help with:

  11. Cause the block.vim file contents to be imported into the current buffer starting at the top of the file with:

    :0 read block.vim
  12. Next, add the block.vim file contents to be imported into the current buffer at the bottom of the buffer with:

    :$ read block.vim
  13. Now add the contents of block.vim into the current buffer starting at line 25 with:

    :25 read block.vim
  14. Use the u undo feature to return the file to its original state.

Importing External Command Output, Using External Commands on Buffers, and Writing Buffer Contents to External Commands

Note: You must have done Steps 1 and 2 in Task 1’s to be able to do Task 2.

  1. With the ~/vimwindows.txt file open in Vim, import a command’s output and add it to the top of the file with:
    :0 !ls -l
  2. Undo the import by pressing u until you read the original file state.
  3. Insert the output of the cal command into the buffer starting at line position 15 with:
    :15 read !cal -m3
  4. Undo the import by pressing u until you read the original file state.
  5. Go to the top of the file with gg, then select the 12 numbered lines that start with 1. Introduction and end with 12. Special:
  6. Sort the lines inside Vim with:
    :'<,'>sort r    (vim will insert the '<,'>)
  7. Re-select the range with gv, then use the external sort command to sort the range from the buffer and return it to the same area with:
    :'<,'>!sort -n
  8. Re-select the range again with gv and get a word count of the range with:
    :'<,'>w !wc -w
  9. Note the number returned, and press ENTER to continue.
  10. Load the filetype plugin for looking up man pages in Vim with:
    :runtime! ftplugin/man.vim
  11. Look up a man page from inside Vim with:
    :Man 5 crontab
    q   (When done with Man)
  12. Use man for a lookup from inside Vim by highlighting the word Vim on the line that reads:
    2.  Starting Vim
  13. Then press K to initiate a lookup of the Vim man page.
  14. When done using man, press q to quit and press ENTER to return to Vim.
  15. Exit Vim without saving any changes to the vimwindows.txt file with:
Record and Playback a Vim Macro/Recording
  1. Open Vim with:

  2. Start a recording by pressing:

  3. The status line should show the message:

    recording @z
  4. Press i to enter Insert mode and the status line should show info similar to the following:

    -- INSERT --recording @z
  5. Enter the following text and press ESC:

    This is a macro recording session in Vim.
  6. Copy the line with the yy characters, and paste the line 5 more times with 5p.

  7. Go to top of the buffer with gg.

  8. Execute an external command and bring it’s output back to the buffer to replace the first line of content with:

  9. Stop the recording by pressing q and the recording @z message will disappear.

  10. Use the u feature to remove all contents in the buffer and then playback the recording with:


    Note: The recording should play back exactly as it was recorded.

  11. Check to see that the recording is attached to the z register with:

    :register z
  12. The recording contents should show near the status line, press ENTER to continue.

  13. Write the file to disk with:

    :w recordingz.txt
  14. Put the recording into the .vimrc so it’s available at load time with:

    :e ~/.vimrc
  15. Paste the z register contents (your recording) into the buffer with:

  16. Edit the line so that it roughly matches the text below (ensuring there are single quotes surrounding your recording):

    let @z='yourrecordingcontentshere'
  17. Write both the files to disk and exit with:


Additional Resources

You're getting increasingly skilled at using Vim itself, but find that you have to exit out a lot to use other programs that produce output you need inside Vim. You know Vim has some capabilities for working with external programs, so you set out to find out what Vim can do with them.

You also want to use recordings, or macros, to automate certain tasks, and wonder how far those can go with regard to external programs.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


Sign In
Welcome Back!
Thanks for reaching out!

You’ll hear from us shortly. In the meantime, why not check out what our customers have to say about ACG?