Skip to main content

Editing Text with Vim

Hands-On Lab

 

Photo of Ross Brunson

Ross Brunson

Linux Training Architect II

Length

00:30:00

Difficulty

Beginner

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Editing Text with Vim

Introduction

Being able to edit text in the available editor is a real time-saver, and can be a reputation enhancement step. Sysadmins should have basic editing skills in both VIM and nano, and can use whatever editor they choose. But sometimes only one is available on the system. It would not be a good step to have to install your favorite editor to make simple configuration file changes, and some system change policies absolutely forbid it.

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.

The Scenario

We have been contracted to manage a server for a client who has not installed the GUI desktop or subsystem, in order to save on resources. Rather than being able to use our favorite commercial GUI editor, we are forced to use the Vim editor for all system configuration text editing.

Logging In

Use the credentials provided on the hands-on lab overview page, and log in as cloud_user.

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

Use the Vim editor to create new buffers/files, and open existing files, with options for cursor placement. Edit existing files, inserting text and adding lines as needed. Undo changes and redo changes to suit the need. Cut, copy, and paste text and lines as needed, and navigate by words and lines throughout the buffer/file.

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.

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

Use the Vim editor to delete text by character, word, line or from the current cursor. Change and alter text, including changing text case. Display further file and buffer information to help with editing and navigation. Save changes to disk and quit buffers/files in an appropriate manner, saving or discarding changes. Additionally, conduct simple searching for text in a buffer/file.

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.

Conclusion

Vim is a powerful text editor. Knowing how to use it is invaluable to any sysadmin who needs to edit text files in a command line. Congratulations on getting some Vim experience under your belt. Practice, and use it whenever you can.