Vim Editor

Vim editor is a screen-based text editor that should already installed in your Jammy environment.

If your container does not have vim, then you should run the command sudo apt install vim.

Inside your environment, you can run the editor by executing vim in the environment’s shell. If you include the path/name of a file after vim, it will either open the existing file, or create a new blank file (if no file exists at that path).

Vim is one of the oldest and most influential editors to have existed. Even if people don’t use vim, a lot of people use vim shortcuts and keybindings in other editors (it is a very commonly supported feature!) Travis McGaha actually uses it as his editor of preference. One other reason for that is vim works for every language on almost any platform. It is also much more lightweight (less resource-intensive) than other editors like VS Code.

Lets get started with vim. You will need the same terminal that you started before (with either docker desktop, WSL, orbstack, etc.). If you closed that terminal, re-open it.

From here we can use the command ls which “Lists” the contents of the current directory. If you cloned your repo correctly you should see a directory with the same name as your repo. After running the ls command your terminal should look something like:

moya@4d98e91a6924:~/shared$ ls
26fa-cis5480-xyz
moya@4d98e91a6924:~/shared$ 

where xyz is instead your github username.

We can see that we have the git hub repository we cloned earlier from earlier. Lets now run the cd command to Change Directory into the repo and then run the ls command after that (like show below). If we do that will move us to be inside of the github directory and show us everything in it.

Your treminal should now be something like:

moya@4d98e91a6924:~/shared$ ls
26fa-cis5480-xyz
moya@4d98e91a6924:~/shared$ cd 26fa-cis5480-xyz
moya@4d98e91a6924:~/shared/26fa-cis5480-xyz$ ls
check_setup  cowsay

let’s cd again one more time into the check_setup directory and ls again. If so it should look like:

moya@4d98e91a6924:~/shared$ ls
26fa-cis5480-xyz
moya@4d98e91a6924:~/shared$ cd 26fa-cis5480-xyz
moya@4d98e91a6924:~/shared/26fa-cis5480-xyz$ ls
check_setup  cowsay
moya@4d98e91a6924:~/shared$/26fa-cis5480-xyz$ cd check_setup
moya@4d98e91a6924:~/shared$/26fa-cis5480-xyz/check_setup$ ls
Makefile hello.c

Lets try using the vim command now. Try running:

vim hello.c

When you do this for the first time it will install a bunch of things. Once it is done try typing :q then hitting enter. You should not see this subsequent times you use vim.

You should now be able to see the file hello.c and it should look something like this:

Terminal vim hello

An important concept to vim is that there are different “modes” to vim. With our configuration, you should be able to see what mode you are in by looking in the lower left corner. Notice how in the screenshot above it says NORMAL. You should start off in Normal Mode which is the mode where you are able to scroll through the file using arrow keys (or the h, j, k, l keys) and enter other modes.

From normal mode we can hit : to start tpying in a command and enter COMMAND mode. The most important command to learn first is how to exit vim. It is commonly joked that exiting vim is hard, but it really isn’t. The command for it is :q, just hit enter afterwards. Below is a picture of what it should look like just before you hit the enter key. Note that it says COMMAND in the lower left and just below that it shows the command we are typing (:q).

Exiting Vim

There are also other variations on exiting/saving:

  • :q! exits without saving
  • :wq saves and exits
  • :w just saves, but does not exit

If you hit the enter key to run the :q command, reopen hello.c with vim to continue our walkthrough.

The other useful mode to know about for now is Insert Mode this is how you actually modify text with vim. Press i to enter insert mode. From there it should look like this

Terminal vim insert

notice again how the lower left changes to be in INSERT mode. You can now navigate to the end of line 4 or beginning of line 5 and hit enter. This should insert a blank line where you can now add your own print statement. Try adding the line printf("Vim is the best!\n");. Note: if you are trying to copy and paste in the terminal you typcially need to just right click to paste (or if you have text selected it will instead copy). It should look like this:

Terminal vim print added

We should now save and exit. We can hit the escape key to go back into NORMAL mode and then :wq to save and exit.

Once you have done this, you can try running the command make which should compile hello.c into a program called hello. Try running the program by doing ./hello in the terminal and you should be able to see the message it prints!

If you get an error compiling your code, try running the following commands one after the other:

apt-get install -y lsb-release
apt-get install -y gnupg
apt-get install -y software-properties-common
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 19
rm llvm.sh

If that does not work, then please post on ed.

Another thing that is worth noting is that if you need help in vim, you can have it show the vim help manual by using the :help command from NORMAL mode.

This was just some basics of using vim, but its true power comes from all the other things you can do with it. To avoid overwhelming you, we recommend just starting with these commands and leanring more as you need them or want to learn more.

For a vim tutorial, run vimtutor in your container Shell. There are also many great tutorials for Vim, We encourage you to check them out!

Here is another tutorial that Travis found and thinks is pretty good. It is short and to the point: https://www.makeuseof.com/how-to-use-vim/