Vim for everyday use
Prologue: edit, save and exit!
Edit a file with Vim:
1. vim <filename>
2. Press i
3. Make your changes
Saving in Vim:
1. Press ESC
2. Press :
3. Type w
Exit Vim:
1. Press ESC
2. Press :
3. Type q!
Introduction
When using Vim I often find myself trying to remember how to do various things: how do you comment on multiple lines of code? what’s the shortcut for undo? what was the command for setting line numbers? With this post, I’m trying to gather all this information in one place so I can easily find it at a later date and hope that it will be useful to you too.
Vim is a clone of Vi and stands for Vi IMproved. It was written by Bram Moolenaar and released to the public in 1991. Vi itself was written as a visual editor for the ex line editor which in turn was inspired by ed. Ed was the line editor on the Unix operating system.
You might be wondering what is a line editor and how is it different from a text editor?
A line editor is a limited text editor; limited in the sense that you edit the text but you don’t see the result immediately because there’s no monitor to output the text. It was used especially on teletypes (which are printers with keyboards) that had no video display and therefore no ability to interactively move a cursor or see the result of your editing, unlike a screen-based text editor.
Modes
Vim has several modes, in which characters that you type have different meanings.
Normal
this is the default mode
can be accessed by pressing
ESC
orCTRL-[
characters typed in this mode have special meaning and are used for manipulating, searching and navigating within the text
Insert
can be accessed by:
pressing
i
enters insert mode before the cursorpressing
I
enters insert mode at the start of the current linepressing
a
enters insert mode after the cursorpressing
A
enters insert mode at the end of the current line
characters don’t have a special meaning in this mode; they appear as you type them
Visual
can be accessed by pressing
v
and will also mark a selection start pointused for making text selections
useful for copy, paste, delete, replace operations
Command
can be accessed by typing
:
a prompt appears at the bottom of the window where you can input your commands
useful for things which can’t be done easily in the normal mode
Replace
easily replace text by writing over it
from normal mode, position your cursor over the first character that you want to replace; press
R
and start typing your text; when done, pressESC
Navigation
Editing
Search
Useful commands
Find and replace:%s/foo/bar/g
:
enter command mode%
search across all lines (optional)/foo
search for “foo”/bar
replace with “bar”/g
global otherwise will execute only once per line
Show line number:set number
: enter command modeset
builtin commandnumber
option to be configured
There are a lot of options that can be configured. Please see https://www.shortcutfoo.com/blog/top-50-vim-configuration-options/ for a more complete list.
Comment / uncomment multiple lines
Comment:
Go to the first line you want to comment. Press CTRL-v
and use the arrow key to select until the last line you want to comment. Press SHIFT-i
and then write your comment character (#
for Python, //
for C, C++, etc.). Press ESC
to exit visual mode.
Uncomment:
Go to the first line you want to uncomment. Press CTRL-v
and use the arrow keys to select all the lines you want to uncomment. Press d
to delete characters and then press ESC
.
Customization
You can create a .vimrc
file in your home directory and add various configuration options which will be applied to Vim.
For an example of what a vimrc
file looks like, please see https://vim.fandom.com/wiki/Example_vimrc
Conclusion
Vim can be confusing if you’re used to text editors like nano, joe, notepad. I think it’s confusing because it has different modes which are specialized for different things. This is Vim’s greatest power and also its weakness because if you don’t take the time to get comfortable with this modes, you’ll be lost and frustrated.
I tried to summarize the most useful commands and shortcuts for making it easier to edit text in Vim. However, there’s a lot more power to Vim and several books have been written to show this like “Practical Vim” by Drew Neil, “Pro Vim” by Mark McDonnell, and others.
If you have any favorite shortcuts or snippets please let me know in the comments.
Thank you!