Chapter 5: Text, editors and the shell environment
Exercise: Create and edit a file with vim
Work through the first lesson of vim's own tutorial, then use what it taught to write a short notice file on servera and edit it: copy a line, replace text, change your mind, and quit without saving.
Create and edit a file with vim
- Move between normal, insert and command-line mode.
- Copy, paste, delete and undo lines, and replace text.
- Save, or quit without saving.
Start the exercise
On workstation, start the exercise. It removes welcome.txt of an earlier run from student's home on servera.
[student@workstation ~]$ lab start sa-vimLater tasks assume that you are logged in to the server they name: ssh student@servera from workstation, then sudo -i for a root shell.
Do lesson 1 of vimtutor
On servera, start the tutorial:
[student@workstation ~]$ ssh student@servera
[student@servera ~]$ vimtutorRead and do lessons 1.1 to 1.6: moving with h j k l, quitting with :q!, deleting with x, inserting with i and A, and saving with :wq. Read the lesson 1 summary, then leave with :q!. The tutorial edits a copy, so nothing you do in it matters.
Write a new file
Open welcome.txt, press i, type these three lines, press Esc, and save and quit with :wq:
Welcome to servera.
Maintenance window: Saturdays 22:00 to 23:00.
Contact: admin@lab.example.com[student@servera ~]$ vim welcome.txt
[student@servera ~]$ cat welcome.txt
Welcome to servera.
Maintenance window: Saturdays 22:00 to 23:00.
Contact: admin@lab.example.comCopy a line and change the copy
Open the file again. Add a second contact by copying line 3 and editing the copy:
- Type
:3and Enter to jump to line 3. yycopies the line;pputs the copy below it, and the cursor moves to the copy.:s/admin/oncall/and Enter replaces the firstadminon the current line.
Then make the host name in line 1 complete, with a substitution over the whole file: :%s/servera\./servera.lab.example.com./ and Enter. The backslash makes the dot a literal dot, as in grep. Save with :wq.
[student@servera ~]$ cat -n welcome.txt
1 Welcome to servera.lab.example.com.
2 Maintenance window: Saturdays 22:00 to 23:00.
3 Contact: admin@lab.example.com
4 Contact: oncall@lab.example.comcat -n numbers the lines, which makes it easy to check what moved.
Change your mind twice
Open the file, go to line 2 with :2, and delete it with dd. The line disappears. Press u to undo it, and it comes back. Now quit with :q!.
[student@servera ~]$ cat -n welcome.txt
1 Welcome to servera.lab.example.com.
2 Maintenance window: Saturdays 22:00 to 23:00.
3 Contact: admin@lab.example.com
4 Contact: oncall@lab.example.comNothing changed: :q! discarded the session's edits. Remember this when a file you are editing goes wrong.
Delete a line for real
The maintenance window is cancelled. Delete line 2 and save.
vim welcome.txt, then :2, dd, :wq.
[student@servera ~]$ cat -n welcome.txt
1 Welcome to servera.lab.example.com.
2 Contact: admin@lab.example.com
3 Contact: oncall@lab.example.com
[student@servera ~]$ rm welcome.txt
[student@servera ~]$ exitGrade and finish
Check your work with lab grade sa-vim, then put the exercise away with lab finish sa-vim.