QUESTION

How do I use the VIM text editor?


curated by 270 crowd contributors

We pulled together 16 of the most popular web sites to create this page, including www.linux.com, linux.startcom.org, lifehacker.com. We've organized their information into the most commonly discussed topics below, ranging from Text editing commands to Useful operations. A total of 61 crowdworkers were involved during the process, of which 35 of them gathered the information, 26 of them organized the information by different topics, and 0 of them synthesized the information into this article.


Overview

Text editing commands: There are many commands you need to know in order to use VIM. Read the detailed article for the breakdown of all the necessary commands.

Moving through the text: In Vim, there are many shortcuts that allow users to expediate the editing process. This article provides some of the more common and useful shortcuts.

Vim and vi editor: The Vim Editor is a supremely useful text editor found on all Unix based systems, but takes some time to learn.

Help and tutorial: While you can't learn everything about vim in just half an hour, the tutor is designed to describe enough of the commands that you will be able to easily use Vim as an all-purpose editor.In UNIX and MS Windows, if Vim has been properly installed, you can start this program from the shell or command line, entering the vimtutor command.

Features: The vimulator will teach you how to use the editor.

Using command mode: Various ways to use VIM text editor.

Software info: This article provides many commands available in the Vim editor. Below you can find the arguments and the decriptions of the arguments. This article also provides information of the Vim editor on various OS.

Useful operations: Vim can assist in editing text documents. Spell check, buffers, and windows or views can be used for editing. Vim allows the user to edit multiple buffers at once.


Text editing commands

Text editing commands

There are many commands you need to know in order to use VIM.

The commands you most need to start out: v highlight one character at a time. V highlight one line at a time. Ctrl-v highlight by columns. p paste text after the current line. P paste text on the current line. y yank text into the copy buffer.

Opening a New File Step 1 type vim filename (create a file named filename) Step 2 type i ( switch to insert mode) Step 3 enter text (enter your Ada program) Step 4 hit Esc key (switch back to command mode) Step 5 type :wq (write file and exit vim)

Text Entry Commands (Used to start text entry) a Append text following current cursor position A Append text to the end of current line i Insert text before the current cursor position I Insert text at the beginning of the cursor line o Open up a new line following the current line and add text there O Open up a new line in front of the current line and add text there The following commands are used only in the commands mode.

Vim distinguishes between screen-lines (those shown on the monitor) and real lines (those ended with a new-line). So here the most important commands 0 …first column of the line ^ …first non-blank character of the line w …jump to next word W …jump to next word, ignore punctuation e …jump to word-end E …jump to word-end, ignore punctuation b …jump to word-beginning B …jump to word-beginning, ignore punctuation ge …jump to previous word-ending gE …jump to previous word-ending, ignore punctuation g_ …jump to last non-blank character of the line $ …jump to the
i insert at current location a insert after current location (append) I insert AT START of current line A insert AFTER END of current line o insert line below current line (open) O insert line ABOVE current line s delete character under cursor and start inserting in its place (substitute text) S delete all text on line and start inserting in its place (substitute line) cw delete to the end of current word and start inserting in its place (any movement command can be substituted for w) cc same as S (change line) C delete from the cursor to the end of line and start inserting at the cursor position

Inserting text is pretty simple in Vim, just type i and start typing. But Vim offers quite sophisticated text-editing commands. d …delete the characters from the cursor position up the position given by the next command (for example d$ deletes all character from the current cursor position up to the last column of the line). c …change the character from the cursor position up to the position indicated by the next command. x …delete the character under the cursor. X …delete the character before the cursor (Backspace). y …copy the characters from the current cursor position up to the position indicated by the next command. p …paste previous deleted or yanked (copied) text after the current cursor position. P …paste previous deleted or yanked (copied) text before the current cursor position. r …replace the current character with the newly typed one. s …substitute the text from the current cursor position up to the position given by the next command with the newly typed one. . …repeat the last insertion or editing command (x,d,p…). Doubling d, c or y operates on the whole line, for example yy copies the whole line.

