Vim

Written by Alex Guyer | guyera@oregonstate.edu

While it's fully possible to execute your programs locally (directly on your computer) given that you've installed the proper tools, your grade will always be based on what your programs do when executed on the engineering servers. In certain cases, the same program can behave differently depending on its execution environment (e.g., the computer that it's being executed on). Hence, it's critical that you always execute your programs on the engineering servers so that there are no surprises during grading.

This means that you need some way of getting your code onto the engineering servers where you can execute it. You have a few options:

  1. (Not recommended) You can write the code locally (i.e., directly on your computer) using your favorite text editor or IDE and manually transfer it to the engineering servers whenever you make changes to it. This method is somewhat inconvenient. Still, if you want to go down this route, I recommend looking into an SFTP client (e.g., FileZilla), which is a program that allows copying of files to and from an SFTP server. SFTP is incorporated into most SSH server implementations, and indeed, the engineering servers act as both SSH servers and SFTP servers. Hence, it's possible to use an SFTP client to copy files to and from the engineering servers.

  2. (Recommended for work outside of this class) You can create a virtual networked file system locally (i.e., directly on our computer) and link it up to your remote engineering file space. This allows you to work with files stored in your file space on the engineering servers as if they're in a folder stored directly on your computer. For example, you could edit your source code files using your favorite text editor or IDE, and the moment you save your changes, the corresponding file on the engineering servers would be updated automatically to reflect the changes. This method requires a bit of configuration (including connecting to OSU's VPN if you want this method to work from off-campus internet networks), but it's fairly convenient once everything is set up. You can find information about how to set this up on the College of Engineering IT department's website (see, e.g., this explanation for how to do it on Windows).

  3. (Recommended for classwork) You can write the code remotely (i.e., directly on the engineering servers) within an SSH session in a terminal by using a terminal-based text editor. This is the option that I'll demonstrate. It requires no configuration and will work on any machine that can connect to the engineering servers in an SSH session (even a Chromebook, in most cases). It's also strongly recommended that you go with this option since you may be tested on your understanding of terminals and shells in this class. Using a terminal-based text editor for all of your classwork is a great way to get familiar with these sorts of things. (Do not be afraid of a terminal! They're extremely powerful and every computer scientist's best friend. But using them is a skill, and it must be honed. If you have any doubts about the power of these tools, know that I wrote all of these lecture notes in a terminal-based text editor.)

  4. (Not recommended for this course) Some IDEs are capable of connecting to SSH servers and remotely installing additional server software to support remote development and debugging (e.g., VSCode's Remote - SSH extension). While you might be able to get these methods to work, 1) configuration can be a hastle, 2) they usually don't work as well as you'd like them to, and 3) some of these methods can bog down the servers with runaway processes, which can lock you out of your engineering user account until you manually kill those runaway processes via the sidebar in TEACH (not to mention, all of the runaway processes can slow down the servers and interfere with other students' work).

We're going with the terminal-based text editor option. The engineering servers have several terminal-based text editors already installed and available for use. We're going to use Vim. Vim stands for "Vi improved". Vi, in turn, is a much older terminal-based text editor that was created by Bill Joy at UC Berkeley in the 1970's as a part of his work on BSD UNIX.

Vim has its quirks, but it's also very powerful if you know how to use it. You will often find yourself editing files on a remote system and Vim almost always available, so it is very useful to have at least a basic familiarity with it. And for the advanced features that IDEs support out-of-the-box (e.g., frontend linters to detect and highlight syntax errors), there are Vim plugins for that (e.g., ALE).

All that being said, this is not a Vim course, so we'll only be using Vim in a relatively basic capacity. Open your terminal and connect to the engineering servers over SSH. Use cd to navigate to the directory where you want to write code while following along with the lectures (I recommend using mkdir to create a directory named lecture-notes inside your cs162 directory, and then navigate into it). Then, execute the following command:

vim hello.py

