Skip to content

VIM Code Editor

VIM Code Editor

Being able to edit files from a terminal is an essential skill. Generally, the tool of choice is either emacs or vim. There are a few tools, such as nano.  We don’t teach nano here, since our goal is for you to be a legitimate data scientists. In this example, we’ll focus on vim.

VIM Editor Commands

To open the editor type vi followed by the name of the script you would like to write:


There are two modes in vim. One is the command mode and another is the insert mode. In the command mode, you can move around the file, delete text, etc, whereas in the insert mode, you can insert text.

Text Entry Commands

When you enter the editor, press one of the following keys to begin text entry

  • a Append text following the current cursor position
  • A Append text to the end of the current line
  • i Insert text before the current cursor position
  • I Insert text at the beginning of the cursor line
  • o Open up a new line following the current line and add text there
  • O Open up a new line in front of the current line and add text there

To return to the command mode, press ESC.

The following commands are used only in the commands mode.

  • ^F (CTRl F) Forward screenful
  • ^B Backward screenful
  • ^f One page forward
  • ^b One page backward
  • ^U Up half screenful
  • ^D Down half screenful
  • $ Move the cursor to the end of the current line
  • 0 (zero) Move the cursor to the beginning of the current line
  • w Forward one word
  • b Backward one word

Exit Commands

  • :wq Write file to disk and quit the editor
  • :q! Quit (no warning)
  • :q Quit (a warning is printed if a modified file has not been saved)
  • ZZ Save workspace and quit the editor (same as :wq)

Text Deletion Commands

  • x Delete character
  • dw Delete word from cursor on
  • db Delete word backward
  • dd Delete line
  • d$ Delete to end of line
  • d^ (d caret, not CTRL d) Delete to beginning of line

Yank (has most of the options of delete)– VI’s copy command

  • yy yank current line
  • y$ yank to end of the current line from cursor
  • yw yank from the cursor to end of the current word
  • 5y yank, for example, 5 lines

Paste (used after delete or yank to recover lines.)

  • p paste below cursor
  • P paste above cursor
  • u Undo last change
  • U Restore line
  • J Join next line down to the end of the current line

File Manipulation Commands

  • :w Write workspace to original file
  • :w file Write workspace to named file
  • . Repeat last command
  • r Replace one character at the cursor position
  • R Begin overstrike or replace mode � use ESC key to exit
  • :g/pat1/s//pat2/g replace every occurrence of pattern1 (pat1) with path


Enter the code editor by typing in `vi` followed by the filename of your script


You will find that your environment is now a big empty box. To go to the insert mode (where you can begin inserting code), click i .

In your editor, paste the following.

echo "Hello World"

  • A bash script begins with a shebang line `#!/bin/bash` that initializes your script.
  • The contents of the script are found in the following lines. In this example, the script will echo the words “Hello World” to the user.

To save your new file and leave the editor, press ESC, then :wq.

Concept: File permissions & chmod

Now you must execute the script:


If you were to try to execute the script, you would get a stern ‘Permission denied’.  At the moment, the file isn’t executable.

The permissions of a file can be described by a 3 digit number. The first digit represents the permission you give yourself(the user), the second represents the permission you give your group, and last represents the permission you give the world. A +4 is fully readable, a +2 is fully writable and a +1 is fully executable. It’s a little confusing at first glance. With a quick example, things should make a lot more sense:

Level 1: User (A.K.A yourself): If you want to be able to read a file, give yourself four points (+4). If you want to be able to write to a file or change a file, give yourself two points (+2). If you want to be run a file – such as a script as a program, give yourself a point. You want to do all three, give yourself 4+2+1, a grand total of 7 points

Level 2: Groups: Perhaps you belong to bioinformaticians. If your friend is also a bioinformatician. Do you want your friend to execute a file? Well, he or she technically must be able to read it. So she needs 4 + 1 points = 5 points

Level 3: World: What about others on the computer? Meh, I think I’ll make it readable and executable to everyone, give them readability 4 and executability, 1. So 4+1 = 5 points.

My permissions can now be described by the three digit number 755.

If you’d like to look at a few more examples, here is a webpage that goes into a little more detail. File Permissions

Now let’s change the permissions of our script to actually give us the ability to do all the things described above. The chmod command is used to change the permissions of a file:

chmod 755

If you were to execute the script, it would work now.


To see our new permissions, you may use the following command:

ls -l

This asks to list the files in our current directory, showing all permissions, dates and so forth. My example is below:

To the left, we see permissions described by a series of letters. The ‘r’ stands for readability, the ‘w’ stands for writability and the ‘x’ stands for the executability. The letters are in groups of three in the series of User, Group and World permissions.

Paths and Settings

Directories are something we’ve touched on, but it’s important to know that every file is within a directory. In Unix, these are separated by /. If we cd to the top level it would be cd /. The character ~ has a special meaning and it means the home directory. Typing cd ~ changes directory to our user’s home. You can do that followed by pwd to figure out your home directory.

To run an executable file, you need to either be in the same directory as our script or you must provide the full path of the file, which is the name of the file and the directory it’s in.  We can run the script we created in the example above by specifying its path as such:


In this example, we are running , which is found in the ~ home directory.

It can be cumbersome and not always convenient to specify the path of a file each time we run it.  I mentioned ls is also a program, but we don’t have to type the full path for ls.  Why?

Here, we want to introduce our first important variable: the PATH variable.  The PATH variable is basically a list of directories your computer looks through to find a requested executable. When we login, a series of bash commands (within the .BASHRC file) are automatically run, and typically one of these sets your PATH variable. Where is this file?  Well all files that begin with a . are hidden by default and we need to type ls -a to see them:

.             binder   demo        .jupyter  .profile          .travis.yml
..            .cache   .empty      LICENSE         .yarn
appveyor.yml  .conda   .git        .local    talks.yml
.bash_logout  .config  .gitignore  mydir
.bashrc       data     .ipython    .npm      timestypedls.txt

The PATH variable is very important, and so much so that we have a default. It’s the directory for a file. In fact it’s so important, we generally need to know the entire path of a program in order to run it. For example, let’s say the program ‘ls’ is stored in /usr/bin/ls. To run it, you’d have to type /usr/bin/ls. However if add /usr/local/bin to our path, then we only have to type ls. We can have a lot of things in our path, and then separate them by colons. If you like, type echo $PATH to see what’s in your current path. All of your favorite startup settings are in your .bashrc file. In some cases, a default setup is only calling up .bash_profile. Here, people usually have only one command in it – that is to source .bashrc.

There are some conventions. The bin directory is typically where you put executable programs. So the first good thing to do is to create a bin in your home directory. These are often called local executable files. To actually make the bin meaningful, you’d have to add it to your path, such as PATH=$PATH:~/bin, would add that local bin to your existing path. There are going to be some programs installed as superuser or root (who can read and write anywhere). These are typically in /usr/bin. The directory /etc is where settings are – and don’t go here unless you know what you are doing. Don’t worry – you shouldn’t be able to do anything without becoming superuser.