VIM is the best programming tool, full stop. This short guide will take the average JIT1 student from “why would I use this silly terminal thing” to “ohmygod where has this amazing tool been all my life?" in 5 minutes, plus an hour or so of experimentation and adapting to the keybindings.
1. What is VIM?
VIM is a text editor with infinite reprogrammability, time-traveling and full-line undo, and a seemingly endless list of very useful functionality. There is no end to the things you can learn about VIM. Notably, it has been released and developed as Charityware2. Open a file by typing:
If you typed in that simple command correctly, VIM will open your file.
2. How to use VIM
Structurally, VIM is designed to allow a programmer to move, cut, find, paste, delete, and program as fast as is humanly possible, moving all menus and buttons into the back of the user’s head, so all focus can be placed on the program. VIM’s learning curve is steep but short.
To use the VIM text editor, you need to remember a few simple rules:
- ESC brings you to Command Mode where you can use commands.
- Simple commands can be combined for more powerful editing.
- The bottom left corner shows the Mode, and is blank in command mode.
- Using : allows you to use EX commands like :wq to save and quit, :q! to quit without saving anything, or :![command] to execute a shell command.
Simple Command Mode operations:
- hjkl moves the cursor, e moves to the end of a word, w moves one word forward, b moves back one word, $ moves to the end of a line.
- a appends text, i inserts text.
- x deletes a character.
- d is the delete command, needs to be used with a movement.
- / will search through the text. (n moves to next instance of search.)
- u is undo and Ctrl r is redo, :undolist shows an undo tree.
- V or v visually selects, y is yank, p is paste.
C-C-C Combo Examples:
- d w deletes a word.
- d 100 w deletes 100 words. (Yes, you can specify quanitities with most commands.)
- 10 j moves down 10 lines.
Useful EX Commands:
- :set number and :set nonumber turn line numbering on and off.
- :w is to write, :q is to quit, :wq is to save and exit.
3. Modifying VIM
The true power of VIM lies in its flexibility. My personal .vimrc is very small and tuned for editing C. Take a quick read, but implementing a robust .vimrc will not be covered in this post, as it is intended to introduce the user to simple editing and navigation within VIM.
syntax on colorscheme ron set tabstop=2 set shiftwidth=2 set softtabstop=0 set expandtab set smarttab set autoindent nnoremap <F4> :! clear && gcc % && clear && ./a.out<cr>
4. Additional Resources
VIM provides an interactive help menu, :help. Typing ‘vimtutor’ in your shell will bring up an interactive tutorial to teach you the basics outlined above in a very nice hand-holding way. The verbose MAN pages are, as always, available by typing man vim in your shell. Consider reading these koans. Below is a picture of vimtutor.
Thanks for reading.