- Lab
- A Cloud Guru
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.
Path Info
Table of Contents
-
Challenge
Running an External Shell, Running External Commands in Vim, and Reading File Contents in to a Buffer
-
Create a working file (and open it for editing) with:
vim :help windows :w ~/vimwindows.txt :q :e ~/vimwindows.txt
-
Open a shell session from inside Vim with:
:shell
-
Run the
ls
command and inspect the output. -
Display the user information with:
whoami
-
Exit the bash shell and return to Vim with:
exit
-
Now run a command from within Vim to display the files in local directory with:
:!ls -l ENTER (To return to Vim)
-
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 useCtrl-l
to refresh the Vim session screen. -
Grab some text from a help file and write it to a file:
:h toc
-
Highlight the block that starts with
usr_01.txt
and ends withusr_12.txt
by pressingShift-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 '<,'>)
-
Exit help with:
:q
-
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
-
Next, add the
block.vim
file contents to be imported into the current buffer at the bottom of the buffer with::$ read block.vim
-
Now add the contents of
block.vim
into the current buffer starting at line 25 with::25 read block.vim
-
Use the
u
undo feature to return the file to its original state.
-
-
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.
- 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
- Undo the import by pressing
u
until you read the original file state. - Insert the output of the
cal
command into the buffer starting at line position 15 with::15 read !cal -m3
- Undo the import by pressing
u
until you read the original file state. - Go to the top of the file with
gg
, then select the 12 numbered lines that start with1. Introduction
and end with12. Special
:Shift-v 11j
- Sort the lines inside Vim with:
:'<,'>sort r (vim will insert the '<,'>)
- Re-select the range with
gv
, then use the externalsort
command to sort the range from the buffer and return it to the same area with::'<,'>!sort -n
- Re-select the range again with
gv
and get a word count of the range with::'<,'>w !wc -w
- Note the number returned, and press
ENTER
to continue. - Load the
filetype
plugin for looking up man pages in Vim with::runtime! ftplugin/man.vim ENTER
- Look up a man page from inside Vim with:
:Man 5 crontab q (When done with Man)
- Use
man
for a lookup from inside Vim by highlighting the wordVim
on the line that reads: ``` - Starting Vim ```
- Then press
K
to initiate a lookup of the Vim man page. - When done using
man
, pressq
to quit and pressENTER
to return to Vim. - Exit Vim without saving any changes to the
vimwindows.txt
file with::q!
- With the
-
Challenge
Record and Playback a Vim Macro/Recording
-
Open Vim with:
vim
-
Start a recording by pressing:
qz
-
The status line should show the message:
recording @z
-
Press
i
to enter Insert mode and the status line should show info similar to the following:-- INSERT --recording @z
-
Enter the following text and press
ESC
:This is a macro recording session in Vim. ESC
-
Copy the line with the
yy
characters, and paste the line 5 more times with5p
. -
Go to top of the buffer with
gg
. -
Execute an external command and bring it's output back to the buffer to replace the first line of content with:
!!date
-
Stop the recording by pressing
q
and therecording @z
message will disappear. -
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. -
Check to see that the recording is attached to the
z
register with::register z
-
The recording contents should show near the status line, press
ENTER
to continue. -
Write the file to disk with:
:w recordingz.txt ENTER
-
Put the recording into the
.vimrc
so it's available at load time with::e ~/.vimrc
-
Paste the
z
register contents (your recording) into the buffer with:"zp
-
Edit the line so that it roughly matches the text below (ensuring there are single quotes surrounding your recording):
let @z='yourrecordingcontentshere'
-
Write both the files to disk and exit with:
:wqa
-
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.