Text Entry Commands (Used to start text entry) a Append text following current cursor position A Append text to the end of current line i Insert text before the current cursor position I Insert text at the beginning of the cursor line o Open up a new line following the current line and add text there O Open up a new line in front of the current line and add text there The following commands are used only in the commands mode.

n dd will delete n lines starting from the current cursor position.n dw will delete n words at the right side of the cursor.x will delete the character on which the cursor is positioned:n moves to line n of the file.:w will save (write) the file:q will exit the editor.

n dd will delete n lines starting from the current cursor position.n dw will delete n words at the right side of the cursor.x will delete the character on which the cursor is positioned

Moving through the text

When you are using most word processors and text editors, the alphanumeric keys (a- z, 1-9) are used to input those characters unless they're modified by a control key. In Vim, the mode that the editor is in determines whether the alphanumeric keys will input those characters or move the cursor through the document.

Hands on the home row asdf hjkl. hjkl move in normal mode: h is left and moves left; l is right and moves right; j looks like a down arrow and moves down; k moves up. w moves one word forward; 3w moves three words forward; b moves one word backward; 3b moves three words backwards. gg moves to first line, G moves to last line, and 123G moves to line number 123. More moving: 8k moves eight lines up, 5j moves five lines down, 4l moves four characters right, 23h moves 23 characters left. : opens command line to enter "ex" commands :help ex-cmd-index ! after an ex command ignores warnings from many commands, or changes the behavior subtly for others. Objects and actions: at beginning of word: d2w (action)(times)(object) (delete)(2)(words forward) this deletes including the trailing space; use de to delete to end of word (leaving the trailing space). d2b (delete)(2)(words backward). hjkl are also objects! example: d3l (delete)(3)(left), for hl we count individual characters, not words, for jk we count individual lines, d3k delete 4 lines up (3 plus current). cw (change)(word), c3w (change)(3)(words). "Change" means delete the current text and enter insert mode it its place. cb (change)(word backward), c3b (change)(3)(words backwards).
Why c and d? Use cw to delete word and enter insert mode (so you can type a new word finishing with Esc). Use dw to delete word, staying in normal mode. Use u to undo and Ctrl-r to redo, multiple times. Autocompletion: "whatchamacallit" need to type it again? Type wh then press Ctrl-p to find the previous word that starts with "wh". Ctrl-p and Ctrl-n cycle through matches in previous and next order.

Visual mode: v3w (visual select)(3)(words); change selection with b and hjkl*. After selecting: y will "yank" (copy); p will "put" (paste) at a new location (after the cursor; use P for before the cursor). Use y in visual mode and p in normal mode. Convenience commands: dd delete current line; yy yank current line. Searching: /regularexpression to search forward, ?regularexpression search backward; press n for next hit, or N for previous. As before we can combine objects for more: y/) will yank everything to NEXT parens (or whatever you search for) while y?) will yank everything up to the PREVIOUS parens.