This command starts Vim, telling it to open the file called hello.py in your working directory (if the file doesn't exist, it will be created automatically). The moment you execute the command, your entire terminal will be taken over by the Vim user interface. It will look vaguely like this:

A screenshot of Vim when first opened

Vim is a text editor, which means it can (of course) be used to edit the file that you just created and opened (hello.py). However, at this stage, if you naively try to write some text, it won't work as you'd expect. This is because Vim has multiple modes of operation. When first opened, it will be in Normal Mode by default. In order to insert text, you must first switch to Insert Mode. You can do this by pressing the "i" key on the keyboard. After doing so, you should see -- Insert -- appear at the bottom-left of the terminal:

A screenshot of Vim after switching to Insert Mode

At this point, you may now start writing text into the file. I didn't tell you this, but we named the file hello.py because .py is the standard Python source code file extension, and we're going to write a brief "Hello, World!" Python program. Copy and paste the following code directly into Vim (you should be able to paste from your system clipboard into Vim via Ctrl+Shift+V in PowerShell, assuming you've enabled it in your PowerShell settings, or via Cmd+V in a Mac Terminal):

def main():
    print("Hello, World!")

if __name__ == '__main__':
    main()

We've written our code, and it's time to run it. First, we have to save the file and quit Vim. Recall that Vim is a terminal-based text editor, and recall that terminals are primarily text-based. This means that there are no menu buttons to click on to save or exit Vim (if you click the X button in the menu bar at the top of your terminal, that will exit out of your entire terminalnot just Vim). Indeed, actions such as saving and quitting require executing text-based commands. As it turns out, executing text-based commands is the entire purpose of Normal Mode (it's sometimes even referred to as Command Mode). To switch back to Normal Mode, press the escape key on your keyboard. The -- Insert -- text should disappear from the bottom-left of your terminal again.

While in Normal Mode, there are at least two kinds of commands that you can execute: a) simple hotkey commands, where you press a single key and it performs some corresponding action, and b) complete commands, which are prefixed with a colon (:). Currently, our goal is just to save and quit. There are no hotkey commands for this, so we have to execute a complete command. First, type a colon. It should appear at the bottom-left of your terminal:

A screenshot of Vim with an arrow pointing to the colon that appears when executing a complete command from Normal Mode.

After the colon, you can type any one of various Vim commands and press enter to execute it. The basic commands that you'll use most frequently are as follows:

There are countless other commands, some of which allow you to do much more advanced things (e.g., find and replace, enable line numbers, create macros, remap keys so that they do more complex things, etc). We'll discuss some of them shortly.

Let's save the file and quit Vim. Type wq after the colon and press enter. You should now find yourself back in your shell once again. If you execute ls, you should now see hello.py in your working directory:

(base) guyera@flip4:lecture-notes$ ls
hello.py

(This lecture isn't about Python, but if you're curious, you can execute the Python program that we just wrote by typing python hello.py into the terminal and pressing enter; refer to my "Hello, World!" Python lecture notes for more information).

If you wish to edit the file some more, simply execute the same command as earlier: vim hello.py. Since hello.py already exists, this will simply open the file in Vim for editing rather than creating it.

Some Vim tricks

That's it for the basics. Now onto some slightly more advanced Vim tricks (Vim is very powerful, but it would take several weeks if not an entire term to cover all of its capabilities, so I'll just tell you enough to bring your Vim skills up a notch and make your life a bit easier).

(If you've already configured a .vimrc file to your liking, you can skip these first few steps). First, use Vim to create and open a file named .vimrc within your home directory. It must have this exact name, and it must be in your home directory. An easy way to do this is by executing the following shell command from your terminal:

vim ~/.vimrc

The ~ is an alias for your home directory, similar to how .. is an alias for a given directory's parent, so this creates the .vimrc file in your home directory as needed. Recall that files starting with a . are hidden files, so you'll only be able to see it in your home directory later on via ls -a.

Now, copy and paste the following contents into the file within Vim (recall that you can paste while in Insert Mode via Ctrl+Shift+V, assuming your terminal configuration has been setup properly):

set nu
set mouse=a
filetype plugin indent on
set autoindent
colorscheme desert
set colorcolumn=81
syntax on
set expandtab
set tabstop=4
set softtabstop=4
set shiftwidth=4

Now save and quit Vim (press escape to enter Normal Mode, then type :wq and press enter).

Let me explain what we just did. A .vimrc file specifies a list of Vim commands that Vim should execute automatically every time it starts up. Each one of the above Vim commands enables a certain feature in Vim. By putting them in your .vimrc file, they'll be re-enabled every time you start Vim (these features do not persist, hence it's necessary to put them in your .vimrc file if you want them to always be enabled).

The above commands enable the following respective features:

These settings alone will surely make Vim easier to use, but you'll be even more proficient if you can master some of the (slightly) more advanced commands and hotkeys detailed below. (Keep in mind that many hotkeys and commands are case-sensitive. For example, pressing the U key while in Normal Mode with caps lock off will do something different from pressing the U key with caps lock on).

Again, there are countless more Vim commands and hotkeys that we don't have time to cover. If you want to improve your Vim skills beyond what's covered in this lecture, I recommend studying Vim mappings, macros, abbreviations, and, if you really want to get your hands dirty, plugins and plugin development.