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). If you are wondering as to why you should learn Vi, here are some reasons.

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 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, so no mode switching required. Shortcuts can be used anytime.
Commands do not work in Insert mode. Always in Insert mode, and shortcuts can be used anytime.
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.
CTRL-s may disable the terminal window. Use CTRL-q to enable. Commonly saves the document.

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 Description
:help {command} show {command} usage
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
:files
:ls
Window menu bar item Show all buffers/documents
:b SPACE TAB List buffers, TAB to cycle, ENTER to select
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 Append/Insert after the cursor
I Home Insert at the beginning of the line
A End Append/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
/{string} CTRL f Search {string}
* CTRL F3 (in some editors) 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 (character) and start marking by using cursor movement keys h, j, k, l
V Enter visual mode (line) and start marking full lines by using up/down cursor movement keys j, k
CTRL v or CTRL q Enter visual mode (block) and start marking by using cursor movement keys h, j, k, l
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
: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. To toggle them off, simply append ! to the same command. For example,  :set wrap  and  :set wrap! .

: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 wrap Enable line wrapping
:set tabstop=4 Tab shifts by 4 characters
:set shiftwidth=4 Indentation shift with >> and << commands
: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 switch to insert mode
O Open a new line above and switch to insert mode
r Overwrite one character under the cursor
D Delete characters under the cursor until end of line
J Join/Combine two lines
yy Copy the full line where the cursor is
yw Copy a word
dd Delete the full line where the cursor is
dw Delete a word, remain in command mode
cw Change Word, delete the word and switch to insert mode
zt Scroll current cursor position to top of page
zz Scroll current cursor position to center of page
zb Scroll current cursor position to bottom of page
Useful in source code
% Jump to matching brace
[{ Jump to previous brace
>> Indent right
<< Indent left
> Indent right in visual mode
< Indent left in visual mode
m{name} Named bookmark, where {name} is a lower case character
`{name} Go to {name} bookmark
`. Go to last edited line
Window manipulation
CTRL w s Horizontal split window
CTRL w v Vertical split window
CTRL w w Switch to other window
CTRL w c Close split window
:tabnew New empty tab window
:tabedit Edit file in new tab window
:tabnext
gt
Cycle and switch to next tab window
:tabprevious
gT
Cycle and switch to previous tab window
:tabclose Close tab window

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. See the sample configuration file.

  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 <C-C>
    :map "+d <C-X>
    :map "+p <C-V>

  4. User defined commands

    An example is to format a single line JSON by invoking the installed Python command.
    :command FormatJson .!"C:\Python37\python.exe" -m json.tool

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.

3 thoughts on “vi (Vim) for Windows users

  1. Pingback: Why learn vi or Vim? | Cognitive Waves

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s