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 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.
When we say, "vim
is a text editor," we really do mean "text": it can only work with plain character data, not tables, images, or any other media. While there are simpler editors available for use (i.e. nano which is more commonly used with Software Carpentry), 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 computing clusters.
We previously covered how to create a file in Unix using the touch
command, but you can also create a document by calling a text editor and providing the name of the document you wish to create. To create a document in vim entitled draft.txt
, let's type in the terminal:
vi 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 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.
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: vi 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!
Create the document "spider.txt" in vi. 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:
`gg`: move to top of file
`G`: move to bottom of file
`0`: move to beginning of line
`$`: move to end 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 + u`: redo
Practice some of the editing shortcuts, then quit the document without saving any changes.
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.
spider.txt
again, and delete: "Out came the sun and dried up all the rain, and the itsy bitsy spider went up the spout again."