Skip to content

Latest commit

 

History

History
161 lines (102 loc) · 7.52 KB

File metadata and controls

161 lines (102 loc) · 7.52 KB
title author date
Introduction to Vim
Mary Piper and Alfredo Iacoangeli
March, 2020

Approximate time: 30 min

Learning Objectives

  • Learn basic operations using the Vim text editor

Writing files

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 folder and create a document using vim entitled draft.txt:

$ mkdir -p ~/ngs_course/unix_lesson/other

$ cd ~/ngs_course/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:

vim-insert-mode

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.

vim-save

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.

vim-postsave

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-quit

Vim Editing

Create the document "spider.txt" in vim. Enter the text as follows:

image

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.

image

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:

`gg`: move to top of file  
`G`: move to bottom of file  
`$`: move to end of line 
`0`: move to beginning of line  
`w`: move to next word
`b`: move to previous word

In addition to shortcuts for navigation, vim also offers editing shortcuts such as:

`dw`: delete word 
`dd`: delete line  
`u`: undo
`Ctrl + r`: redo

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.

  1. Open spider.txt, and delete the word "water" from line #2.
  2. Quit without saving.
  3. Open spider.txt again, and delete: "Down came the rain."
  4. Save the file.
  5. Undo your previous deletion.
  6. Redo your previous deletion.
  7. Delete the first and last words from each of the lines.
  8. Save the file and see whether your results match your neighbors.

Overview of vim commands

Vim modes:

`i`: insert mode - to write and edit text
`esc`: command mode - to issue commands / shortcuts

Saving and quiting:

`:w`: write to file (save)
`:wq`: write to file and quit
`:q!`: quit without saving

Shortcuts for navigation:

`gg`: move to top of file  
`G`: move to bottom of file  
`$`: move to end of line 
`0`: move to beginning of line  
`w`: move to next word
`b`: move to previous word

Shortcuts for editing:

`dw`: delete word 
`dd`: delete line  
`u`: undo
`Ctrl + r`: redo
`:set number`: number lines
`:set nonumber`: no line numbers

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.