Text editors: Vim
1 Starting Vim
Vim is a text editor. That is all it does. You cannot format a document, insert shapes, make a table with lines, use multiple fonts, highlight sentences, or print out with headings or margins. Vim, however, is very good at editing text. Once you’ve mastered Vim, it is very powerful. You can find and replace chunks of text using commands, copy and paste columns of data, and jump to a specified line number.
We sometimes call programs like Microsoft Word or LibreOffice Writer “text editors”, but we need to be a bit more careful when it comes to programming.
By default, Microsoft Word uses .docx
files to store not only text, but also formatting information about fonts, headings, and so on. This extra information isn’t stored as characters, and doesn’t mean anything to tools like head
: they expect input files to contain nothing but the letters, digits and punctuation on a standard computer keyboard. When editing programs, therefore, you must either use a plain text editor, or be careful to save files as plain text.
Vim is also everywhere. If you are working on a computer with a shell (such as terminal or gitbash), then you have Vim; it’s already there. And you can open it directly from the shell. The problem is, however, that Vim is notoriously difficult to learn.
Let’s create a new file with Vim. First, open your shell, then type:
vim new-file.txt
Yay, a new file! Now, close that file. Type:
:q
Ta-da! Congratulations, you’ve exited vim.
2 Text editing
So, you’ve created and exited a file with Vim. Your next challenge is to put some text inside the file. Open your file again:
vim new-file.txt
Now, try and type some text. What’s happening? You are currently in Normal (or ‘control’) mode. To add text, you need to be in Insert or ‘edit’ mode.
To enter Insert mode, type i. You can now type! Add some text:
Yay I can now add words to my file! This wasn't difficult at all!
How do you save your work? Ctrl + s will not help you. First, you need to go back to Normal mode by pressing Esc, then type:
:w
(If you spend too much time working in Vim, your text documents start to accumulate random :w
in them.) Now, you can safely quit vim again:
:q
Practice this. Re-open your vim document, enter insert mode, and add some more random text.
There is also a command for saving and exiting all in one instruction. Press Esc, then type:
:x
Phew! You had little control over where your new text went in the last exercise. How do you move around in vim? In Normal mode, you can move one character at a time using your arrow keys, or the keys K (up), L (right), H (left), and J (down).
Once you are in place, type i to enter Insert mode on the left-hand-side of the current character. You can also type a to enter Insert mode on the right-hand-side (think (a)fter the character). Try this out and add some more text.
Vim has several modes. For our purposes, the Normal mode and Insert mode are the most important.
- Normal mode is the default mode for navigating and manipulating text.
- To enter, press Esc from any other mode.
- Use this mode for moving around the file, deleting text, copying, pasting, and other commands.
- Insert mode is used for inserting and editing text.
- To enter this mode, press i, I, a, A, o, or O from Normal mode.
- Use this mode to type text directly into the document.
3 Useful shortcuts
/pattern
searches forward from the current cursor position for the text matchingpattern
. Type /, type the search term (pattern), and press Enter.- To search for the word “example”: first, ensure you are in Normal mode by pressing Esc. Then type
/example
and press Enter. - To navigate search results, n moves to the next occurrence of the search pattern.
- N moves to the previous occurrence of the search pattern.
- To search for the word “example”: first, ensure you are in Normal mode by pressing Esc. Then type
?pattern
searches backward from the current cursor position for the text matchingpattern
. Press ?, type the search term (pattern), and press Enter.- To search backward for the word “example”, you would type
?example
in Normal mode and press Enter.
- To search backward for the word “example”, you would type