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

Editing Text with Vim

This lab is designed help us work through the basics of creating, editing, and manipulating text with the VIM editor, giving you the ability to work with any text file you encounter on a Linux system, using the most installed editor, though not the simplest one.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 30m
Published
Clock icon Nov 27, 2019

Contact sales

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

Table of Contents

  1. Challenge

    Use the Vim Editor to Create, Edit, Navigate and Manipulate Text Files

    Start Vim

    Running vim alone will open up the editor, but we won't be looking at a file, just Vim itself. To get out, type :q, then press Enter.

    Open a File for Editing

    To actually edit a file, type the filename after the command. vim .profile will open up the .profile file for editing.

    Typing G (capital G) will take us to the end of the file. Type o to start a new line below that, and let's put export COLOR=Green there. If we run echo $COLOR, we won't get anything returned. Let's source our profile:

    source .profile
    

    Now if we run echo $COLOR again, we'll get Green printed out to the screen.

    Open Another File

    Let's grab a new file to edit:

    cp /usr/share/doc/packages/zip/README ./zip-readme.txt
    

    Now get into the file again, but with an option. Let's say we know that the word Testing is in that file, and we want to jump right down to it. This will do that for us:

    vim zip-readme.txt +/Testing
    

    Inserting New Lines Above or Below the Current Position

    In command mode, type O to insert a new line above the current cursor position, or press o to insert a line under. Both of these will put us on that new line in insert mode. Hit Esc to get out of insert mode and play with running each of those commands.

    Undo and Redo

    If we're not happy with a change, getting into command mode and pressing u will undo it. To redo an undone change, press Ctrl + r.

    Cutting, Copying, and Pasting

    To copy a whole line of text, press yy while we're sitting on the line we want to copy. Press p to paste that onto a new line below. Pressing P would have pasted it above the current line.

    To paste multiple instances of that line (5, for instance) we'd preface the letter p (or P) with 5: 5p

    dd, when typed in command mode, will cut a line. To cut four lines, simply preface that with a 4: 4dd

    Moving Around

    Here are several keystrokes, run from command mode, that will move us around in Vim:

    • ^: Go to the beginning of a line
    • $: To go to the end of a line
    • h: Go left one character
    • j: Go down one line
    • k: Go up one line
    • l: Go right one character
      • Pressing a number before these last four commands will take us that many places (4l to go four spaces right, 7j to go seven lines down, etc.)
    • w: Prefaced by a number, will take us forward that many words
    • b: Prefaced by a number, will take us back that many words

    Exiting Vim

    The keystrokes Esc (to get out of insert mode) followed by :q! will get us out of Vim without saving any changes. If we want to save the changes, ZZ (those are capital) will save and exit. So will :wq if we'd rather.

  2. Challenge

    Use the Vim Editor to Alter and Transform Text and Perform Basic Searches and File Operations

    Let's get back into that text file, but come in with the cursor right on line 20:

    vim zip-readme.txt +20
    

    If we look down to the right, we'll see that we are in fact on line 20, at column 1. Let's navigate up to the paragraph that starts with Testing.

    Some More Moving

    Press w or b with no preceding numbers to just go forward and backward a word at a time. Pressing W or B (uppercase) will do the same thing, but it will ignore punctuation.

    Joining

    Pressing J will join the line below where we at with the line we're currently sitting on.

    Transforming Words

    If we get the cursor on a word (in command mode) and press cw (Change Word), then that word is deleted and we're in insert mode, ready to type in the word we're replacing it with. Let's just make sure when we do this that we're at the beginning of the word. cw will start the replace wherever our cursor is.

    To just replace a character, press r and type what we need changed (swapping a hyphen for an underscore, for instance).

    Changing Case

    With the cursor over a letter, in command mode, press ~ (the tilde key). U becomes u, and vice-versa.

    Even cooler, if we type 50~, then the next fifty characters will go to the opposite case. Something like This is sentence case will be transformed to tHIS IS SENTENCE CASE (well, in this case it would have been 21~)

    Mass Changes

    In command mode, if we type /Zip, we're going to land at the first instance of the word Zip. Press n to go to the next instance. If we want to change all of them in the file to ZIP, then we'd type :%s/Zip/ZIP/g. The g here means global. If we leave the g out, then only the first instance of Zip would be changed in each paragraph.

    Getting File Information

    Let's look at some information now, about the file. From command mode, pressing Ctrl + g will bring up the file name, when it was modified last, how many lines are in it, and how far down the file (percentage-wise) our cursor is.

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