vi (Vim) for Windows users

Learn by comparison with Windows editor

vi or Vim (Vi IMproved) is a powerful text editor with origins in UNIX and Linux. It’s available for Windows too (www.vim.org). For the uninitiated user of a GUI text editor (Word, Notepad, Notepad++, etc.), it can be a challenge. There are some conceptual differences that can make it frustrating. A way to bridge this gap is to compare and map the two behaviors.

Concept
Basic Operations
Useful Settings
More Operations
Tips and Tricks
Conclusion

Concept

vi operates in two modes, Command mode and Insert mode. Standard GUI text editors are always in one mode, which is the Insert mode.
Command mode (also called Normal mode) – All keys pressed execute a command rather than adding text to the document. Note that there are different forms – single key, multiple keys in sequence and colon commands (where the command is prefixed with “:” character).
Insert mode – Standard GUI text editor behavior. All keys pressed adds text to the document.
Typically, the bottom left corner indicates the mode. When in Insert mode, the text “--INSERT--” is displayed. It’s empty in Command mode.

Listed below are some conceptual differences. Note the comments in red below. These usually frustrate a beginner.

vi GUI Text Editor
Command Shortcut key
Commands are case sensitive Shortcut keys are not case sensitive
Buffer Document
Starts in Command mode Starts in Insert mode
Press i, a, I, A to enter into Insert mode.
You have to press one of these keys before text gets added to the document.
Always in Insert mode, so you just keep typing to add text.
Press ESC key to return to Command mode.
When in Insert mode, you have to press the ESC key before executing a command.
Always in Insert mode. No mode switching is required. Shortcuts are active and can be used.
Commands do not work in Insert mode. Shortcuts are active in Insert mode.
For copy/cut/paste, you can use the local clipboard or the system clipboard. Copy/cut/paste always uses the system clipboard
Mouse and scroll wheel may not work to move the cursor. Have to use one of the many keyboard commands for cursor movement. Mouse and scroll wheel works as expected.

Basic Operations

Following are the minimal set of commands which will help you operate vi. They may not be the most efficient way in vi but will keep you out of trouble. The equivalent Windows shortcut keys in typical text editors are mentioned for comparison.

vi Command Shortcut key Detail
ESC Return to command mode
File operations
:q ALT F4 quit/close the application
:q! ALT F4, ‘No’ to save quit/close without saving
:e CTRL o edit/open a file
:w CTRL s write/save to file
:bn CTRL TAB cycle forward through open buffers/documents
:bp CTRL SHIFT TAB cycle reverse through open buffers/documents
:bd CTRL F4 Close current buffer/document
:buffers Window menu bar item Show all buffers/documents
Cursor Movements
h Left Arrow
j Down Arrow
k Up Arrow
l Right Arrow
w CTRL Right Arrow Move forward by a word
b CTRL Left Arrow Move backward by a word
CTRL f Page Down
CTRL b Page Up
gg CTRL Home Beginning of document
G CTRL End End of document
:n CTRL g (in some editors) Go to line number n
Enter into Insert mode
i Insert before the cursor
a Insert after the cursor
I Home Insert at the beginning of the line
A End Insert at the end of line
R Insert Insert and overwrite text as you type
Deletion
x Delete
X Backspace
Undo/Redo
u CTRL z Undo
CTRL r CTRL y Redo
Find
/ CTRL f
* Word on the cursor is set as the find string
n F3 (in some editors) Find Next
N SHIFT F3 (in some editors) Find Previous
:noh clear last search highlights
Find and Replace
:%s/Foo/Bar/gc CTRL h (in some editors) Foo is the search string and Bar is the replace string. The /gc asks for confirmation before replace.
Copy Cut Paste
v SHIFT Arrow keys Enter visual mode and start marking by using cursor movement keys h, j, k, l
V Enter visual mode and start marking full lines by using up/down cursor movement keys j, k
ggVG CTRL a mark/select full buffer/document
y CTRL c yank/copy
d CTRL x delete/cut
p CTRL v Lowercase p, paste after the current cursor position
P Uppercase P, paste before the current cursor position
"+y CTRL c copy to system clipboard
"+d CTRL x cut to system clipboard
"+p CTRL v Lowercase p, paste after the current cursor position from system clipboard
"+P Uppercase P, paste before the current cursor position from system clipboard
File Status
CTRL g or :f Prints the current file information at the bottom status line

Useful Settings

Following are some settings in vi which will help getting comfortable with it. These are colon commands which can be applied in command mode.

:set autochdir Opening a file, the base directory is set to the location of the current buffer
:set hlsearch Highlight the text found during search
:set ignorecase Ignore Case during find
:set incsearch Incremental search, find as you type
:set list Display hidden character like tab and end of line
:set number Show line numbers
:set ruler Display the current cursor position (row and column) at the bottom
:set tabstop=4 Tab shifts by 4 characters
:set expandtab Insert spaces instead of tab character

More Operations

The following are more commands in vi which might give you some insight into its power and flexibility. There are no real equivalents in standard text editors.

. Repeat previous command
$ Move to end of current line
0 Move to start of current line
o Open a new line below and insert mode
O Open a new line above and insert mode
r Overwrite one character under the cursor
yy Copy the line where the cursor is
yw Copy a word
dd Delete the line where the cursor is
dw Delete a word
cw Change a word
D Delete characters under the cursor until end of line
J Join/Combine two lines

Tips and Tricks

  1. Startup configuration file

    The vimrc file contains optional runtime configuration settings to initialize Vim when it starts. On Unix based systems, the file is named .vimrc, while on Windows systems it is named _vimrc. It's convenient to have some of the settings defined above on startup.

  2. Tab settings

    The default tab settings is 8 columns. Some conventions require it be 4 columns. This link, Secrets of tabs in vim, will help in adjusting the defaults.

  3. Mapping keys

    It means user defined shortcut keys. One good example is mapping to the Windows copy/cut/paste shortcut keys (since the Vim command is not convenient at all).

    :map  "+y
    :map  "+d
    :map  "+p

Conclusion

Love it or hate it, knowing the basics of vi is a must if you were to ever work on a Linux system. As with many things, after the first hurdle, it becomes better over time. Give it a try and if you have any questions or clarifications, just ask.

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s