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.
Successfully complete this lab by achieving the following learning objectives:
- Use the Vim Editor to Create, Edit, Navigate and Manipulate Text Files
vimalone 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 .profilewill open up the
.profilefile 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:
Now if we run
echo $COLORagain, 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
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
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
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.
Pressing J will join the line below where we at with the line we’re currently sitting on.
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).
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~)
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.