Introduction to the `vim` text editor
Approximate time: 30 min
Learning Objectives
- Learn basic operations using the Vim text editor
Writing files
We've been able to do a lot of work with files that already exist, but what if we want to write our own files. Obviously, we're not going to type in a FASTA file, but you'll see as we go, there are a lot of reasons we'll want to write/create a file or edit an existing file.
To create or edit files we will need to use a text editor. When we say, "text editor," we really do mean "text": these editors can only work with plain character data, not tables, images, or any other media. The types of text editors available can generally be grouped into graphical user interface (GUI) text editors and command-line editors.
GUI text editors
A GUI is an interface that has buttons and menus that you can click on to issue commands to the computer and you can move about the interface just by pointing and clicking. You might be familar with GUI text editors, such as TextWrangler, Sublime, and Notepad++, which allow you to write and edit plain text documents. These editors often have features to easily search text, extract text, and highlight syntax from multiple programming languages. They are great tools, but since they are 'point-and-click', we cannot efficiently use them from the command line remotely on a compute cluster.
Command-line editors
When working remotely, we need a text editor that functions from the command line interface. Within these editors, since you cannot 'point-and-click', you must navigate the interface using the arrow keys and shortcuts.
While there are simpler editors available for use (i.e. nano), most computational scientists tend to favor editors that have greater functionality. Some popular editors include Emacs, Vim, or a graphical editor such as Gedit. These are editors which are generally available for use on high-performance compute clusters.
Introduction to Vim
To write and edit files, we're going to use a text editor called 'Vim'. Vim is a very powerful text editor, and it offers extensive text editing options. However, in this introduction we are going to focus on exploring some of the more basic functions. There is a lot of functionality that we are not going to cover during this session, but encourage you to go further as you become more comfortable using it. To help you remember some of the keyboard shortcuts that are introduced below and to allow you to explore additional functionality on your own, we have compiled a cheatsheet.
Vim Interface
You can create a document by calling a text editor and providing the name of the document you wish to create. Change directories to the unix_lesson/other
folder and create a document using vim
entitled draft.txt
:
$ cd ~/unix_lesson/other
$ vim draft.txt
Notice the "draft.txt" [New File]
typed at the bottom left-hand section of the screen. This tells you that you just created a new file in vim.
Vim Modes
Vim has two basic modes that will allow you to create documents and edit your text:
-
command mode (default mode): will allow you to save and quit the program (and execute other more advanced commands).
-
insert (or edit) mode: will allow you to write and edit text
Upon creation of a file, vim is automatically in command mode. Let's change to insert mode by typing i. Notice the --INSERT--
at the bottom left hand of the screen. Now type in a few lines of text:
After you have finished typing, press esc to enter command mode. Notice the --INSERT--
disappeared from the bottom of the screen.
Vim Saving and Quitting
To write to file (save), type :w. You can see the commands you type in the bottom left-hand corner of the screen.
After you have saved the file, the total number of lines and characters in the file will print out at the bottom left-hand section of the screen.
Alternatively, we can write to file (save) and quit. Let's do that by typing :wq. Now, you should have exited vim and returned back to your terminal window.
To edit your draft.txt
document, open up the file again by calling vim and entering the file name: vim draft.txt
. Change to insert mode and type a few more lines (you can move around the lines using the arrows on the keyboard). This time we decide to quit without saving by typing :q!
Vim Editing
Create the document spider.txt
in vim. Enter the text as follows:
To make it easier to refer to distinct lines, we can add line numbers by typing :set number. Save the document. Later, if you choose to remove the line numbers you can type :set nonumber.
While we cannot point and click to navigate the document, we can use the arrow keys to move around. Navigating with arrow keys can be very slow, so Vim has shortcuts (which are completely unituitive, but very useful as you get used to them over time). Check to see what mode you are currently in. While in command mode, try moving around the screen and familarizing yourself with some of these shortcuts:
key | action |
---|---|
to move to top of file | |
to move to bottom of file | |
to move to end of line | |
to move to beginning of line | |
to move to next word | |
to move to previous word |
In addition to shortcuts for navigation, vim also offers editing shortcuts such as:
key | action |
---|---|
to delete word | |
to delete line | |
to undo | |
to redo | |
to search for a pattern (n/N to move to next/previous match) | |
to search for a pattern and replace for all occurences |
Practice some of the editing shortcuts, then quit the document without saving any changes.
Exercise
We have covered some basic commands in vim, but practice is key for getting comfortable with the program. Let's practice what we just learned in a brief challenge.
- Open
spider.txt
, and delete the word "water" from line #2. - Quit without saving.
- Open
spider.txt
again, and replace every occurrence of "spider" with "unicorn". - Delete: "Down came the rain."
- Save the file.
- Undo your previous deletion.
- Redo your previous deletion.
- Delete the first and last words from each of the lines.
- Save the file and see whether your results match your neighbors.
Overview of vim commands
Vim modes:
key | action |
---|---|
insert mode - to write and edit text | |
command mode - to issue commands / shortcuts |
Saving and quiting:
key | action |
---|---|
to write to file (save) | |
to write to file and quit | |
to quit without saving |
Shortcuts for navigation:
key | action |
---|---|
to move to top of file | |
to move to bottom of file | |
to move to end of line | |
to move to beginning of line | |
to move to next word | |
to move to previous word |
Shortcuts for editing:
key | action |
---|---|
to delete word | |
to delete line | |
to undo | |
to redo | |
to number lines | |
to remove line numbers | |
to search for a pattern (n/N to move to next/previous match) | |
to search for a pattern and replace for all occurences |
This lesson has been developed by members of the teaching team at the Harvard Chan Bioinformatics Core (HBC). These are open access materials distributed under the terms of the Creative Commons Attribution license (CC BY 4.0), which permits unrestricted use, distribution, and reproduction in any medium, provided the original author and source are credited.