Table of contents
Introduction
Throughout the class, you will be asked to remotely login (via ssh) into a server and doing all of the labs there. Therefore, it is in your best interest to get good practice navigating your terminal and using a terminal-based text editor.
Our text editor of choice is vim, but you are free to chose nano or emacs, depending on your preferences and experience. We elected to focus on vim since you have already encountered in prior classes.
vim is already installed on most distributions. However, in case you cannot find it in your setup, you can use (assuming your are running Ubuntu or a Debian-based distribution):
$ sudo apt install -y vim
Tutorial
In case you need a quick tutorial (you should have already done this one in CSSE132), please read through the tutorial provided by vim, it should give you all the basic concepts needed to navigate your editor.
You can launch the tutorial from your shell using:
$ vimtutor
Please read through the tutorial then take some time to practice moving around a file in vim.
The basics
Operation modes
vim has several modes of operation, the three that are of interest to us are the following:
[n]ormalmode: This is where you can navigate your text and perform some operations, such as copy and paste, delete (cut) lines or words, etc.vimby default starts in normal mode.[i]nsertmode: This is where you add (or insert) your text. This is pretty much the mode in which you will be doing your regular editing.You can enter
insertmode by pressingion your keyboard when innormalmode. This will start adding text under the current cursor.To exit
insertmode and go back tonormalmode, pressing theEscapekey anywhere in your text.[c]ommand-linemode: This is where you can enter editor commands to do very useful things (like find and replace, regular expression-based operations, etc.).To enter command-line mode from
normalmode, press the:key on your keyboard, you will notice that your cursor will move to the bottom of your screen where you can enter commands.Try the following commands:
:help Normal-modeto read the help page on normal mode.:help Insert-modeto read the help page on insert mode.:help Command-line-modeto read the help page on command-line mode.
To exit the help page and go back to your main page, enter command-line mode and type
:q(for quit).
Loading our config file
To make things look prettier and nicer for you, we have provided you with a sample vim configuration file that sets up some parameters and shortcuts. To get that file and place in the right spot, from your shell, do the following:
wget -O ~/.vimrc https://www.rose-hulman.edu/class/csse/csse332/current/assets/files/vimrc
This will download our vimrc file and place it in your home directory under a hidden file (thus the . at the start). Feel free to read through that file and checkout the comments around each command, it is pretty self-explanatory.
Some practice
Let’s do some practice and write a simple C file to test our skills. From your terminal windows, open a file called example.c using
$ vim example.c
Editing the file
You are now in normal mode, let’s enter insert mode using i. Then start typing the following code:
#include <stdio.h>
int x = 3;
int main(int argc, char **argv) {
printf("Hello world!\n");
printf("x = %d\n", x);
}
Okay, now we want to save our file. To do so, go into normal mode by hitting Escape. Then enter command-line mode using : and type w. This will save your buffer to the file example.c.
Our configuration file (
vimrc) maps a shortcut to exitinsertmode intonormalmode. If you type the charactersjkright after one another (ininsertmode), it will act as if you pressedEscape. Try it out!
Note: If for some reason, you need
jkto appear in your text, simply write thej, wait 2 seconds, then write thek, it will keep you in insert mode and thejksequence will show up.
Some text manipulation
You can notice that in our file, the line int x = 3; shows up at line 3, but what we really wanted it to be part of the main function. Let’s make that happen.
Go to line 3:
To go to a specific line in
vim, enternormalmode, then entercommand-linemode using:, then type your line number followed byEnter(so your command line looks like:3). Line 3 should now be highlighted.Cut the line:
To cut line 3 so we can move it to another spot, enter
normalmode, then hit the sequencedd, you will see that the line will disappear and the text will be shifted one line higher.Normally, we use
ddboth to cut a line and deleting it (simple cut it and don’t paste it!).To see what you have copied/cut, you can use
:regincommand-linemode.Paste the line:
Now let’s go paste that line where we want it to be. We want
int x = 3;to appear right after theint main(int argc, char **argv) {line. Therefore, innormalmode, move your cursor to line 4 (:4), then innormalmode, hit thepkey. The cut line (int x = 3;) will be pasted in the line after the current one!If you want to paste the cut line in the line above the current one, you can use
Pinstead ofp.Fixing the indentation:
If you notice now, the line is in the right spot, but the indentation is messed up, we don’t like that!
To fix the indentation, here are a few methods to do so:
- Place your cursor on the line you want to indent. Then, in
normalmode, hit the==keys. - Place your cursor at the opening (or closing) brace (or curly brace or parenthesis) surrounding the line you want to indent (in this case, at the brace at the end/start of the
mainfunction). Then, innormalmode, hit the=%keys. This will indent the whole block of code in between the braces/parenthesis. - Indent the whole file. In
normalmode, hit thegg=Gkey sequence. This will reindent the whole file but will replace your cursor at the top of the file.
Your lines should now look better, with proper indentation!
- Place your cursor on the line you want to indent. Then, in
Copy instead of cut:
To copy a line instead of cutting it, redo the same steps, except replace the
ddsequence in step (2) withyy(whereystands for yank or copy).Save and exit the file:
Now, let’s save and exit our file. We need to enter
command-linemode and issue two commands:wfor saving the file andqto quit the file. Fortunately, you can do both in one short by chaining them together, so you can use:wqto save the file and exit.You can also use
:xto achieve the same purpose.
A warning
Often times, you will feel tempted to close off the terminal window without properly quitting vim. This is not a good idea as it can risk you losing your progress. To prevent that, vim often save temporary buffers that can be used to recover a file in case of an incorrect quit (you might see something like .example.c.swp show up in your directory when editing example.c).
To prevent this from messing up your submissions, please always make sure to save and quit your files before your close the terminal window. It is as simple as typing :x or :wq in command-line mode.
Useful tips and tricks
For a full cheat sheet, please take a look at the well organized document here; it is very useful when starting with vim.
Widely used ones
Here are a couple of very useful commands that I often find myself using.
- When entering
insertmode fromnormalmode, you can use the following:i: enterinsertmode on the current cursor.a: enterinsertmode at the spot next to the current cursor.A: enterinsertmode at the end of the current line.o: enterinsertmode by adding a new line below the current line.O: enterinsertmode by adding a new line above the current line.r: replace the character under the current cursor with the one pressed next.R: enter replace mode. All the keys you press next will replace the characters starting from the current cursor.
- In
normalmode:l: move the cursor to the next character (equivalent torightarrow key).h: move the cursor to the previous character (equivalent toleftarrow key).j: move the cursor to the character below the current character (equivalent to thedownarrow key).k: move the cursor to the character above the current character (equivalent to theuparrow key).w: move to the start of the next word.e: move to the end of the current word, or the end of the next if currently at the end of a word.eain normal mode will move to the end of the current word and then start editing right after it!u: undo the last action.<C-r>: redo the last action. (<C-r>stands for holding the control key and then pressingr).zz: visually move the current line to the middle of the buffer; useful if you are editing a file and reached the end of it and need to see more of it.dw: cut the word under the cursor, up to and including thespacecharacter following the word.de: cut the word under the cursor, up to but not including thespacecharacter following the word.gg: Go to the start of the file.<Shift-g>orG: Go to the end of the file.$: Go to the end of the line.^: Go to the first character in the line, skipping spaces and tabs.0: Go to the first character in the line, not skipping spaces and tabs.~: Switch case for the character under the current cursor.