Linux System Administration: Vi Cheat Sheet and Editing Exercises, Summaries of Linux skills

A vi cheat sheet for opening, editing, and closing files, as well as various editing commands, movements, and searches. It also includes exercises for practicing these skills. Useful for Linux system administration students or anyone looking to learn vi text editor.

Typology: Summaries

2021/2022

Uploaded on 07/05/2022

allan.dev
allan.dev 🇦🇺

4.5

(86)

1K documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
% Linux System Administration
Editing
Commands preceded with "$" imply that you should execute the command as a general user
- not as root.
Commands preceded with "#" imply that you should be working as root.
Commands with more specific command lines (e.g. "rtrX>" or "mysql>") imply that you are
executing commands on remote equipment, or within another program.
vi Cheat Sheet
Open and edit a file
vi filename (fn=filename)
vi -r filename Recover a file from a crashed session
vi + filename Place the cursor on last line of file.
vi +n filename Place the cursor on line "n" of file.
vi +/pat filename Place cursor on line with first occurrence of "pat"tern
Close
:w Write the file to disk. Don't exit.
:w! Write the file to disk even if read/only.
:wq Write the file to disk and exit.
:wq! Write the file to disk even if read/only and quit.
:q Quit the file (only if no changes).
:q! Quite the file even if changes.
Movement
A Move to end of line, change to insert mode.
h Move 1 space backwards (back/left arrow).
j Move down 1 line (down arrow).
k Move up 1 line (up arrow).
l Move 1 space forwards (forward/right arrow)
w Move cursor to start of next word.
W Same as "w".
b Move cursor to start of previous word.
B Same as "b".
:n Go to line number "n" in the file.
Editing text
i Enter in to input mode.
o Add a line below cursor and enter in to input mode.
x Delete character (del key in some cases).
D Delete line from right of cursor to end of line.
dd Delete entire line.
u Undo last edit or restore current line.
p Put yanked text before the cursor.
yy Yank current line.
pf3
pf4
pf5

Partial preview of the text

Download Linux System Administration: Vi Cheat Sheet and Editing Exercises and more Summaries Linux skills in PDF only on Docsity!

% Linux System Administration

Editing

  • Commands preceded with "$" imply that you should execute the command as a general user
    • not as root.
  • Commands preceded with "#" imply that you should be working as root.
  • Commands with more specific command lines (e.g. "rtrX>" or "mysql>") imply that you are

executing commands on remote equipment, or within another program.

vi Cheat Sheet

Open and edit a file

vi filename (fn=filename) vi -r filename Recover a file from a crashed session vi + filename Place the cursor on last line of file. vi +n filename Place the cursor on line "n" of file. vi +/pat filename Place cursor on line with first occurrence of "pat"tern

Close

:w Write the file to disk. Don't exit. :w! Write the file to disk even if read/only. :wq Write the file to disk and exit. :wq! Write the file to disk even if read/only and quit. :q Quit the file (only if no changes). :q! Quite the file even if changes.

Movement

A Move to end of line, change to insert mode. h Move 1 space backwards (back/left arrow). j Move down 1 line (down arrow). k Move up 1 line (up arrow). l Move 1 space forwards (forward/right arrow) w Move cursor to start of next word. W Same as "w". b Move cursor to start of previous word. B Same as "b". :n Go to line number "n" in the file.

Editing text

i Enter in to input mode. o Add a line below cursor and enter in to input mode. x Delete character (del key in some cases). D Delete line from right of cursor to end of line. dd Delete entire line. u Undo last edit or restore current line. p Put yanked text before the cursor. yy Yank current line.

Searching

/pattern Search for "pattern" in the file going forwards. ?pattern Search for "pattern" in the file going backwards. n Find the next occurrence of pattern found forwards. N Find next occurrence of pattern found backwards.

Copy/Cut and Paste

nyyp Copy n lines to buffer, paste below cursor nyyP Copy n lines to buffer, paste above cursor nddp Cut n lines and copy to buffer, paste below cursor nddP Cut n lines and copy to buffer, paste above cursor

Exercises

Practice using vi

Remember The vi editor uses "modes"

The easiest thing to do if you get confused in vi is to press the ESCape key a couple of times and

start over with what you were doing.

To begin log in or use a terminal with your sysadm user already logged in and do:

$ cd $ vi temp.txt

vi will create the file "temp.txt" for you.

Inserting text

Press the "i" key to switch to input mode.

Type something like:

VI is great! I think I'll be using vi from now on instead of Word I like VI!

Save the file

To save the file, do:

  • Press the ESCape key for command mode
  • Type ":wq" to save and quit the file (notice the ":" before the "wq"!).
  • Remember to press (or ) after entering a command

Edit an existing file

Copy a large file to your home directory so that you can play around with some more vi commands.

We'll copy over the file /usr/share/aptitude/COPYING for this exercise. To do this do:

Search and replace

Go to the top of the file, replace all occurrences of "GNU" with "COW", but prompt for each

change:

:%s/GNU/COW/gc

Answer "y" (yes) or "n" (no) to a few prompts then escape from this mode by pressing CTRL-C.

Go to line 1, search for "kernel", move to the end of the line, add some text:

/kernel SHIFT-A "some text"

Now let’s exit from the file and not save the few changes we’ve made.

:q!

press

Practice, Practice, Practice!

As you should be able to see vi is extremely powerful as an editor, but not necessarily intuitive. The

best way to get good at using vi is to practice.

Make sure you are logged in as sysadm, then do the following:

$ cd $ vi COPYING

In this file practice some of the following elements:

  • Moving around
    • By word
    • End of line
    • Start of line
    • Top of file
    • Bottom of file
    • To an absolute line number
  • Copying and pasting multiple lines (use vi commands)
  • Copying and pasting single lines (use vi commands)
  • Copying and pasting multiple lines (use your mouse buffer)
  • Copying and pasting single lines (use your mouse buffer)
  • Search for items backwards and forwards
  • Replacing text

And, anything else you wish to practice.

Repeat the exercises above using another editor

Use joe, nano, jed, etc.