In a very specific lab session (
CS130 Fall 2017, Lab 1), we asked you to type in
gedit
to open up a graphical text editor.
Unfortunately, whenever you do work remotely,
everything is via the terminal.
You will not have the choice to use the graphical user interface. So gedit won't work.
Thankfully, there are terminal based text editors that will help you get the job done... and they are typically more powerful than the graphical ones anyways.
Introducing
vim
...
Startup
Vim can be very intimidating at the start, because it's unorthodox to the average user. But it has some true power behind it.
To begin, let's open it up. Type the following command:
UNIX Command
vi test.cpp
Note: vi
and vim
are not the same (the latter improves on the former immensely). However, on most machines out there, vi
is aliased to vim
.
So you opened up Vim, but now you're presented with a blank screen!
"My Vim doesn't look like yours! What gives?"
Someone asked me about this in lab so I figured I'd mention it here.
Applications such as Vim allow the user to customise how it looks.
I wrote my own theme for it, but the functionality is the exact same as yours.
Control Cheatsheet
Vim isn't like a normal text editor, but its own controls have become popular enough that other text editors have a "Vim mode".
Your controls are based on what "
mode" you are in. Here is a list of common modes:
- Normal Mode: This is the mode you start out in. You may use the arrow buttons (or Vi's HJKL setup)
to navigate around. You may also press the colon button ":" ("Shift + ;" on USA keyboards) in order to input a command (more on that below). Press "i" to enter Insert Mode, etc.
- Insert Mode: Pressing "i" while in Normal Mode triggers this mode. This is where Vim acts like a text editor that you're used to. Type stuff on your keyboard and it types. Press Esc to exit Insert Mode and go back to Normal Mode.
- Visual (and Visual Block) Mode: Pressing "v" while in Normal Mode triggers this Visual Mode. Pressing "V" will trigger Visual Block Mode. I will cover these in a more advanced guide.
In
Normal Mode, you have commands which you can execute by typing in the ":" character. The only essential ones you need to know are "w", "q" and "q!".
Command |
Description |
Usage(s) |
:w |
Writes a file to the disk. |
:w |
:q |
Quits Vim. If you made changes to a file, you must put "!" after in order to quit without saving changes. Otherwise it'll refuse to quit. |
:q
:q!
|
:wq |
Writes to disk, and then Quits Vim. |
:wq
|
When in
Normal Mode, you can also perform copy and paste of lines easily.
There are a few other things you can do too such as deleting, and repeating actions done in Insert Mode. I showed these in lab too.
Note: These do not use ":". They are commands bound to buttons in Normal Mode that do not require it.
Command |
Description |
i |
Triggers Insert Mode. |
v |
Triggers Visual Mode. |
V |
Triggers Visual Block Mode. |
yy |
Yank. Grabs a line and puts it in clipboard (For Copy and Paste). |
y2y |
Yank. Grabs 2 lines and puts them in clipboard (For Copy and Paste). |
cc |
Cut. Grabs a line and puts it in clipboard (For Cut and Paste). It will also delete that line. |
c2c |
Cut. Grabs 2 lines and puts them in clipboard (For Cut and Paste). It will also delete those lines. |
dd |
Delete. Deletes the current line. |
d2d |
Delete. Deletes 2 lines starting from the current one. |
p |
Paste. Pastes whatever was inside the clipboard from the above commands. |
u |
Undo. Undoes the last action performed. |
C-r (Ctrl+R) |
Redo. Redoes the last action performed if undone. |
. |
Any changes done in Insert Mode are done again. |
You can also do string replacement via regular expression commands, but I do not expect you to remember these. I showed these in lab.
Command |
Description |
:s/old/new |
Substitution. Replaces one case of old with new. |
:%s/old/new |
Substitution. Replaces one case of old with new... for each line (%). |
:s/old/new/g |
Substitution. Replaces ALL (g) cases of old with new in a line. |
:%s/old/new/g |
Substitution. Replaces ALL (g) cases of old with new in a line... for ALL lines (%). |
In case you can't see the pattern...
% means the expression will take effect on all lines... once.
g at the end means that the expression will replace all cases on a single line.
If you combine them, you get it affecting all cases on all lines.
I'll make a more in-depth Vim guide whenever the time comes. Since this is the basics for the terminal, I won't focus much more on Vim.