vim my way
The editor known as "vim" in Linux has many features in common with vi, the editor I learned in 1992. I was drafted to convert a slew of Vax programs to Solaris in over a few weeks. I'd never used Unix. I had a VT-220 terminal that I used on a MicroVax and telnet'd over to the Sun. The Yellow key was great for the EDT editor. It was useless on the Sun. The project manager showed me how to use vi and I started on the project right away. In 1998 I taught a class with at least 30 consultants pretty much the same way it was taught to me. Now it's your turn!
On Windows I run Windows Subsystem for Linux (how I installed
it could be a topic for another day). vi is linked to vim. If you know vim on Linux, you can use vi on
older Unix systems as well--Solaris, AIX, HPUX, etc. Out of habit, I'll still type "vi"
to start the vim editor. If you are on a
Linux desktop, pop up an xterm window to get to a shell.
vim is oriented to the touch typist. The bumps on your keyboard are FJ for your
left and right index fingers. Some keys are for navigation, others have mnemonic
functions associated with them.
Before I tell you the functions of each of the keys, let's create
a test file.
In and Out of a file
For these examples, I copied text from
https://lipsum.com/feed/html. This site
is popular with web developers who need sample text. You get a screen of Latin gibberish, which is
fine for today.
Copy the five paragraphs from your web browser. Then do the following:
Open a CMD Window (if you are on Windows).
wsl
cd Desktop
mkdir vim_basics
cd vim_basics
cat > test.txt
Right mouse-click to push text into the screen.
Click the Enter key and then control-d.
The control-d key combination ends an interactive stream.
Now you can open the file like this:
vim test.txt
To get out of the file, give the key sequence ":q" (colon qyoo) and then hit the Enter key.
The colon (:) character opens a prompt for commands. "q" for "quit" is all you
need if you don't change the file. I'll
show you other commands later.
Also try opening the file with vi and view. View is read-only vim. You can still write to the file, but you will
have to use the exclamation point to give the "yes, I am sure" response
to override the read-only mode.
:q! – yes, I’m sure I want to quit without saving changes.
:wq! – yes, I’m sure I want to write, quit and save my
changes.
:wq – Write and quit with a normal file (which is most of
the time).
This last key sequence will become second-nature the longer
you use vim.
Navigation: Down-Up-Right-Left
Starting with your right index finger, J is down. Up is your
middle finger on K.
H is left and L is right.
Yes, L is right.
So, don't look at the keys.
Just get used to the following:
Down-Up-Down-Up-Down-Up
Right-Left-Right-Left-Right-Left
Go ahead and try that out.
Navigation: Page Down, Page Up
Control-U is Page Up.
Control-D is Page Down.
Give it a try and repeat three times. Since you only have a few lines of text, you
will probably navigate to the first or last line of the file. It's more impressive when you have several
pages of text.
Navigation: Word at a time
The w key goes forward a word. The b key goes backward a word. Using shift with w or b skips punctuation,
like commas and periods. Throughout, you
may notice that Shift and Control will give you more options on a command.
Navigation: First Line, Last Line
Shift-G will Go to a line in the file.
G by itself goes to the end of the file.
1G goes to the first line of the file.
3G goes to the third line of the file.
Type a number, Shift-G,
and you're there.
Navigation: Line begin, Line end
Now if you go zero then shift-G, you will first go to the
beginning of the line and then to the end of the file. Zero (0) key puts you in the beginning of the
line. The dollar sign ($) puts you at
the beginning of the line. Zero and dollar have other meanings below.
Where am I?
Your cursor indicator in the lower right of your screen
shows where you are in terms of lines and character position within a line.
So, you've used Shift-G to go to a line in the file. But, where are you?
Control-G gives you a percentage
of your position in a file and how many total lines of text in the file. (In vi on some Unix systems, it may also give
your line number.)
Deleting
Navigate to the middle of a line and click "x".
That deletes a character. Now click
"u" to undo. If you go shift-u, it re-does the thing you just undid.
The "d" key deletes when you add other keystrokes
to it. If you want to delete a letter
(same as x), the key combination is "dl". Delete a word is "dw". Delete to the
end of the line is "d$". Delete to the beginning is
"d0". Deleting the whole
current line is "dd".
Shift-d is the same as "d$".
Click u to put back your changes, or get out of the file with
":q!" and get back in.
Inserting
Navigate to the third line with 3G and over a few words with
w. If you click on "o"
(oh). The line below will open, and your
cursor will go the beginning of the line.
To get out of insert mode, hit the Esc or Escape key. To undo the line opening, click u. That should
put you back in the middle of the third line.
Now try Shift-O. That
opens the line above. Escape u puts it right back.
Now type i123. You
should see 123 inserted in front of the character you were on. Do Escape u to remove.
Now type a123. You now see 123 was inserted after the character
you were sitting on.
This time hit the escape key, navigate forward a couple
words and insert 456. You should see something
like this (doesn't have to be exact).
Delete a line, paste a line
Let's say you want to take the first line and move it to the
end of the file. Try the following keystrokes.
- Escape (if you are still in insert mode)
- Shift-G (goes to top of the file)
- dd (deletes the line)
- Shift-G (goes to end of file)
- p (for paste)
When you delete anything, your text goes into a local memory
buffer in case you need it again.
If you click u twice, it undoes the paste and the delete.
This time, let's try with the Shift-P.
1GddGP
(You're getting faster, so I didn't break up the example.)
You have pasted the line above instead of below.
This also works for delete word and delete character. Lower p pastes forward, Shift-P pastes backward.
Click u twice to put it back.
Yank and Paste
The copying happens with the "y" key and not the
"c" key. What you learned about delete with the d key mostly works
with yank. The text is copied to a memory buffer, but it doesn't disappear.
To copy that first line to the end, use the following sequence.
1GyyGp
Your homework is to do yanks and pastes the same way you did
deletes and pastes.
Yank or Delete Multiple Lines
You can skip this if you never want to work with more than
one line at a time. Still here? Great!
You need to "mark" the first and last line before
you yank or delete. Here is how I move the first two paragraphs to the end:
1.
- 1G to go to the top of the file, to the line that begins with "Lorem Ipsum"
- Mark to buffer "a" with keystrokes "ma"
- Navigate down two lines to the paragraph that begins with "Etiam tempor"
- Yank the lines into the "a" buffer with the sequence "y'a".
- Navigate to the end with G
- Paste with p
The bottom of your file should look like this:
Search and Replace
I'm getting tired.
Just try this and see what happens.
:1,$s/tempus/time/g
You have substituted every occurrence of "tempus"
in the file with "time". Without "1,$", you are only substituting
on the current line instead of a range of lines. Without "g", you are
only doing the first occurrence.
Type the following, followed with the Enter key.
/time
When you click on the n key, you will navigate to the next occurrence
of "time". Shift-n will search backwards.
Conclusion
Did I show you EVERYTHING you can do with vim or vi or
view? Nah. I gave you enough to get started. You have the man command and internet search
engines.
By the way, if you go "man vim", you can use some of
the same navigation commands (j,k, Control-u, Control-d, 1G, G, slash for find,
etc.) that you learned here.
Below is a cheat sheet of all the commands you learned in
the order you learned them. Cut, paste and print!
Cheat Sheet
:q! – Quit, I'm sure
:wq – Write and Quit
:wq! – Write, Quit, and I'm sure
j – Down
k – Up
h – Left
l – Right
0 (zero) – beginning of line
$ (dollar) – end of line
Control-U – Page Up
Control-D – Page Down
Shift-G – Go to Last Line
1 then Shift-G – Go to First Line
w – Go Forward a word
b – Go Backward a word
Shift-w – Go Forward a Word (skip punctuation)
Shift-b – Go Backward a Word (skip punctuation)
x – delete a character
dl – also delete a character
dw – delete a word
dd – delete a line
d0 (dee zero) – delete to the beginning of the line
d$ - delete to end of line
Shift-d – also delete to the end of the line
u – undo last command(s)
Shift-u – toggle undos
Escape – get out of insert mode
i – insert here
a – insert after this character
o – open a line below
Shift-o – open a line above
p – paste forward
Shift-p – paste backward
yl – yank a character
yw – yank a word
yd – yank a line
y0 (why zero) – yank to the beginning of the line
y$ - y to end of line
ma then y'a – mark and yank
ma then d'a – mark and delete
Shift-y – also yank to the end of the line
:1,$s/oldstring/newstring/g – search and replace
/string – find string
n – find next string forward
Shift-n – find nextstring backward
Bob's Tech Corner
"vim my way"
https://bobstech.rvnllc.com
Bob Nightingale, info@rvnllc.com
Created 28-OCT-2020
Comments
Post a Comment