Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

External Commands via Vim

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.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 30m
Published
Clock icon Apr 03, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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:

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

      :shell
      
    3. Run the ls command and inspect the output.

    4. Display the user information with:

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

      exit
      
    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: :q

    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.

  2. Challenge

    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:
      Shift-v
      11j
      
    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 ENTER
    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: ```
    13. Starting Vim ```
    14. Then press K to initiate a lookup of the Vim man page.
    15. When done using man, press q to quit and press ENTER to return to Vim.
    16. Exit Vim without saving any changes to the vimwindows.txt file with: :q!
  3. Challenge

    Record and Playback a Vim Macro/Recording

    1. Open Vim with:

      vim
      
    2. Start a recording by pressing:

      qz
      
    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.
      ESC
      
    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:

      !!date
      
    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: ESC @z > 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 ENTER

    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: "zp

    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: :wqa

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans