VIM: A Tool for Learning Puppet
The other posts in this series are the intro, which includes instructions for downloading a virtual machine; a tutorial for learning the command line interface, and, and a tutorial for learning Git, a popular version control tool.
This is the third blog post in our tutorial series about the tools you need to know to learn Puppet. They are necessary if you're going to take Puppet Fundamentals training, and are also generally useful when working with open source tools and the enterprise tools derived from them. Here we're going to learn how to use vim, a popular text editor that's free and open source.
Let’s have a quick review, shall we? First we installed the Puppet Labs Learning VM (virtual machine) so we would be looking at the same computer. Then we learned how to navigate around that VM using command line. You are probably a little numb to crazy things happening to your computer by now. Let’s feel some terror again.
ball_of_yarn.txt in our last exercise, and now we are going to use vim to go inside it and fill it with words. Keep this blog visible, grab your towel, and don’t panic.
vim ball_of_yarn.txt in your terminal.
Everything went dark! Don’t worry — you and your terminal are not dead, you are just in vim. If you were to try to type something (don’t!), it would act in bizarre and terrifying ways. This is because when you first open vim, you are in what is called Normal mode. That’s the mode in which you tell the computer to do things to your file, rather than just typing inside of it. In order to type, we will want to be in Insert mode.
Go ahead and type
i to enter Insert mode.
You should see
--INSERT-- at the bottom of the window. Now when you type something, it should appear at the top of the window. Go ahead and let your cat write a letter.
Once the cat is done inserting text, the two of you need to get back into Normal mode to save the letter as a file. Do this by hitting the Escape key (esc on most keyboards). Type
:w to save the file as is, or type
:w newfilename.txt to save it as a new file.
Kitty’s done now, so we should quit vim by typing
! to say, "Yes, I’m sure I want to quit." If you want to combine these, you could type
:wq! to say, "write the file, quit the file, yes, I’m sure!"
If there’s a fire alarm, and you just don’t have time for that colon,
ZZ will save and quit for you. If what you typed caused the fire, maybe you want to type
ZQ to quit without saving your flammable monstrosity.
:wwrites your file
:w newname.txtwrites your file and names it newname.txt
:wq!saves and quits
ZZsaves and quits vim
ZQquits vim without saving
Damn young kids won't even get this... pic.twitter.com/e9Mul1w27k— Ryan Johnson // (@tenthirtyam) June 11, 2014
If you want to read Kitty’s letter after quitting vim, use the
cat command. It stands for “concatenate” (or catenate, but let’s not play semantics when we should be playing computer), and it’s a pretty versatile CLI command. You can use
cat and variations on it to copy contents of a file to another file, or to smoosh several files together. If you want to learn more, http://www.cyberciti.biz/faq/howto-use-cat-command-in-unix-linux-shell-script/ is a great resource for showing more nifty things that
cat can do.
Let’s check to make sure that worked. Here’s what I see when I type
dear human,, moar food,, pls.. also,, stop mocking me when i drink from the large water bowl in the bathroom.. that stuff is delicious.. i own you,, The Duke
Congrats! You (and Kitty) wrote a thing in vim!
A lot of the text editing you will be doing will actually be editing an existing file, so we should learn how to make changes. Since it's clear that typing with paws results in plenty of typos,
ball_of_yarn.txt is a good place to start. Let’s get back in with
Moving Around in Normal Mode
I could type
i to go back into Insert mode, but you may have noticed while typing in vim that you still cannot click inside the VM — if you want to move the cursor, you have to hit arrow keys. Man, that is going to be a pain in the butt! Luckily, there are a whole bunch of shortcuts you can use in vim without going into Insert mode.
Rather than moving your paws from the main row of keys, you can use:
hto move the cursor one space left
jto go down one line
kto go up one line
lto go one space right
Using these keys, I can move the cursor under each of the extra punctuation marks in turn, and type
x to delete the character, without entering Insert mode.
Sweet, we’re moving… but are we moving fast enough?
If you want to jump by more than one character, you could add a number to any of the directional commands above. For example, when we type
3h, we jump left three characters, and when we type
10j we go down 10 lines. This also works with
x, so you could type
4x to delete four characters — the character above your cursor, plus the next three to the right.
Sometimes you reaaallllllly just don’t want to count how many characters or lines are between you and where you want to be, which is why these commands are super useful:
ggtakes you to the beginning of the file
Gtakes you to the end of the file
0takes you to the beginning of the line
$takes you to the end of the line
wtakes you to the next word
btakes you back a word
You could also add a number to the beginning of a command, just like we did before. Type
3b to go back 3 words or type
2$ to go to the end of the line below the cursor. (Note: A punctuation mark — or a group of punctuation marks like “!!!” — counts as a word in vim.)
Making Things Happen
OK, now we are cruising, but are we really doing anything? Sure,
x can take out bits and pieces, but let’s learn how to do some real damage.
If you are serious about ripping something out,
d is the key you want. There’s a nifty trick where you can double down by typing
dd to delete the whole line your cursor is currently on. You can also combine it with any of the other keys we just learned, and the next thing you know, you are typing things like:
dwto delete a word
d$to delete from where you are to the end of the line
ddto delete the whole line you are on
dggto delete from where you are all the way back to the beginning of the file.
If you would like to resurrect your deleted words in a different place in the file, you can move to the new spot and hit
p to paste the most recently deleted section.
Just in case you got a little too enthusiastic and deleted more than you intended,
u is the undo command. You can hit it as many times as you need to get back to the way you actually want your doc to look.
Control + r is the re-do button in case you get overenthusiastic about the undo command.
If you would like to resurrect your deleted words in a different location, you could move to the new spot and hit
p — that will paste the most recently deleted section.
If you want to paste a copy of some text without deleting the original, you will want to learn how to “yank” text out. If you just want to copy one line,
yy will yank the whole line you are currently on (kind of like
dd deleted the whole line we were on). If you want to copy a paragraph,
yap will yank around paragraph — that is, it will yank all text above and below your cursor until it reaches a blank line in each direction. You can then move the cursor somewhere new and click
p to paste the section you just
For more complex yanking, move to the beginning of the section of text you want to select and type
v. At the bottom you will see that we are now in
--VISUAL-- mode. Move to the end of the section of text you want to select, and type
y to yank the selected text onto the clipboard.
--VISUAL-- will disappear, and you can move somewhere else and click
p to paste the section you just
y’ed out. For reference:
vbegins your selection
yends and yanks your selection
ppastes your selection
yyyanks the line you are on
yapyanks the paragraph you are on
This is particularly handy if you need to make several sections with the same format. So if The Duke wanted to send his letter to me to each of the humans in his life, he could type the following:
G$to move to the end of the letter
vto begin the selection
ggto select to the top of the letter
yto yank the selection
pto paste the selection
The Duke could then make minor changes to the text he yanked.
If you need to go digging through your entire file for a word or phrase like “i own you”, you would type
/i own you and hit
enter. vim will highlight the text. If you had a lot of text and several instances of the word you were searching for, you would scroll through them by typing
n for next (or
N for previous).
And now for one of my favorites: find and replace.
:%s/old text/new text/gc will find each of the times “old text” appears in your file and ask if you would like to replace it with “new text.” This is particularly useful with open source stuff, since you will often need to configure a file by replacing something that says “YourIPAddress” in a document with your actual IP address. When I type
:%s/own/love/gc in The Duke’s letter, “i own you” changes to “i love you.”
If you want to know what all the bits in that command mean:
:is just a marker that puts vim into command mode, just like when we were writing and quitting with
%is vim shorthand for “all of it,” so we are looking at everything in the file. (If you replaced
2,5, vim would only look through material from the second through the fifth lines.)
sstands for substitute,
gstands for global (i.e. “do this to everything in the file”) and
cis for “check.” If you left out the
/gc, all of the old text would be replaced. The
/gccommand lets you approve each instance of the replacement with
afor “all the remaining,” or
qfor “that’s it, I’ve replaced all the things I wanted to, get me out of here.”
/birdsearches for “bird”
nscrolls through the returned hits for “bird”
:%s/dogs/cats/gcreplaces all of the “dogs” with “cats”
Remember to hit Escape to enter command mode before using these commands, and to hit
i (entering Insert mode) so you can type again.
We’ve been working with text files, because I don’t know what coding language(s) your cat speaks, but vim will format coding syntax for pretty much whatever you need.
If you have an extra 30 to 60 minutes in your day and want some extra practice moving around in vim, you could go to Vim Adventures and play the first levels of this video game. (After the first levels, you will be offered a six-month license to use the remaining levels for $25.)
I don’t know about you, but that felt both pretty cool and pretty exhausting. I think we can wrap up this kitten caboodle* and consider ourselves ready to tackle the next section of this series: Git!
*TIL it’s actually “kit and caboodle.”
Tiffany Longworth is a business systems analyst at Puppet Labs.
- The command line interface is a powerful way to interact with your computer, but not intuitively obvious if you're coming from a non-Unix, non-Linux background. Read Tiffany's tutorial.
- Read the intro to tools for learning Puppet.
- Tiffany's tutorial on Git will get you started wit h this popular version control system.
- Looking for enterprise-level configuration management that's fully tested, scalable and fully supported? Download and try out Puppet Enterprise for free.