All of the `:' commands end with your hitting the Enter key. The a command, which puts text to the right of the cursor, puts you in insert-text mode, just like the i command. If you need to insert a control character while in append/insert mode, hit control-v first. For example, to insert control-g into the file being edited, type control-v then control-g.
One of vi's advantages is easy cursor movement. Since the keys h,j,k,l are adjacent and easily accessible with the fingers of your right hand, quickly reach them to move the cursor; h to move the cursor to the leftl to move it to the rightk to move upj to move down

The use of h,j,k,l becomes second nature, increasing speed, efficiency and enjoyment. Commands are prefixed by a number. For example, 3dd deletes (consecutive) three lines, starting with the current one. 4cw deletes the next four words. The p command is used for cut-and-paste and copy operations. For example, to move three lines from place A to place B: 1. Move the cursor to A. 2. Type 3dd. 3. Move the cursor to B. 4. Type p. The same steps are used to copy text, except that p must be used twice, after Step 2 (to put back the text just deleted). You can do operations like cut-and-paste, cursor movement easily with a mouse. This requires a GUI version of vi, discussed later.

Marking requires an identifier.
Mark the line as identified by the letter "a" by marking with keystroke "ma"
'a Move cursor to line mark "a" generated by marking with keystroke "ma" 'A Move cursor to line mark "A" (global between buffers) generated by marking with keystroke "mA" ]'
Move cursor to next lower case mark. [' Move cursor to previous lower case mark.

i - Enters insert mode at the current cursor position.
a - Enters insert mode after the current position.
I - Enters insert mode at the beginning of the current line.
A - Enters insert mode at the end of the current line.

To save a file, from Vim's insert mode, hit Escape and then :w.

When you run vim filename to edit a file, Vim starts in command mode. Thus the alphanumeric keys are bound to commands.

Move cursor around with following keys h, l, k, j, left, right, up and down respectively.

Vim Novice level summary: At this point of the vim tutorial you have learned: Moving with cursor around: h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN

Step 1 type vim filename (edit the existing file named filename)
Step 2 move around the file using h/j/k/l key or appropriate command. h moves the cursor one character to the left. l moves the cursor one character to the right. k moves the cursor up one line. j moves the cursor down one line nG or :n Cursor goes to the specified (n) line (ex. 10G goes to line 10)
Step 3 edit required text (replace or delete or insert)
Step 4 hit Esc key (exit from insert mode if you insert or replace text)

Step 5 type :wq

Basic Movements h: left l: right j: down k: up
Other Movements gg: top of the document G: bottom of document or to line number if a number is placed in front of G w: next word e: end of word 0: beginning of line $: end of line

Move through the text with the arrow keys. If not, move the cursor to the left, l to move it to the right, k to move up, j to move down. SHIFT-G will put the prompt at the end of the document.

[esc] - Enter normal mode by pressing the "escape" key.

Keystrokes Action h/j/k/l
Move cursor left/down/up/right spacebar
Move cursor right one space -/+
Move cursor down/up in first column ctrl-dn ctrl-d
Scroll down one half of a screen.Set scroll to "n" lines.
New default set for half screen. ctrl-u n ctrl-u
Scroll up one half of a screenSet scroll to "n" lines.
New default set for half screen. ctrl-fn ctrl-f
Scroll forward one screenScroll forward "n" screen ctrl-bn ctrl-b
Scroll back one screen
Scroll back "n" screen ctrl-yn ctrl-y
Scroll forward one lineScroll forward "n" lines ctrl-en ctrl-e
Scroll back one lineScroll back "n" lines M (shift-m)
Move cursor to middle of page H (shift-h)
Move cursor to top of page L (shift-l)
Move cursor to bottom of page W w 5w
Move cursor a word at a time (white space delimited)
Move cursor a word at a time (first non-alphanumeric)
Move cursor ahead 5 words Bb5b
Move cursor back a word at a time (white space delimited)
Move cursor back a word at a time (first non-alphanumeric)
Move cursor back 5 words Ee5e
Move cursor to end of word (white space delimited)
Move cursor to end of word (first non-alphanumeric)
Move cursor ahead to the end of the 5th word 0 (zero)
Move cursor to beginning of line :30
Move cursor to line thirty $
Move cursor to end of line )
Move cursor to beginning of next sentence (delimeted by ".", "?" or "!")
( Move cursor to beginning of current sentence }
Move cursor to beginning of next paragraph (delimeted by blank line or nroff macros: .IP, .LP, .PP, .QP, .P, .LI and .bp)
Also see "set paragraphs" to define a paragraph.
{ Move cursor to beginning of current paragraph ]]
Move cursor to beginning of next section (delimeted by nroff macros: .NH, .SH, .H, .HU). Also see "set sections" to define a section.

Vim and vi editor

Vim and Vi Editor

The Vim Editor is a supremely useful text editor found on all Unix based systems, but takes some time to learn..

Vim has long been praised as one of the best text editors around, mostly for its completely mouseless navigation; however, it can be very confusing for beginners.

The vi editor is a very powerful tool and has a very extensive built-in manual, activated through the :help command when the program is started. We will only discuss the very basics here to get you started.

VIM Editor Commands: Vim is an editor to create or edit a text file. There are two modes in vim. One is the command mode, and another is the insert mode. In the command mode, user can move around the file, delete text, search, replace, mark blocks, and perform other editing tasks. Some commands switch the editor to insert mode.
In the insert mode, user can insert text. Changing mode from one to another:
From command mode to insert mode: type a/A/i/I/o/O
From insert mode to command mode: type Esc (escape key)

What makes vi confusing to the beginner is that it can operate in both command mode and insert mode. The editor always starts in command mode.
Vim, an improvement on the classic vi text editor, is extremely powerful at editing code and plain text. While it may seem obtuse and difficult at first, it is one of the most efficient ways of editing text due to its language like command syntax.

Vim has a vi compatibility mode, but when not in this mode, Vim has many enhancements over vi.

The advantage of learning vi and learning it well is that one will find vi on all Unix based systems and it does not consume an inordinate amount of system resources. Vi works great over slow network ppp modem connections and on systems of limited resources. One can completely utilize vi without departing a single finger from the keyboard. (No hand to mouse and return to keyboard latency)

Help and tutorial

Help and Tutorial

While you can't learn everything about vim in just half an hour, the tutor is designed to describe enough of the commands that you will be able to easily use Vim as an all-purpose editor.In UNIX and MS Windows, if Vim has been properly installed, you can start this program from the shell or command line, entering the vimtutor command.

Instead of reading the text, which is quite boring, you can use the vimtutor to learn you first Vim commands. This is a thirty minute tutorial that teaches the most basic Vim functionality in eight easy exercises.
While you can't learn everything about vim in just half an hour, the tutor is designed to describe enough of the commands that you will be able to easily use Vim as an all-purpose editor.In UNIX and MS Windows, if Vim has been properly installed, you can start this program from the shell or command line, entering the vimtutor command.
This will make a copy of the tutor file, so that you can edit it without the risk of damaging the original. There are a few translated versions of the tutor. To find out if yours is available, use the two-letter language code. For French this would be vimtutor fr (if installed on the system).

As a brief introduction to vi, go through the following: First, type vi x at the Unix prompt. Assuming you did not already have a file named x, this command will create one. (If you have tried this example before, x will already exist, and vi will work on it. If you wish to start the example from scratch, simply remove x first.) The file will of course initially be empty.
To put something in it, type the letter i (it stands for "insert-text mode"), and type the following (including hitting the Enter key at the end of each of the three lines):

I took the approach to start using some tutorial and let the help-system guide (type :help <command> to get help for the command) me through the rest.

Features

features

The vimulator will teach you how to use the editor..

Some of Vim's enhancements include completion, comparison and merging of files (known as vimdiff).

Vim is designed for use both from a command line interface and as a standalone application in a graphical user interface.

Like vi, Vim's interface is based not on menus or icons but on commands given in a text user interface; its GUI mode, gVim, adds menus and toolbars for commonly used commands but the full functionality is still expressed through its command line mode.

To enter text, vim must transition into "insert" mode. Insert mode is analogous to the typing interface of most other text entry programs. What you type appears on the screen in the document. All normal keys will produce the corresponding character at the current cursor position.

Vim does support mouse clicks, arrow keys, and even menus

Instead of reading the text, which is quite boring, you can use the vimtutor to learn you first Vim commands.

By default, when vim is called without any arguments, it opens with a blank document.

Part of Vim's power is that it can be extensively customized. The basic interface can be controlled by the many options available, and the user can define personalized key mappings—often called macros—or abbreviations to automate sequences of keystrokes, or even call internal or user defined functions.

Using command mode

using command mode

Various ways to use VIM text editor..

Pressing the Esc key switches back to command mode. If you're not sure what mode you're in because you use a really old version of vi that doesn't display an "INSERT" message, type Esc and you'll be sure to return to command mode. It is possible that the system gives a little alert when you are already in command mode when hitting Esc, by beeping or giving a visual bell (a flash on the screen). This is normal behavior.

It moves the cursor one position to the right before switching to insert mode will insert a blank line under the current cursor position and move the cursor to that line.Pressing the Esc key switches back to command mode. If you're not sure what mode you're in because you use a really old version of vi that doesn't display an "INSERT" message, type Esc and you'll be sure to return to command mode. It is possible that the system gives a little alert when you are already in command mode when hitting Esc, by beeping or giving a visual bell (a flash on the screen). This is normal behavior.

In normal mode, you can enter commands, for example, to copy, delete or indent text. You return to normal mode from other modes by pressing the Esc key. You can enter insert mode from normal mode by pressing the i key. Now, text you type will be inserted. You can enter visual mode from normal mode by pressing the v key. That starts a visual selection.

Software info

Software info

This article provides many commands available in the Vim editor.

Vim arguments are listed in this table:
Arguments Description
+[num] Open editor with cursor on line "num". If "num" is not specified, the cursor will be on the last line of the file.
+/{pat} Open editor with cursor on the first occurrence of {pat}.
-c {command}--cmd {command} A "ex" command in dowble quotes will be processed against the file specified.
-b Binary file mode.
-C-v VI compatibility mode. Loses the more advanced vim features.
-d Diff file mode. Must list all files to perform a diff upon (list 2, 3 or 4 files). Same as vimdiff.
-g GUI gvim mode (if compiled in and available).
-h--help Print help messages. Also see vimtutor
-i filename Specify viminfo file. Default is ~/.viminfo
-r-L Recovery mode. Used after a crash. The ".swp" file is used. See ":help recovery".
-M-R File modifications and write not allowed.
-n Prohibit ".swp" file generation. Required for special devices of limited space.
-x Use encryption when writing files. Will prompt for a crypt key.
--noplugin Skip loading plugins.
--version Print vim version. One edits a file in vi by issuing the command: vi file-to-edit.txt
l TOP

OS/2 The OS/2 version runs in a console window. For Vim 5.x and Vim 6 look in the os2 directory. Version 6.2 is not available. Versions 6.3 and 6.4 were compiled by David Sanders. Version 7.0 was compiled by David Sanders. Runtime files vim##rt.zip vim70rt.zip Documentation, syntax files, etc. You always need this. Executables vim##os2.zip vim70os2.zip Vim, Xxd, Tee and EMX libraries.

PC: MS-DOS and MS-Windows For modern MS-Windows systems (starting with XP) you can simply use the executable installer: gvim74.exe It includes GUI and console versions, for 32 bit and 64 bit systems. You can select what you want to install and includes an uninstaller. Since there are so many different versions of MS operating systems, there are several versions of Vim for them. For Vim 5.x, Vim 6.x and Vim 7 look in the pc directory. Self-installing executable gvim##.exe gvim74.exe For Vim 6 and later. This includes a GUI version of Vim - with many features and OLE support - and all the runtime files. It works well on MS-Windows 95/98/ME/NT/2000/XP/Vista/7. Use this if you have enough disk space and memory. It's the simplest way to start using Vim on the PC. The installer allows you to skip the parts you don't want. For Vim 6.3 and later it also includes a console version, both for MS-Windows 95/98/ME and MS-Windows NT/2000/XP/Vista/7. The installer automatically selects the right one. For the latest version with all patches included see Cream below. These versions are unofficial, but the download number is high and complaints are few.

Unix The best way to install Vim on Unix is to use the sources. This requires a compiler and its support files. Compiling Vim isn't difficult at all. You can simply type "make install" when you are happy with the default features. Edit the Makefile in the "src" directory to select specific features. You need to download at the sources and the runtime files. And apply all the latest patches. For Vim 6 up to 7.2 you can optionally get the "lang" archive, which adds translated messages and menus. For 7.3 and later this is included with the runtime files.

Vim 7.4 is the latest stable version. It is highly recommended, many bugs have been fixed since 7.2 and earlier. If you have a problem with it (e.g., when it's too big for your system), you could try version 6.4 or 5.8 instead. To avoid having to update this page for every new version, there are links to the directories. From there select the files you want to download. In the file names ## stands for the version number. For example, vim##src.zip with version 7.4 is vim74src.zip and vim-##-src.tar.gz for version 7.4 is vim-7.4-src.tar.gz. Links are provided for quick access to the latest version.

PC: MS-DOS and MS-Windows For modern MS-Windows systems (starting with XP) you can simply use the executable installer: gvim74.exe It includes GUI and console versions, for 32 bit and 64 bit systems. You can select what you want to install and includes an uninstaller. Since there are so many different versions of MS operating systems, there are several versions of Vim for them. For Vim 5.x, Vim 6.x and Vim 7 look in the pc directory. Self-installing executable gvim##.exe gvim74.exe For Vim 6 and later. This includes a GUI version of Vim - with many features and OLE support - and all the runtime files. It works well on MS-Windows 95/98/ME/NT/2000/XP/Vista/7. Use this if you have enough disk space and memory. It's the simplest way to start using Vim on the PC. The installer allows you to skip the parts you don't want. For Vim 6.3 and later it also includes a console version, both for MS-Windows 95/98/ME and MS-Windows NT/2000/XP/Vista/7. The installer automatically selects the right one. For the latest version with all patches included see Cream below. These versions are unofficial, but the download number is high and complaints are few.

Unix The best way to install Vim on Unix is to use the sources. This requires a compiler and its support files. Compiling Vim isn't difficult at all. You can simply type "make install" when you are happy with the default features. Edit the Makefile in the "src" directory to select specific features. You need to download at the sources and the runtime files. And apply all the latest patches. For Vim 6 up to 7.2 you can optionally get the "lang" archive, which adds translated messages and menus. For 7.3 and later this is included with the runtime files.

Vim 7.4 is the latest stable version. It is highly recommended, many bugs have been fixed since 7.2 and earlier. If you have a problem with it (e.g., when it's too big for your system), you could try version 6.4 or 5.8 instead. To avoid having to update this page for every new version, there are links to the directories. From there select the files you want to download. In the file names ## stands for the version number. For example, vim##src.zip with version 7.4 is vim74src.zip and vim-##-src.tar.gz for version 7.4 is vim-7.4-src.tar.gz. Links are provided for quick access to the latest version.

Finally, after more than a thousand patches, there is a new version of Vim. This is mostly a bug-fix release. Also, many runtime files have been improved, syntax highlighting and indenting works better. To find out the details, do ":help version7.4" after installing it. Direct link to the MS-Windows installer. For Linux you probably want to use Mercurial for convenient building. For MacVim look here. Otherwise see the download page.

Details and options for: Unix PC: MS-DOS and MS-Windows Amiga OS/2 Macintosh Others Mirrors Alternative sites to download Vim files from. Sources Build Vim yourself and/or make changes. Mercurial Obtain Vim sources with a Mercurial client (recommended). Patches Include the latest improvements (requires sources and rebuilding). Runtime Get the latest syntax files, documentation, etc.. Script links Links to individual syntax, indent, color, compiler and ftplugin scripts. Translations Non-English documentation packages. Versions before 7.3 can also be obtained with Subversion and CVS.

Useful operations

Useful operations

Vim can assist in editing text documents.

If you are editing plain text documents, Vim can assist you in a variety of ways. One of the features that is essential for this function is spell check.

One way to manage multiple files is through buffers. Buffers usually represent a file open for editing. They are basically everything that Vim has open currently and can get access to easily.

A separate control mechanism that Vim has for managing multiple files is the concept of windows or views. This allows you to split the current editing area into different windows so that you can view multiple buffers at the same time.

You can move the cursor around with arrow keys; however, this is possible only if they are available.