Vim FAQ

FAQ
select color
help
manual
PS / PDF
single file
VimOnline
Vim.org
search
VimDoc



This Vim FAQ is created from the questions and answers posted to the
vim@vim.org user mailing list and the comp.editors newsgroup. There are
several ways to solve a problem in Vim. This FAQ gives one of those several
possibilities. You can explore the other ways using the information and
links given in this FAQ. The credit for the answers in this FAQ goes to
Peppe, Benji, Charles Campbell and numerous others.

INDEX

SECTION 1 - GENERAL INFORMATION
    1.1. What is Vim?
    1.2. Who wrote Vim?
    1.3. Is Vim compatible with Vi?
    1.4. What are some of the improvements of Vim over Vi?
    1.5. Is Vim free?

SECTION 2 - RESOURCES
    2.1. Where can I learn more about Vim?
    2.2. Is there a mailing list available?
    2.3. Is there an archive available for the Vim mailing lists?
    2.4. Where can I get the Vim user manual in HTML/PDF/PS format?
    2.5. I have a "xyz" (some) problem with Vim. How do I determine it is a
         problem with my setup or with Vim?
    2.6. Where can I report bugs?
    2.7. Where can the FAQ be found?
    2.8. What if I don't find an answer in this FAQ?
    2.9. I have a patch for implementing a Vim feature. Where do I send the
         patch?
    2.10. I have a Vim tip or developed a new Vim
          syntax/indent/filetype/compiler plugin or developed a new script
          or a colorscheme. Is there a public website where I can upload
          this?

SECTION 3 - AVAILABILITY
    3.1. What is the latest version of Vim? (*Updated*)
    3.2. Where can I find the latest version of Vim? (*Updated*)
    3.3. What platforms does it run on?
    3.4. Where can I download the latest version of the Vim runtime files?
         (*New*)

SECTION 4 - HELP
    4.1. How do I use the help files?
    4.2. How do I search for a keyword in the Vim help files?
    4.3. I am getting an error message E123, what did I do wrong?
    4.4. Where can I read about the various modes in Vim?
    4.5. How do I generate the Vim help tags file after adding a new Vim
         help file?
    4.6. Can I use compressed versions of the help files?

SECTION 5 - EDITING A FILE
    5.1. I am currently editing a file in Vim. How do I load another file
         into Vim?
    5.2. How do I save the current file in another name (save as) and edit
         a new file?
    5.3. How do I change the current directory to the directory of the
         current file?
    5.4. How do I write a file without the line feed (EOL) at the end of
         the file?
    5.5. How do I configure Vim to open a file at the last edited location?
    5.6. When editing a file in Vim, which is being changed by an external
         application, Vim opens a warning window (like the confirm dialog)
         each time a change is detected. How do I disable this warning?
    5.7. How do I edit a file whose name is under the cursor?
    5.8. How do I reload/re-edit the current file?
    5.9. How do I autosave a file periodically?
    5.10. How do I open a file in read-only mode?

SECTION 6 - EDITING MULTIPLE FILES
    6.1. How do I switch between files while editing multiple files in Vim?
    6.2. How do I configure Vim to autoload several files at once similar
         to "work-sets" or "projects"?
    6.3. Is it possible to open multiple top level windows in a single
         instance of Vim similar to Nedit or emacs?
    6.4. How do I open multiple files at once from within Vim?
    6.5. How do I open several windows in Vim by supplying command line
         arguments?
    6.6. How do I browse/explore directories from within Vim?
    6.7. How do I edit files over a network using ftp/scp/rcp/http?

SECTION 7 - BACKUP
    7.1. When I edit and save files, Vim creates a file with the same name
         as the original file and a "~" character at the end. How do I stop
         Vim from creating this file (or) How do I disable the Vim backup
         file feature?
    7.2. How do I configure Vim to store all the backup files in a
         particular directory? (*New*)
    7.3. When I save a file with Vim, the file permissions are changed.
         How do I configure Vim to save a file without changing the file
         permissions?

SECTION 8 - BUFFERS
    8.1. I have made some modifications to a buffer. How do I edit another
         buffer without saving the modified buffer and also without losing
         the modifications?
    8.2. How do I configure Vim to auto-save a modified buffer when
         switching to another buffer?
    8.3. How do I replace the buffer in the current window with a blank
         buffer?
    8.4. Is there a keyboard shortcut to load a buffer by the buffer
         number?
    8.5. How do I open all the current buffers in separate windows?
    8.6. How do I close (delete) a buffer without exiting Vim?
    8.7. I have several buffers opened with ":e filename". How do I close
         one of the buffers without exiting Vim?
    8.8. When I use the command ":%bd" to delete all the buffers, not all
         the buffers are deleted. Why?
    8.9. How do I display the buffer number of the current buffer/file?
    8.10. How do I delete a buffer without closing the window in which the
          buffer is displayed?
    8.11. How do I map the tab key to cycle through and open all the
          buffers? (*New*)

SECTION 9 - WINDOWS
    9.1. What is the difference between a Vim window and a buffer?
    9.2. How do I increase the width of a Vim window?
    9.3. How do I zoom into or out of a window? (*New*)
    9.4. How do I execute an ex command on all the open buffers or open
         windows or all the files in the argument list? (*Updated*)

SECTION 10 - MOTION
    10.1. How do I jump to the beginning (first line) or end (last line) of
         a file?
    10.2. In insert mode, when I press the <Esc> key to go to command mode,
         the cursor moves one character to the left (except when the cursor
         is on the first character of the line). Is it possible to change
         this behavior to keep the cursor at the same column?
    10.3. How do I configure Vim to maintain the horizontal cursor position
         when scrolling with the <Page Up>, <Page Down>, etc keys?
    10.4. Some lines in a file are more than the screen width and they are
         all wrapped. When I use the j, k keys to move from one line to the
         next, the cursor is moved to the next line in the file instead of
         the next line on the screen. How do I move from one screen line to
         the next?
    10.5. What is the definition of a sentence, paragraph and section in
         Vim?
    10.6. How do I jump to beginning or end of a sentence, paragraph or a
         section?
    10.7. I have lines in a file that extends beyond the right extent of the
         screen. How do I move the Vim view to the right to see the text
         off the screen?
    10.8. How do I scroll two or more buffers simultaneously?
    10.9. When I use my arrow keys, Vim changes modes, inserts weird
         characters in my document but doesn't move the cursor properly.
         What's going on?
    10.10. How do I configure Vim to move the cursor to the end of the
          previous line, when the left arrow key is pressed and the cursor
          is currently at the beginning of a line?
    10.11. How do I configure Vim to stay only in insert mode (modeless
          editing)?
    10.12. How do I display some context lines when scrolling text?
    10.13. How do I go back to previous cursor locations? (*New*)

SECTION 11 - SEARCHING TEXT
    11.1. After I searched for a text with a pattern, all the matched text
          stays highlighted. How do I turn off the highlighting
          temporarily/permanently?
    11.2. How do I enter a carriage return character in a search pattern?
    11.3. How do I search for the character ^M?
    11.4. How can I search/replace characters that display as '~R', '~S',
          etc.?
    11.5. How do I highlight all the non-printable characters in a file?
    11.6. How do I search for whole words in a file?
    11.7. How do I search for the current word under the cursor?
    11.8. How do I search for a word without regard to the case (uppercase
          or lowercase)?
    11.9. How do I search for words that occur twice consecutively?
    11.10. How do I count the number of times a particular word occurs in a
           buffer?
    11.11. How do I place the cursor at the end of the matched word when
           searching for a pattern?
    11.12. How do I search for an empty line?
    11.13. How do I search for a line containing only a single character?
    11.14. How do I search and replace a string in multiple files?
    11.15. I am using the ":s" substitute command in a mapping. When a
           search for a pattern fails, the map terminates. I would like the
           map to continue processing the next command, even if the
           substitute command fails. How do I do this?
    11.16. How do I search for the n-th occurrence of a character in a
           line?
    11.17. How do I replace a tab (or any other character) with a hard
           return (newline) character?
    11.18. How do I search for a character by its ASCII value?
    11.19. How do I search for long lines?
    11.20. How do I display all the lines in the current buffer that
           contain a specified pattern? (*New*)
    11.21. How do I search for a text string that spans multiple lines?
           (*New*)

SECTION 12 - CHANGING TEXT
    12.1. How do I delete all the trailing white space characters (SPACE
          and TAB) at the end of all the lines in a file?
    12.2. How do I replace all the occurrences of multiple consecutive
          space characters to a single space?
    12.3. How do I reduce a range of empty lines into one line only?
    12.4. How do I delete all blank lines in a file? How do I remove all
          the lines containing only space characters?
    12.5. How do I copy/yank the current word? (*Updated*)
    12.6. How do I yank text from one position to another position within a
          line, without yanking the entire line?
    12.7. When I yank some text into a register, how do I append the text
          to the current contents of the register?
    12.8. How do I yank a complete sentence that spans over more than one
          line?
    12.9. How do I yank all the lines containing a pattern into a buffer?
    12.10. How do I delete all the lines in a file that does not contain a
           pattern?
    12.11. How do I add a line before each line with "pattern" in it?
    12.12. Is there a way to operate on a line if the previous line
           contains a particular pattern?
    12.13. How do I execute a command on all the lines containing a
           pattern?
    12.14. Can I copy the character above the cursor to the current cursor
           position?
    12.15. How do I insert a blank line above/below the current line
           without entering insert mode?
    12.16. How do I insert the name of current file into the current
           buffer?
    12.17. How do I insert the contents of a Vim register into the current
           buffer? (*New*)
    12.18. How do I move the cursor past the end of line and insert some
           characters at some columns after the end of the line?
    12.19. How to replace the word under the cursor (say: junk) with
           "foojunkbar" in Vim?
    12.20. How do I replace a particular text in all the files in a
           directory?
    12.21. I have some numbers in a file. How do I increment or decrement
           the numbers in the file?
    12.22. How do I reuse the last used search pattern in a ":substitute"
           command?
    12.23. How do I change the case of a string using the ":substitute"
           command?
    12.24. How do I enter characters that are not present in the keyboard?
    12.25. Is there a command to remove any or all digraphs?
    12.26. In insert mode, when I press the backspace key, it erases only
           the characters entered in this instance of insert mode. How do I
           erase previously entered characters in insert mode using the
           backspace key?
    12.27. I have a file which has lines longer than 72 characters
           terminated with "+" and wrapped to the next line. How can I
           quickly join the lines?
    12.28. How do I paste characterwise yanked text into separate lines?
    12.29. How do I change the case (uppercase, lowercase) of a word or
           a character or a block of text?
    12.30. How do I enter ASCII characters that are not present in the
           keyboard?
    12.31. How do I replace non-printable characters in a file?
    12.32. How do I remove duplicate lines from a buffer?
    12.33. How do I prefix all the lines in a file with the corresponding
           line numbers?
    12.34. How do I exchange (swap) two characters or words or lines?
           (*New*)

SECTION 13 - COMPLETION IN INSERT MODE
    13.1. How do I complete words or lines in insert mode?
    13.2. How do I complete file names in insert mode?
    13.3. I am using CTRL-P/CTRL-N to complete words in insert mode. How do
          I complete words that occur after the just completed word?

SECTION 14 - TEXT FORMATTING
    14.1. How do I format a text paragraph so that a new line is inserted
          at the end of each wrapped line?
    14.2. How do I format long lines in a file so that each line contains
          less than 'n' characters?
    14.3. How do I join short lines to the form a paragraph?
    14.4. How do I format bulleted and numbered lists?
    14.5. How do I indent lines in insert mode? (*Updated*)
    14.6. How do I format/indent an entire file?
    14.7. How do I increase or decrease the indentation of the current
          line?
    14.8. How do I indent a block/group of lines?
    14.9. When I indent lines using the > or < key, the standard 8-tabstops
          are used instead of the current 'tabstop' setting. Why?
    14.10. How do I turn off the automatic indentation of text?
    14.11. How do I configure Vim to automatically set the 'textwidth'
           option to a particular value when I edit mails?
    14.12. Is there a way to make Vim auto-magically break lines?
    14.13. I am seeing a lot of ^M symbols in my file. I tried setting the
           'fileformat' option to 'dos' and then 'unix' and then 'mac'.
           None of these helped. How can I hide these symbols?
    14.14. When I paste some text into a Vim buffer from another
           application, the alignment (indentation) of the new text is
           messed up. How do I fix this?
    14.15. When there is a very long wrapped line (wrap is "on") and a line
           doesn't fit entirely on the screen it is not displayed at all.
           There are blank lines beginning with '@' symbol instead of
           wrapped line. If I scroll the screen to fit the line the '@'
           symbols disappear and the line is displayed again. What Vim
           setting control this behavior?
    14.16. How do I convert all the tab characters in a file to space
           characters?
    14.17. What Vim options can I use to edit text that will later go to a
           word processor?

SECTION 15 - VISUAL MODE
    15.1. How do I do rectangular block copying?
    15.2. How do I delete or change a column of text in a file?
    15.3. How do I apply an ex-command on a set of visually selected lines?
    15.4. How do I execute an ex command on a column of text selected in
          Visual block mode?
    15.5. How do I select the entire file in visual mode?
    15.6. When I visually select a set of lines and press the > key to
          indent the selected lines, the visual mode ends. How can I
          reselect the region for further operation?  (or) How do I
          re-select the last selected visual area again? (*Updated*)
    15.7. How do I jump to the beginning/end of a visually selected region?
    15.8. When I select text with mouse and then press : to enter an ex
          command, the selected text is replaced with the : character. How
          do I execute an ex command on a text selected using the mouse
          similar to the text selected using the visual mode?
    15.9. When I select a block of text using the mouse, Vim goes into
          selection mode instead of Visual mode. Why?

SECTION 16 - COMMAND-LINE MODE
    16.1. How do I use the name of the current file in the command mode or
          an ex command line?
    16.2. How do I edit the text in the Vim command-line effectively?
    16.3. How do I switch from Vi mode to Ex mode?
    16.4. How do I copy the output from an ex-command into a buffer?
    16.5. When I press the tab key to complete the name of a file in the
          command mode, if there are more than one matching file names,
          then Vim completes the first matching file name and displays a
          list of all matching filenames. How do I configure Vim to only
          display the list of all the matching filenames and not complete
          the first one?
    16.6. How do I copy text from a buffer to the command line and from the
          command line to a buffer?
    16.7. How do I put a command onto the command history without executing
          it?
    16.8. How do I increase the height of the command-line? (*New*)

SECTION 17 - VIMINFO
    17.1. When I invoke Vim, I get error messages about illegal characters
          in the viminfo file. What should I do to get rid of these
          messages?
    17.2. How do I disable the viminfo feature?
    17.3. How do I save and use Vim marks across Vim sessions?

SECTION 18 - REMOTE EDITING
    18.1. How do I open a file with existing instance of gvim? What
          happened to the Vim 5.x OpenWithVim.exe and SendToVim.exe files?
    18.2. How do I send a command to a Vim server to write all buffers to
          disk?
    18.3. Where can I get the documentation about the Vim remote server
          functionality?

SECTION 19 - OPTIONS
    19.1. How do I configure Vim in a simple way?
    19.2. How do I toggle the value of an option?
    19.3. How do I set an option that affects only the current
          buffer/window?
    19.4. How do I use space characters for a Vim option value?
    19.5. Can I add (embed) Vim option settings to the contents of a file?
    19.6. How do I display the line numbers of all the lines in a file?
    19.7. How do I change the width of the line numbers displayed using the
          "number" option? (*Updated*)
    19.8. How do I display (view) all the invisible characters in a file?
    19.9. How do I configure Vim to always display the current line and
          column number?
    19.10. How do I display the current Vim mode?
    19.11. How do I configure Vim to show pending/partial commands on the
           status line?
    19.12. How do I configure the Vim status line to display different
           settings/values?
    19.13. How do I configure Vim to display status line always?
    19.14. How do I make a Vim setting persistent across different Vim
           invocations/instances/sessions?
    19.15. Why do I hear a beep (why does my window flash) about 1 second
           after I hit the Escape key?
    19.16. How do I make the 'c' and 's' commands display a '$' instead of
           deleting the characters I'm changing?
    19.17. How do I remove more than one flag using a single ":set" command
           from a Vim option?

SECTION 20 - MAPPING KEYS
    20.1. How do I know what a key is mapped to?
    20.2. How do list all the user-defined key mappings?
    20.3. How do I unmap a key?
    20.4. I am not able to create a mapping for the <xxx> key. What is
          wrong?
    20.5. How do I map the numeric keypad keys?
    20.6. How do I create a mapping that works only in visual mode?
    20.7. In a Vim script, how do I know which keys to use for my mappings,
          so that the mapped key will not collide with an already used key?
    20.8. How do I map the escape key?
    20.9. How do I map a key to perform nothing?
    20.10. I want to use the Tab key to indent a block of text and
           Shift-Tab key to unindent a block of text. How do I map the keys
           to do this?  This behavior is similar to textpad, visual studio,
           etc.
    20.11. In my mappings the special characters like <CR> are not
           recognized. How can I configure Vim to recognize special
           characters?
    20.12. How do I use the '|' to separate multiple commands in a map?
    20.13. If I have a mapping/abbreviation whose ending is the beginning of
           another mapping/abbreviation, how do I keep the first from
           expanding into the second one?
    20.14. Why does it take a second or more for Vim to process a key,
           sometimes when I press a key?
    20.15. How do I map a key to run an external command using a visually
           selected text?
    20.16. How do I map the Ctrl-I key while still retaining the
           functionality of the <Tab> key?

SECTION 21 - ABBREVIATIONS
    21.1. How do I auto correct misspelled words? (*New*)
    21.2. How do I create multi-line abbreviations?
    21.3. When my abbreviations are expanded, an additional space character
          is added at the end of the expanded text. How do I avoid this
          character?
    21.4. How do I insert the current date/time stamp into the file?
    21.5. How do I prevent an abbreviation from expanding in insert mode?
          (*New*)

SECTION 22 - RECORD AND PLAYBACK
    22.1. How do I repeat an editing operation (insertion, deletion, paste,
          etc)? (*New*)
    22.2. How I record and repeat a set of key sequences?
    22.3. How do I edit/modify a recorded set of key sequences?
    22.4. How do I write recorded key sequences to a file?
    22.5. I am using register 0 to record my key sequences (i.e. q0 ....
          q). In the recorded key sequences, I am yanking some text.  After
          the first replay of the recorded key sequence, I am no longer
          able to play it back.

SECTION 23 - AUTOCOMMANDS
    23.1. How do I execute a command when I try to modify a read-only file?
    23.2. How do I execute a command every time when entering a buffer?
    23.3. How do I execute a command every time when entering a window?
    23.4. From an autocmd, how can I determine the name of the file or the
          buffer number for which the autocommand is executed?
    23.5. How do I automatically save all the changed buffers whenever Vim
          loses focus?
    23.6. How do I execute/run a function when Vim exits to do some
          cleanup?

SECTION 24 - SYNTAX HIGHLIGHT
    24.1. How do I turn off/on syntax highlighting?
    24.2. How do I change the background and foreground colors used by Vim?
    24.3. How do I change the highlight colors to suit a dark/light
          background?
    24.4. How do I change the color of the line numbers displayed when the
          ":set number" command is used?
    24.5. How do I change the background color used for a Visually selected
          block?
    24.6. How do I highlight the special characters (tabs, trailing spaces,
          end of line, etc) displayed by the 'list' option?
    24.7. How do I specify a colorscheme in my .vimrc/.gvimrc file, so that
          Vim uses the specified colorscheme everytime?
    24.8. Vim syntax highlighting is broken. When I am editing a file, some
          parts of the file is not syntax highlighted or syntax highlighted
          incorrectly.
    24.9. Is there a built-in function to syntax-highlight the
          corresponding matching bracket?
    24.10. How do I turn off the C comment syntax highlighting?
    24.11. How do I add my own syntax extensions to the standard syntax
           files supplied with Vim?
    24.12. How do I replace a standard syntax file that comes with the Vim
           distribution with my own syntax file?
    24.13. How do I highlight all the characters after a particular column?
    24.14. How do I convert a source file (.c, .h, etc) with the Vim syntax
           highlighting into a HTML file?
    24.15. How do I list the definition of all the current highlight
           groups? (*New*)

SECTION 25 - VIM SCRIPT WRITING
    25.1. How do I list the names of all the scripts sourced by Vim?
    25.2. How do I debug Vim scripts?
    25.3. How do I locate the script/plugin which sets a Vim option?
    25.4. I am getting some error/informational messages from Vim (possibly
          when running a script), the messages are cleared immediately. How
          do I display the messages again?
    25.5. How do I save and restore a plugin specific information across
          Vim invocations?
    25.6. How do I start insert mode from a Vim function?
    25.7. How do I change the cursor position from within a Vim function?
    25.8. How do I check the value of an environment variable in the .vimrc
          file?
    25.9. How do I check whether an environment variable is set or not from
          a Vim function?
    25.10. How do I call/use the Vim built-in functions?
    25.11. I am using some normal mode commands in my Vim script. How do I
           avoid using the user-defined mappings for these normal mode
           commands and use the standard Vim functionality for these normal
           mode commands?
    25.12. How do I get a visually selected text into a Vim variable or
           register?
    25.13. I have some text in a Vim variable 'myvar'. I would like to use
           this variable in a ":s" substitute command to replace a text
           'mytext'. How do I do this?
    25.14. A Vim variable (bno) contains a buffer number. How do I use this
           variable to open the corresponding buffer?
    25.15. How do I store the value of a Vim option into a Vim variable?
    25.16. I have copied and inserted some text into a buffer from a Vim
           function. How do I indent the inserted text from the Vim
           function?
    25.17. How do I get the character under the cursor from a Vim script?
    25.18. How do I get the name of the current file without the extension?
    25.19. How do I get the basename of the current file?
    25.20. How do I get the output from a Vim function into the current
           buffer?
    25.21. How do I call external programs from a Vim function?
    25.22. How do I get the return status of a program executed using the
           ":!" command?
    25.23. How do I determine whether the current buffer is modified or
           not?
    25.24. I would like to use the carriage return character in a normal
           command from a Vim script. How do I specify the carriage return
           character?
    25.25. How do I split long lines in a Vim script?
    25.26. When I try to "execute" my function using the "execute 'echo
           Myfunc()'" command, the cursor is moved to the top of the
           current buffer.  Why?
    25.27. How do I source/execute the contents of a register?
    25.28. After calling a Vim function or a mapping, when I press the 'u'
           key to undo the last change, Vim undoes all the changes made by
           the mapping/function. Why?
    25.29. How can I call a function defined with s: (script local
           function) from another script/plugin?
    25.30. Is it possible to un-source a sourced script? In otherwords,
           reverse all the commands executed by sourcing a script.

SECTION 26 - PLUGINS
    26.1. How do I set different options for different types of files?
    26.2. I have downloaded some Vim plugins, syntax files, indent files,
          color schemes, filetype plugins, etc from the web. Where should I
          copy these files so that Vim will find them?
    26.3. How do I extend an existing filetype plugin?
    26.4. How do I turn off loading the Vim plugins?
    26.5. How do I turn on/off loading the filetype plugins?
    26.6. How do I override settings made in a file type plugin in the
          global ftplugin directory for all the file types?
    26.7. How do I disable the Vim directory browser plugin?
    26.8. How do I set the filetype option for files with names matching a
          particular pattern or depending on the file extension?

SECTION 27 - EDITING PROGRAM FILES
    27.1. How do I enable automatic indentation for C/C++ files?
    27.2. How do I configure the indentation used for C/C++ files?
    27.3. How do I turn off the automatic indentation feature?
    27.4. How do I change the number of space characters used for the
          automatic indentation?
    27.5. I am editing a C program using Vim. How do I display the
          definition of a macro or a variable?
    27.6. I am editing a C program using Vim. How do I jump to the
          beginning or end of a code block from within the block?
    27.7. Is there a way to turn off the "//" comment auto-insertion
          behavior for C++ files?  If I'm sitting on a line beginning with
          "//", then I open a new line above or below it, Vim automatically
          inserts new "//" chars.
    27.8. How do I add the comment character '#' to a set of lines at the
          beginning of each line?
    27.9. How do I edit a header file with the same name as the
          corresponding C source file?
    27.10. How do I automatically insert comment leaders while typing
           comments?

SECTION 28 - QUICKFIX
    28.1. How do I build programs from Vim?
    28.2. When I run the make command in Vim I get the errors listed as the
          compiler compiles the program. When it finishes this list
          disappears and I have to use the  :clist command to see the error
          message again. Is there any other way to see these error
          messages?

SECTION 29 - FOLDING
    29.1. How do I extend the Vim folding support?
    29.2. When I enable folding by setting the 'foldmethod' option, all the
          folds are closed. How do I prevent this?
    29.3. How do I control how many folds will be opened when I start
          editing a file?
    29.4. How do I open and close folds using the mouse?
    29.5. How do I change the text displayed for a closed fold?
    29.6. How do I store and restore manually created folds across
          different Vim invocations?

SECTION 30 - VIM WITH EXTERNAL APPLICATIONS
    30.1. Can I run a shell inside a Vim window?
    30.2. How do I pass the word under the cursor to an external command?
    30.3. How do I get the output of a shell command into a Vim buffer?
    30.4. How do I pipe the contents of the current buffer to an external
          command and replace the contents of the buffer with the output
          from the command? (*Updated*)
    30.5. How do I sort a section of my file?
    30.6. Is there a step-by-step guide for using Vim with slrn?
    30.7. How do I use Vim as a pager?
    30.8. How do I view Unix man pages from inside Vim?
    30.9. How do I change the diff command used by the Vim diff support?
    30.10. How do I use the Vim diff mode without folding?

SECTION 31 - GUI VIM
    31.1. How do I create buffer specific menus?
    31.2. How do I change the font used by GUI Vim?
    31.3. When starting GUI Vim, how do I specify the location of the GVIM
          window?
    31.4. How do I add a horizontal scrollbar in GVim?
    31.5. How do I make the scrollbar appear in the left side by default?
    31.6. How do I remove the Vim menubar?
    31.7. I am using GUI Vim. When I press the ALT key and a letter, the
          menu starting with that letter is selected. I don't want this
          behavior as I want to map the ALT-<key> combination. How do I do
          this?
    31.8. Is it possible to scroll the text by dragging the scrollbar so
          that the cursor stays in the original location?
    31.9. How do I get gvim to start browsing files in a particular
          directory when using the ":browse" command?
    31.10. For some questions, like when a file is changed outside of Vim,
          Vim displays a GUI dialog box. How do I replace this GUI dialog
          box with a console dialog box?
    31.11. I am trying to use GUI Vim as the editor for my xxx application.
           When the xxx application launches GUI Vim to edit a file, the
           control immediately returns to the xxx application. How do I
           start GUI Vim, so that the control returns to the xxx
           application only after I quit Vim?
    31.12. Why does the "Select Font" dialog doesn't show all the fonts
           installed in my system?
    31.13. How do I use the mouse in Vim command-line mode?
    31.14. When I use the middle mouse button to scroll text, it pastes the
           last copied text. How do I disable this behavior?
    31.15. How do I change the location and size of a GUI Vim window?

SECTION 32 - VIM ON UNIX
    32.1. I am running Vim in a xterm. When I press the CTRL-S key, Vim
          freezes. What should I do now?
    32.2. I am seeing weird screen update problems in Vim. What can I do to
          solve this screen/display update problems?
    32.3. I am using the terminal/console version of Vim. In insertmode,
          When I press the backspace key, the character before the cursor
          is not erased. How do I configure Vim to do this?
    32.4. I am using Vim in a xterm. When I quit Vim, the screen contents
          are restored back to the original contents. How do I disable
          this?
    32.5. When I start Vim, it takes quite a few seconds to start. How do I
          minimize the startup time?
    32.6. How can I make the cursor in gvim in unix stop blinking?
    32.7. How do I change the menu font on GTK Vim?
    32.8. How do I prevent <Ctrl-Z> from suspending Vim?
    32.9. When I kill the xterm running Vim, the Vim process continues to
          run and takes up a lot of CPU (99%) time. Why is this happening?
    32.10. How do I get the Vim syntax highlighting to work in a Unix
           terminal?

SECTION 33 - VIM ON MS-WINDOWS
    33.1. In MS-Windows, CTRL-V doesn't start the blockwise visual mode.
          What happened?
    33.2. When I press the CTRL-Y key, it acts like the CTRL-R key. How do
          I configure Vim to treat CTRL-Y as CTRL-Y?
    33.3. How do I start GUI Vim in a maximized window always?
    33.4. After doing some editing operations, Vim freezes. The cursor
          becomes an empty rectangle. I am not able enter any characters.
          What is happening?
    33.5. I am using Windows XP, the display speed of maximized GVim is
          very slow. What can I do to speed the display updates?
    33.6. What are the recommended settings for using Vim with cygwin?
    33.7. I am trying to use GNU diff with Vim diff mode. When I run the
          diff from command line, it works. When I try to use the diff with
          Vim it doesn't work. What should I do now?
    33.8. Is it possible to use Vim as an external editor for MS-Windows
          Outlook email client?
    33.9. I am using Vim to edit HTML files. How do I start internet
          explorer with the current file to preview the HTML file?
    33.10. I would like to use Vim with Microsoft Visual Studio. How do I
           do this?
    33.11. Where do I place the _vimrc and _gvimrc files?
    33.12. Everytime I save a file, Vim warns about the file being changed
           outside of Vim. Why?

SECTION 34 - PRINTING
    34.1. How do I print a file along with line numbers for all the lines?
    34.2. How do I print a file with the Vim syntax highlighting colors?

SECTION 35 - BUILDING VIM FROM SOURCE
    35.1. How do I build Vim from the sources on a Unix system?
    35.2. How do I install Vim in my home directory or a directory other
          than the default installation directory in Unix?
    35.3. How do I build Vim from the sources on a MS-Windows system?
          (*Updated*)
    35.4. The Vim help, syntax, indent files are missing from my Vim
          installation. How do I install these files?
    35.5. I have built Vim from the source and installed the Vim package
          using "make install". Do I need to keep the Vim source directory?
    35.6. How do I determine the Vim features which are enabled at compile
          time?
    35.7. Can I build Vim without the GUI support?
    35.8. When building Vim on a Unix system, I am getting "undefined
          reference to term_set_winsize' error. How do I resolve this
          error?
    35.9. Vim configure keeps complaining about the lack of gtk-config
          while trying to use GTK 2.03. This is correct, since in GTK 2
          they moved to using the generic pkg-config. I can get pkg-config
          to list the various includes and libs for gtk, but for some
          reason the configure script still isn't picking this up.

SECTION 36 - VARIOUS
    36.1. How do I edit binary files with Vim?
    36.2. How do I disable the visual error flash and the error beep?
    36.3. How do I display the ascii value of a character displayed in a
          buffer?
    36.4. Can I use zero as a count for a Vim command?
    36.5. How do I disable the Vim welcome screen?
    36.6. How do I avoid the "hit enter to continue" prompt?
    36.7. How do I invoke Vim from command line to run a group of commands
          on a group of files?
    36.8. How do I use a normal mode command from insert mode without
          leaving the insert mode? (*New*)

SECTION 37 - UNICODE
    37.1. Is it possible to create Unicode files using Vim?
    37.2. Which Vim settings are particularly important for editing Unicode
          files?
    37.3. What is the 'encoding' option?
    37.4. How does Vim name the various Unicode encodings?
    37.5. How does Vim specify the presence or absence of a byte-order
          mark?
    37.6. What is the 'fileencoding' option?
    37.7. What is the 'fileencodings' option?
    37.8. What is the 'termencoding' option?
    37.9. What is the 'bomb' option?
    37.10. Where can I find an example of a typical use of all these
           options?
    37.11. How can I insert Unicode characters into a file using Vim?
    37.12. How can I know which digraphs are defined and for which
           characters?


=============================================================================

SECTION 1 - GENERAL INFORMATION


1.1. What is Vim?

Vim stands for Vi IMproved. It used to be Vi IMitation, but there are so
many improvements that a name change was appropriate. Vim is a text editor
which includes almost all the commands from the Unix program "Vi" and a lot
of new ones. All commands can be given with the keyboard. This has the
advantage that you can keep your fingers on the keyboard and your eyes on
the screen. For those who want it, there is mouse support and a GUI version
with scrollbars and menus.

Vim is an editor, not a word processor. A word processor is used mainly to
do layout of text. This means positioning it, changing the way it appears
on output. More often than not, the final document is meant to be printed
or typeset or what have you, in order to present it in a pleasing manner to
others. Examples of word processors are Microsoft Word, WordPerfect,
FrameMaker, and AmiPro.

An editor is simply for entering text. Any typesetting or laying out of the
document is secondary. With an editor, one's main concern is entering text,
not making the text look good. Examples of editors other than Vim and Vi
are Emacs, Crisp, Brief, and xedit.  And Notepad.

For more information, read

    :help intro


1.2. Who wrote Vim?

Most of Vim was written by Bram Moolenar, with contributions from too many
people to mention here. See ":h credits" for a complete list.

Vim is based on Stevie, worked on by Tim Thompson, Tony Andrews and G.R.
(Fred) Walter.

For more information, read

    :help author


1.3. Is Vim compatible with Vi?

Yes. Vim is very much compatible with Vi.  You can use the "-C"
command-line flag to start Vim in Vi compatible mode:

    $ vim -C

You can also use:

    $ vim -u NONE

You can also set the 'compatible' option to enable Vi compatibility:

    :set compatible

For more information, read

    :help -C
    :help 'compatible'
    :help compatible-default


1.4. What are some of the improvements of Vim over Vi?

A short summary of the improvements of Vim over vi is listed below.  The
list shows that Vim is a thoroughly modern and feature-packed editor.
Standard features of modern editors are implemented, and there is an equal
emphasis on general power-user features and features for programmers.

Features to modernise Vi:

  Multi-level undo
     Allows you to set the number of times you can undo your changes in a
     file buffer. You can also redo an undone change.
  Multiple windows and buffers
     Each file can be displayed in its own window. You can move easily from
     one window to another. Each file opened during a Vim session also has
     an associated buffer and you can easily jump from one to the other.
  Flexible insert mode
     Vim allows you to use the arrow keys while in insert mode to move
     around in the file. No more hitting <Esc>, moving around, then hitting
     `i' or `a'.
  Macros
     Vim has a facility which allows you to record a sequence of typed
     characters and repeat them any number of times.
  Visual mode
     You can highlight sections of text and execute operations on this
     section of text only.
  Block operators
     Allow selection and highlighting of rectangular blocks of text in
     order do execute specific operations on them.
  Online help system
     You can easily find help on any aspect of using Vim. Help is displayed
     in its own window.
  Command-line editing and history
     History allows you to use the arrow keys to repeat or search for a
     command that has already been typed. Allows you to match the beginning
     of a command with the beginning of another similar command in the
     history buffer. You can also edit a command to correct typos or change
     a few values.
  Command line completion.
     Using the <Tab> key, you can complete commands, options, filenames,
     etc. as needed.
  Horizontal scrolling.
     Long lines can be scrolled horizontally (with or without the GUI).

Advanced user features:

  Text formatting.
     With two keystrokes, you can format large sections of text, without
     the use of external programs.
  Word completion in Insert mode
     Vim can complete words while you are typing, by matching the current
     word with other similar words in the file.
  Jump tags
     Just like in an internet browser, you can jump back to previous parts
     of the text you were editing, and then forward again.  Your brain is
     thus free to edit instead of navigate.
  Automatic commands
     Commands automatically executed when reading or writing a file,
     jumping to another buffer, etc.
  Viminfo
     Allows storing of the command line history, marks and registers in a
     file to be read on startup.  Therefore, you can recall old search
     patterns, macros, etc., in a new Vim session.
  Mouse support
     The mouse is supported in an xterm and for MS-DOS. It can be used to
     position the cursor, select the visual area, paste a register, etc.
  Graphical User Interface (GUI)
     Just like any modern editor.  Also, it's very easy to add your own
     menus.  Of course, console vim is still supported, and very widely
     used.
  Scripting language
     Vim has a powerful scripting language so new commands can be created.
     You can also use Perl, Python, TCL and Ruby to achieve the same thing!
  Plugins
     Extra functionality implemented via vim commands (regular commands or
     the scripting language) that is automatically loaded on startup.
     Examples: file explorer, network editing.  More are being developed
     and shared on VimOnline all the time.
  Syntax highlighting for many programming languages
     Syntax highlighting for hundreds of programming languages is
     supported. Support for others can be added.
  Extended regular expressions
     Vim supports extended regular expressions which are similar in
     functionality to that of perl regular expressions.

Programming performance features:

  Edit-compile-edit speedup
     You can compile within Vim and automatically jump to the location of
     errors in the source code.
  Indenting for many programming languages
     C, C++, Java, Perl, XML and many other languages can be automatically
     indented by vim while you type.  Support for others can be added.
  Searching for words in include files
     Vim allows you to search for a match of the word under the cursor in
     the current and included files.
  Advance text objects
     Instantly select, or delete, or copy, or indent, or format, or change
     case, or ... to all the text between ( and ), or { and }, or < and >,
     or [ and ].  Or a word, sentence, or paragraph.  Very powerful.
  Folding
     Certain parts of the text can be "folded" away.  The best example is
     the body of a function.  You can get an overview of the code, and then
     open the fold of the function whose detail you need to see.
  ctags and cscope integration
     Using these two powerful programs, you can jump to a definition of a
     function from a calling instance of it, and use other tricks to
     navigate source code.

For more information, read

    :help vi-differences


1.5. Is Vim free?

Vim is Charityware. There are no restrictions on using or copying Vim, but
the author encourages you to make a donation to charity. A document
explaining how to do so is included in the distribution.

For more information, read

    :help copyright


=============================================================================

SECTION 2 - RESOURCES


2.1. Where can I learn more about Vim?

You can post your Vim questions to the vim@vim.org mailing list.  You can
post your Vim development related questions to the vim-dev@vim.org mailing
list. Vim does not have a newsgroup of its own. But the appropriate
newsgroup to post to is comp.editors.

"VimOnline" is a web page that serves as a de facto homepage for vim,
although the main purpose of it is to gather tips and scripts from
everywhere.  Get involved!  The URL is vim.sourceforge.net or vim.sf.net.

Finally, read the Vi FAQ:

    http://www.faqs.org/faqs/editor-faq/vi/part1/index.html

For more information, read

    :help mail-list
    :help internet


2.2. Is there a mailing list available?

There are several:

    NAME              DESCRIPTION

    vim-announce      Announcements of new releases
    vim               General discussion
    vim-dev           Patches, bug reports, development discussions
    vim-mac           Macintosh discussion
    vim-fr            General discussion in French
    vim-multibyte     Multibyte development issues
    vim-vms           Development on VMS

Of these, only vim and vim-dev are of general interest.  vim-announce is
read-only to most people, and its messages are sent to the other lists as
well.  The remaining four are very low volume.

To subscribe:   send an email to <NAME>-subscribe@vim.org
To unsubscribe: send an email to <NAME>-unsubscribe@vim.org
To get help:    send an email to <NAME>-help@vim.org


2.3. Is there an archive available for the Vim mailing lists?

Yes.  Visit http://www.yahoogroups.com/list/<name>, where name is one of:
vimannounce, vim, vimdev, vim-fr, vim-mac, vim-multibyte, vim-vms.

Alternatively, visit www.gmane.org to find out about GMANE, which allows
you to access the mailing lists as though they were newsgroups.  This
offers some convenience to those who wish to browse the history or casually
observe the current threads.


2.4. Where can I get the Vim user manual in HTML/PDF/PS format?

You can download the HTML/PDF/PS format of the Vim user manual from:

    http://vimdoc.sourceforge.net/


2.5. I have a "xyz" (some) problem with Vim. How do I determine it is a
     problem with my setup or with Vim?

First, you have to determine that the problem is not with your .vimrc or
.gvimrc or system vimrc or your personal plugin files or in any of your
setup files. To do this, use

    $ vim -N -u NONE -U NONE

This will start Vim in 'nocompatible" mode and will not source your
personal .vimrc and .gvimrc files. It will also not load your personal
plugins. In this invocation of Vim, try to reproduce your problem. If you
are not able to reproduce the problem, then the problem is related to some
setting in one of your local setup files or plugins. To locate the problem
in your setup files, you have to use trial and error and try commenting out
the lines in your setup files one by one. You can also use the -V command
line argument to Vim to get more debug information and analyze the problem:

    $ vim -V2

You can increase the value passed to the -V argument to get more debug
information.

For more information, read

    :help -u
    :help -U
    :help -N
    :help -V
    :help 'verbose'
    :help :verbose
    :help set-verbose


2.6. Where can I report bugs?

First collect the required information using the following command:

    :source $VIMRUNTIME/bugreport.vim

Now send the resulting text from the above command to the bugs@vim.org
e-mail address.

The Vim Development mailing list (see above) is a good place to discuss
general bugs.  If the bug you find is with syntax highlighting or some
other "added feature" (i.e. not directly programmed into vim), attempt to
inform the maintainer of that feature.

For more information, read

    :help bug-reports


2.7. Where can the FAQ be found?

The FAQ can be found at VimOnline (vim.sf.net).  Other places will be
decided in the future.


2.8. What if I don't find an answer in this FAQ?

This FAQ covers mainly Vim-specific questions. You may find more
information suitable for most Vi clones by reading the Vi FAQ. It is posted
regularly on comp.editors. You can also find a copy at

    http://www.faqs.org/faqs/editor-faq/vi/part1/index.html

Also, since Vim has gathered so many features in the last few years,
successfully documenting the frequently asked questions here is a
near-impossible task.  To make it possible, please email the maintainer if
you have a good question.  A good question is one that you've tried to
answer yourself (remember, Vim has great documentation) but struggled.


2.9. I have a patch for implementing a Vim feature. Where can I send this
     patch?

You can send your patches to the Vim developer mailing list
vim-dev@vim.org.

For more information, read

    :help vim-dev


2.10. I have a Vim tip or developed a new Vim
      syntax/indent/filetype/compiler plugin or developed a new script or a
      colorscheme. Is there a public website where I can upload this?

Yes. You can use the Vim Online website to upload your plugins/scripts,
colorschemes, tips, etc. The site is at http://vim.sourceforge.net


=============================================================================

SECTION 3 - AVAILABILITY


3.1. What is the latest version of Vim?

The latest version of Vim is 6.2 released on 1st June 2003.

A history of previously released versions of Vim is below:

Version 6.1     24th March 2002
Version 6.0     27th September, 2001
Version 5.8     31st May, 2001
Version 5.7     24th June, 2000
Version 5.6     16th January, 2000
Version 5.5     21st September, 1999
Version 5.4     26th July, 1999
Version 5.3     31st August, 1998
Version 5.2     24th August, 1998
Version 5.1     7th April, 1998
Version 5.0     19th February, 1998
Version 4.6     13th March,1997
Version 4.5     17th October, 1996
Version 4.2     5th July,1996
Version 4.0     21st May, 1996
Version 3.0     16th August, 1994
Version 2.0     21st December, 1993
Version 1.27    23rd April, 1993
Version 1.17    21st April, 1992


3.2. Where can I find the latest version of Vim?

You can download the sources for the latest version of Vim from the
VimOnline website. The URL for this site is
http://vim.sourceforge.net/download.php.


3.3. What platforms does it run on?

All Unix platforms.
All Windows platforms.
Amiga, Atari, BeOS, DOS, Macintosh, MachTen, OS/2, RiscOS, VMS.


3.4. Where can I download the latest version of the Vim runtime files?

You can download the latest version of the Vim runtime files (syntax files,
filetype plugins, compiler files, color schemes, documentation, indentation
files and keymaps) from the Vim ftp site from the
ftp://ftp.vim.org/pub/vim/runtime directory.


=============================================================================

SECTION 4 - HELP


4.1. How do I use the help files?

Help can be found for all functions of Vim. In order to use it, use the
":help" command.  This will bring you to the main help page. On that first
page, you will find explanations on how to move around. Basically, you move
around in the help pages the same way you would in a read-only document.
You can jump to specific subjects by using tags. This can be done in two
ways:

   * Use the "<Ctrl-]>" command while standing on the name of a command or
     option. This only works when the tag is a keyword. "<Ctrl-LeftMouse>"
     and "g<LeftMouse>" work just like "<Ctrl-]>".
   * use the ":tag <subject>" command. This works with all characters.

Use "<Ctrl-T>" to jump back to previous positions in the help files. Use
":q" to close the help window.

If you want to jump to a specific subject on the help pages, use ":help
{subject}". If you don't know what to look for, try ":help index" to get a
list of all available subjects. Use the standard search keys to locate the
information you want. You can abbreviate the ":help" command as ":h".

For more information, read

    :help online-help


4.2. How do I search for a keyword in the Vim help files?

You can press the CTRL-D key after typing the help keyword to get a list of
all the help keywords containing the supplied pattern. You can also use the
meta characters like *, \+, etc to specify the help search pattern:

    :help init<C-D>
    :help str*()<C-D>
    :help '*indent<C-D>

You can press the Tab key after typing a partial help keyword to expand to
the matching keyword. You can continue to press the Tab key to see other
keyword matches.

From the help window, you can use the ":tag" command to search for
keywords. For example,

    :tselect /window

This command will list all the help keywords containing the text "window".
You can select one from the list and jump to it.

You can use the ":helpgrep" command to search for the given text in all the
help files. The quickfix window will be opened with all the matching lines.

For more information, read

    :help c_CTRL-D
    :help c_<Tab>
    :help :tselect
    :help :help
    :help :helpgrep


4.3. I am getting an error message E123, what did I do wrong?

You can get more information about the error and the error message using:

    :help E123

For more information, read

    :help error-messages


4.4. Where can I read about the various modes in Vim?

You can get information about the different modes in Vim by reading

    :help vim-modes


4.5. How do I generate the Vim help tags file after adding a new Vim help
     file?

You can use the ":helptags" command to regenerate the Vim help tag file.
For example:

    :cd $VIMRUNTIME/doc
    :helptags .

For more information, read

    :help :helptags
    :help add-local-help


4.6. Can I use compressed versions of the help files?

Yes. You can compress the help files and still be able to view them with
Vim.  This makes accessing the help files a bit slower and requires the
"gzip" utility. Follow these steps to compress and use the Vim help files:

- Compress all the help files using "ggzip doc/*.txt".

- Edit the "doc/tags" file and change  the ".txt" to ".txt.gz" using
        :%s=\(\t.*\.txt\)\t=\1.gz\t=

- Add the following line to your vimrcc:
        set helpfile={dirname}/help.txt.gz

Where {dirname} is the directory where the help files are.  The gzip.vim
plugin supplied with the standard Vim distribution will take care of
decompressing the files.  You must make sure that $VIMRUNTIME is set to
where the other Vim files are, when they are not in the same location as
the compressed "doc" directory.

For more information, read

    :help gzip-helpfile
    :help 'helpfile'
    :help gzip
    :help $VIMRUNTIME


=============================================================================

SECTION 5 - EDITING A FILE


5.1. I am currently editing a file in Vim. How do I load another file into
     Vim?

There are several ways to load another file for editing. The simplest is to
use the ":e" command:

    :e <another file>

For more information, read

    :help usr_07
    :help edit-files


5.2. How do I save the current file in another name (save as) and edit a
     new file?

You can use the ":saveas" command to save the current file in another name:

    :saveas <newfilename>

Alternatively, you can also do:

    :w <newfilename>
    :edit #

You can also use the ":file" command, followed by ":w" command:

    :file <newfilename>
    :w

For more information, read

    :help :saveas
    :help :file_f
    :help :w


5.3. How do I change the current directory to the directory of the current
     file?

You can use the following command to change the current directory to the
directory of the current file:

    :cd %:p:h

To automatically change the current directory to the directory of the
current file, use the following autocmd:

    :autocmd BufEnter * cd %:p:h

For more information, read

    :help :cd
    :help :lcd
    :help filename-modifiers
    :help autocommand


5.4. How do I write a file without the line feed (EOL) at the end of the
     file?

You can turn off the 'eol' option and turn on the 'binary' option to write
a file without the EOL at the end of the file:

    :set binary
    :set noeol
    :w

For more information, read

    :help 'endofline'
    :help 'binary'
    :help 23.4


5.5. How do I configure Vim to open a file at the last edited location?

Vim stores the cursor position of the last edited location for each buffer
in the '"' register. You can use the following autocmd in your .vimrc or
.gvimrc file to open a file at the last edited location:

    au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") |
                         \ exe "normal g'\"" | endif

For more information, read

    :help '"
    :help last-position-jump


5.6. When editing a file in Vim, which is being changed by an external
     application, Vim opens a warning window (like the confirm dialog) each
     time a change is detected. How do I disable this warning?

You can set the 'autoread' option to configure Vim to automatically read
the file again when it is changed outside of Vim:

    :set autoread

You can also use the following autocommand:

    autocmd FileChangedShell *
          \ echohl WarningMsg |
          \ echo "File has been changed outside of vim." |
          \ echohl None

For more information, read

    :help 'autoread'
    :help FileChangedShell
    :help timestamp
    :help :checktime


5.7. How do I edit a file whose name is under the cursor?

You can use the gf command to edit a file whose name is under the cursor.
You can use the CTRL-W f command to edit the file in a new window.

For more information, read

    :help gf
    :help CTRL-W_f
    :help 'isfname'
    :help 'path'
    :help 'suffixesadd'
    :help 'includeexpr'


5.8. How do I reload/re-edit the current file?

You can use the ":edit" command, without specifying a file name, to reload
the current file.  If you have made modifications to the file, you can use
":edit!" to force the reload of the current file (you will lose your
modifications).

For more information, read

    :help :edit
    :help :edit!
    :help 'confirm'


5.9. How do I autosave a file periodically?

Vim does not support auto-saving a file periodically.

For more information, read

    :help 'updatetime'
    :help CursorHold
    :help swap-file


5.10. How do I open a file in read-only mode?

You can open a file in read-only mode using the ":view" command:

    :view <filename>

This command sets the 'readonly' option for the opened buffer. You can also
use the "-R" command-line option to open a file in read-only mode:

    $ vim -R <filename>

You can also use the symbolic link "view" to open a file in read-only mode:

    $ view <filename>

For more information, read

    :help 07.6
    :help 'readonly'
    :help 'modifiable'
    :help :view
    :help :sview
    :help view
    :help -R
    :help -M


=============================================================================

SECTION 6 - EDITING MULTIPLE FILES


6.1. How do I switch between files while editing multiple files in Vim?

There are several ways to switch between multiple files. You can use the
":buffer" command to switch between multiple files. For example,

    :buffer file1
    :buffer file2

You can also use the CTRL-^ key to switch between buffers. By specifying a
count before pressing the key, you can edit the buffer with that number.
Without the count, you can edit the alternate buffer by pressing CTRL-^

You can also use the ":e #" command to edit a particular buffer:

    :e #5

For more information, read

    :help edit-files
    :help :buffer
    :help CTRL-^
    :help alternate-file
    :help 22.4
    :help 07.3


6.2. How do I configure Vim to autoload several files at once similar to
     "work-sets" or "projects"?

You can use the ":mksession" and ":mkview" commands to achieve this. The
mksession command creates a Vim script that restores the current editing
session. You can use the ":source" command to source the file produced by
the mksession command. The mkview command creates a Vim script that
restores the contents of the current window. You can use the ":loadview"
command to load the view for the current file.

For more information, read

    :help 21.4
    :help 21.5
    :help views-sessions
    :help 'sessionoptions'
    :help :mksession
    :help :source
    :help v:this_session
    :help :mkview
    :help :loadview
    :help 'viewdir'
    :help buffers


6.3. Is it possible to open multiple top level windows in a single instance
     of Vim similar to Nedit or emacs?

No. It is currently not possible to open multiple top-level windows in a
single instance of Vim. This feature is in the todo list.


6.4. How do I open multiple files at once from within Vim?

There are several ways to open multiple files at once from within Vim. You
can use the ":next" command to specify a group of files:

    :next f1.txt f2.txt
    :next *.c

You can use the :args command to specify a group of files as argument:

    :args f1.txt f2.txt
    :args *.c

After loading the files, you can use the ":next" and ":prev" command to
switch between the files.

For more information, read

    :help 07.2
    :help :next
    :help :args_f
    :help argument-list


6.5. How do I open several windows in Vim by supplying command line
     arguments?

You can use the -o and -O Vim command line arguments to open multiple
horizontally or vertically split windows. For example:

    $ vim -o2
    $ vim -O2

For more information, read

    :help -o
    :help -O
    :help startup-options


6.6. How do I browse/explore directories from within Vim?

You can use the explorer.vim plugin, supplied with the standard Vim
package, to browse/explore directories from within Vim. You can start the
file explorer using one of the following commands:

    :e <directory>
    :Explore
    :SExplore

From the file explorer, you can browse through directories, rename, delete
and edit files.

For more information, read

    :help file-explorer
    :help 22.1


6.7. How do I edit files over a network using ftp/scp/rcp/http?

You can use the netrw.vim plugin, supplied with the standard Vim package,
to edit files over a network using ftp/scp/rcp/http. Using this plugin, Vim
will transparently load and save the files over ftp/scp/rcp/http. For
example, to edit a file over ftp, you can use the following command:

    $ vim ftp://machine/path

For more information, read

    :help netrw.vim


=============================================================================

SECTION 7 - BACKUP


7.1. When I edit and save files, Vim creates a file with the same name as
     the original file and a "~" character at the end. How do I stop Vim
     from creating this file? (or) How do I disable the Vim backup file
     feature?

You have set the 'backup' option, so Vim creates a backup file when saving
the original file. You can stop Vim from creating the backup file, by
clearing the option:

    :set nobackup

Note that, by default this option is turned off. You have explicitly
enabled the 'backup' option in one of the initialization files. You may
also have to turn off the 'writebackup' option:

    :set nowritebackup

For more information, read

    :help 07.4
    :help backup-table
    :help 'backup'
    :help 'writebackup'
    :help 'backupskip'
    :help 'backupdir'
    :help 'backupext'
    :help 'backupcopy'
    :help backup


7.2. How do I configure Vim to store all the backup files in a particular
     directory?

You can configure Vim to store all the backup files in a particular
directory using the 'backupdir' option. For example, to store all the
backup files in the ~/backup directory, you can use the following command:

    :set backupdir=~/backup

For more information, read

    :help 07.4
    :help 'backupdir'
    :help backup


7.3. When I save a file with Vim, the file permissions are changed.
     How do I configure Vim to save a file without changing the file
     permissions?

This may happen, if the 'backupcopy' option is set to 'no' or 'auto'. Note
that the default value for this option is set in such a way that this will
correctly work in most of the cases. If the default doesn't work for you,
try setting the 'backupcopy' option to 'yes' to keep the file permission
when saving a file:

    :set backupcopy=yes

This applies, only if you have configured Vim to make a backup whenever
overwriting a file. By default, Vim will not backup files.

For more information, read

    :help 'backupcopy'
    :help backup
    :help 'backup'
    :help 'writebackup'


=============================================================================

SECTION 8 - BUFFERS


8.1. I have made some modifications to a buffer. How do I edit another
     buffer without saving the modified buffer and also without losing the
     modifications?

You can set the 'hidden' option to edit a file without losing modifications
to the current file:

    :set hidden

By setting the 'hidden' option, you can also save the modification history
(undo-history) for the buffer. Otherwise, as you switch between files, the
undo-history will be lost.

For more information, read

    :help 'hidden'
    :help hidden-quit
    :help :hide


8.2. How do I configure Vim to auto-save a modified buffer when switching
     to another buffer?

You can set the 'autowrite' option to auto-save a modified buffer when
switching to another buffer:

    :set autowrite

For more information, read

    :help 'autowrite'
    :help 'autowriteall'
    :help 'hidden'


8.3. How do I replace the buffer in the current window with a blank buffer?

You can use the ":enew" command to load an empty buffer in place of the
buffer in the current window.

For more information, read

    :help :enew


8.4. Is there a keyboard shortcut to load a buffer by the buffer number?

You can use the CTRL-^ command to load a buffer by specifying the buffer
number. For example, to load buffer number 5, you have to use the 5 CTRL-^
command.

For more information, read

    :help CTRL-^


8.5. How do I open all the current buffers in separate windows?

You can use the ":ball" or ":sball" commands to open all the buffers
in the buffer list:

    :ball

For more information, read

    :help :ball


8.6. How do I close (delete) a buffer without exiting Vim?

You can use the ":bdelete" command to delete a buffer without exiting Vim.
For example:

    :bdelete file1

For more information, read

    :help :bdelete
    :help :bwipeout


8.7. I have several buffers opened with :e filename. How do I close one of
     the buffers without exiting Vim?

You can use the ":bdelete <buffername>" command to close the buffer.

For more information, read

    :help :bdelelete
    :help :bunload
    :help :bwipeout


8.8. When I use the command ":%bd" to delete all the buffers, not all the
     buffers are deleted. Why?

In the ":%bd" command, the '%' range will be replaced with the starting and
ending line numbers in the current buffer. Instead of using '%' as the
range, you should specify numbers for the range. For example, to delete all
the buffers, you can use the command ":1,9999bd".

For more information, read

    :help :bd


8.9. How do I display the buffer number of the current buffer/file?

You can use 2<CTRL-G> command to display the buffer number for the current
file/buffer. Note the use of count before the CTRL-G command. If the count
is greater than 1, then Vim will display the buffer number.

You can also use the following command to display the current buffer
number:

    :echo bufnr("%")

You can also include the "%n" field to the 'statusline' option to display
the current buffer number on the statusline.

For more information read,

    :help CTRL-G
    :help bufnr()
    :help :echo
    :help 'statusline'


8.10. How do I delete a buffer without closing the window in which the
      buffer is displayed?

You can use the following command to open the next buffer and delete
the current buffer.

    :bnext | bdelete #

For more information read,

    :help :bnext
    :help :bdelete
    :help :buffers


8.11. How do I map the tab key to cycle through and open all the buffers?

You can use the following two map commands, to map the CTRL-Tab key to open
the next buffer and the CTRL-SHIFT-Tab key to open the previous buffer:

    :nnoremap <C-Tab> :bnext<CR>
    :nnoremap <S-C-Tab> :bprevious<CR>

For more information read,

    :help :bnext
    :help :previous


=============================================================================

SECTION 9 - WINDOWS


9.1. What is the difference between a Vim window and a buffer?

A Vim buffer is a file loaded into memory for editing. The original file
remains unchanged until you write the buffer to the file. A Vim window is a
viewport onto a buffer. You can use multiple windows on one buffer or
several windows on different buffers.

For more information, read

    :help usr_08.txt
    :help 22.4
    :help windows-intro
    :help Q_wi


9.2. How do I increase the width of a Vim window?

You can increase the width of a Vim window using one of the following
commands:

    :vert resize +N
    :vert resize -N
    :vert resize N

You can also use CTRL-W < or CTRL-W > or CTRL-W | commands.

For more information, read

    :help vertical-resize
    :help CTRL-W_
    :help CTRL-W_<
    :help window-resize


9.3. How do I zoom into or out of a window?

You can zoom into a window (close all the windows except the current
window) using the "CTRL-W o" command or the ":only" ex command.

You can use the "CTRL-W _" command or the ":resize" ex command to increase
the current window height to the highest possible without closing other
windows.

You can use the "CTRL-W |" command or the ":vertical resize" ex command to
increase the current window width to the highest possible without closing
other windows.

You can use the "CTRL-W =" command to make the height and width of all the
windows equal.

You can also set the following options to get better results with the above
commands:

Method 1:
Set the 'winminheight' option to 0:

    :set winminheight=0

By default, this option is set to 1.
This option controls the minimum height of an inactive window (when it is
not the current window).  When the 'winminheight' option is set to 0, only
the status line will be displayed for inactive windows.

Method 2:
Set the 'noequalalways' option and set the 'winheight' option to a large
value (like 99999):

    :set noequalalways
    :set winheight=99999

Now, the active window will always open to its maximum size, while the
other windows will stay present, but shrunken to just a status line.

With any of the above mentioned methods, you cannot restore the window
layout after zooming into a window.  If you want to restore the Vim window
layout after zooming into a window, you can use the ZoomWin plugin. You can
download this plugin from the Vim online website at:

http://vim.sourceforge.net/scripts/script.php?script_id=508

For more information, read

    :help CTRL-W_o
    :help window-resize
    :help 'winminheight'
    :help 'equalalways'
    :help 'winheight'
    :help 08.3


9.4. How do I execute an ex command on all the open buffers or open windows
     or all the files in the argument list?

You can use the ":bufdo" command to execute an ex command on all the open
buffers.  You can use the ":windo" command to execute an ex command on all
the open windows.  You can use the ":argdo" command to execute an ex
command on all the files specified in the argument list.

For more information, read

    :help :windo
    :help :bufdo
    :help :argdo
    :help 26.3


=============================================================================

SECTION 10 - MOTION


10.1. How do I jump to the beginning (first line) or end (last line) of a
     file?

You can use 'G' command to jump to the last line in the file and the 'gg'
command to jump to the first line in the file.

For more information, read

    :help G
    :help gg


10.2. In insert mode, when I press the <Esc> key to go to command mode, the
     cursor moves one character to the left (except when the cursor is on
     the first character of the line). Is it possible to change this
     behavior to keep the cursor at the same column?

No. It is not possible to change this behavior. The cursor is *always*
positioned on a valid character (unless you have virtual-edit mode
enabled). So, if you are appending text to the end of a line, when you
return to command mode the cursor *must* drop back onto the last character
you typed. For consistency sake, the cursor drops back everywhere, even if
you are in the middle of a line.

You can use the CTRL-O command in insert mode to execute a single ex
command and return back to insert mode without moving the cursor column.

For more information, read

    :help 'virtual'
    :help i_CTRL-O


10.3. How do I configure Vim to maintain the horizontal cursor position when
     scrolling with the <Page Up>, <Page Down>, etc keys?

You can reset the 'startofline' option to keep the cursor at the same
horizontal location when scrolling text:

    :set nostartofline

For more information, read

    :help 'startofline'


10.4. Some lines in a file are more than the screen width and they are all
     wrapped. When I use the j, k keys to move from one line to the next,
     the cursor is moved to the next line in the file instead of the next
     line on the screen. How do I move from one screen line to the next?

You can use the gj and gk commands to move from one screen line to the
next/previous screen line. The j and k commands move the cursor from one
file line to the next file line. You can also avoid the line wrapping by
resetting the 'wrap' option:

    :set nowrap

For more information, read

    :help gj
    :help gk
    :help 'wrap'

You can use the following mappings:

    :map <Up> gk
    :imap <Up> <C-o>gk
    :map <Down> gj
    :imap <Down> <C-o>gj


10.5. What is the definition of a sentence, paragraph and section in Vim?

A sentence is defined as ending at a '.', '!' or '?' followed by either the
end of a line, or by a space (or two) or tab. Which characters and the
number of spaces needed to constitute a sentence ending is determined by
the 'joinspaces' and 'cpoptions' options.

A paragraph begins after each empty line, and also at each of a set of
paragraph macros, specified by the pairs of characters in the 'paragraphs'
option.

A section begins after a form-feed (<C-L>) in the first column and at each
of a set of section macros, specified by the pairs of characters in the
'sections' option.

For more information, read

    :help sentence
    :help 'joinspaces'
    :help 'cpoptions' | /^\s*j\
    :help paragraph
    :help section
    :help word


10.6. How do I jump to beginning or end of a sentence, paragraph or a
     section?

You can use the following motion commands to jump to the beginning or end
of a sentence or a paragraph or a section:

      motion  position    where
      (       beginning   current sentence
      )       end         current sentence
      {       beginning   current paragraph
      }       end         current paragraph
      []      end         previous section
      [[      beginning   current section
      ][      end         current section
      ]]      beginning   next section

Each of these motions can be preceded by a number which will extend the
jump forward (or backward).

For more information, read

    :help object-motions


10.7. I have lines in a file that extends beyond the right extent of the
     screen. How do I move the Vim view to the right to see the text off
     the screen?

You can use one of the following commands to horizontally scroll the screen
to the left or right:

    zl - scroll to the left
    zh - scroll to the right
    zL - scroll half a screenwidth to the left
    zH - scroll half a screenwidth to the right
    zs - scroll to position the cursor at the start of the screen
    ze - scroll to position the cursor at the end of the screen

You can use the g0 command to move the cursor to the first character of the
screen line and the g$ command to move the cursor to the last character of
the screen line without scrolling the screen.

For more information, read

    :help scroll-horizontal


10.8. How do I scroll two or more buffers simultaneously?

You can set the "scrollbind" option for each of the buffer to scroll them
simultaneously.

For more information, read

    :help 'scrollbind'
    :help scroll-binding
    :help 'scrollopt'


10.9. When I use my arrow keys, Vim changes modes, inserts weird characters
     in my document but doesn't move the cursor properly. What's going on?

There are a couple of things that could be going on: either you are using
Vim over a slow connection or Vim doesn't understand the key sequence that
your keyboard is generating.

If you are working over a slow connection (such as a 2400 bps modem), you
can try to set the 'timeout' or 'ttimeout' option. These options, combined
with the 'timeoutlen' and 'ttimeoutlen' options, may fix the problem.

The preceding procedure will not work correctly if your terminal sends key
codes that Vim does not understand. In this situation, your best option is
to map your key sequence to a matching cursor movement command and save
these mappings in a file. You can then ":source" the file whenever you work
from that terminal.

For more information, read

    :help 'timeout'
    :help 'ttimeout'
    :help 'timeoutlen'
    :help 'ttimeoutlen'
    :help :map
    :help vt100-cursor-keys


10.10. How do I configure Vim to move the cursor to the end of the previous
      line, when the left arrow key is pressed and the cursor is currently
      at the beginning of a line?

You can add the '<' flag to the 'whichwrap' option to configure Vim to move
the cursor to the end of the previous line, when the left arrow key is
pressed and the cursor is currently at the beginning of a line:

    :set whichwrap+=<

Similarly, to move the cursor the beginning of the next line, when the
right arrow key is  pressed and the cursor is currently at the end of a
line, add the '>' flag to the 'whichwrap' option:

    :set whichwrap+=

The above will work only in normal and visual modes. To use this in insert
and replace modes, add the '[' and ']' flags respectively.

For more information, read

    :help 'whichwrap'
    :help 05.7


10.11. How do I configure Vim to stay only in insert mode (modeless
      editing)?

You can set the 'insertmode' option to configure Vim to stay only in insert
mode:

    :set insertmode

By setting this option, you can use Vim as a modeless editor. If you press
the <Esc> key, Vim will not go to the normal mode. To execute a single
normal mode command, you can press CTRL-O followed by the normal mode
command.  To execute more than one normal command, you can use CTRL-L
followed by the commands. To return to insert mode, press the <Esc> key. To
disable this option, reset the 'insertmode' option:

    :set noinsertmode

You can also start vim using the "evim" command or you can use "vim -y" to
use Vim as a modeless editor.

For more information, read

    :help 'insertmode'
    :help i_CTRL-O
    :help i_CTRL-L
    :help evim
    :help evim-keys


10.12. How do I display some context lines when scrolling text?

You can set the 'scrolloff' option to display a minimal number of screen
lines (context) above and below the cursor.

    :set scrolloff=10

For more information, read

    :help 'scrolloff'
    :help 'sidescrolloff'


10.13. How do I go back to previous cursor locations?

You can go back to the cursor location before the latest jump using the ''
or `` command. You can use the CTRL-O command to go back to older cursor
positions and the CTRL-I command to go to the newer cursor positions in the
jump list.

For more information, read

    :help 03.10
    :help mark-motions
    :help jump-motions


=============================================================================

SECTION 11 - SEARCHING TEXT


11.1. After I searched for a text with a pattern, all the matched text
      stays highlighted. How do I turn off the highlighting
      temporarily/permanently?

The 'hlsearch' option controls whether all the matches for the last
searched pattern are highlighted or not. By default, this option is not
enabled. If this option is set in a system-wide vimrc file, then you can
turn off the search highlighting by using the following command:

    :set nohlsearch

To temporarily turn off the search highlighting, use

    :nohlsearch

You can also clear the search highlighting, by searching for a pattern that
is not in the current file (for example, search for the pattern 'asdf').

For more information, read

    :help 'hlsearch'
    :help :nohlsearch


11.2. How do I enter a carriage return character in a search pattern?

You can either use '\r' or <CTRL-V><CTRL-M> to enter a carriage return
character in a pattern. In Vim scripts, it is better to use '\r' for the
carriage return character.

For more information, read

    :help sub-replace-special


11.3. How do I search for the character ^M?

You can enter the ^M character in a search command by first pressing the
CTRL-V key and then pressing the CTRL-M key.

    /^V^M

You can also use the "\r" character. In Vim scripts, "\r" is preferred.

For more information, read

    :help c_CTRL-V
    :help using_CTRL-V
    :help /\r


11.4. How can I search/replace characters that display as '~R', '~S', etc.?

You can use the 'ga' command to display the ASCII value/code for the
special character. For example, let us say the ASCII value is 142. Then you
can use the following command to search for the special character:

    /^V142

where, ^V is entered by pressing CTRL-V.

For more information, read

    :help ga
    :help using_CTRL_V
    :help 24.8


11.5. How do I highlight all the non-printable characters in a file?

You can use the following commands and search pattern to highlight all the
non-printable characters in a file:

    :set hlsearch
    /\(\p\|$\)\@!.

For more information, read

    :help /\p
    :help /bar
    :help /$
    :help /\(
    :help /\@!
    :help 'hlsearch'


11.6. How do I search for whole words in a file?

You can search for whole words in a file using the \< and \> atoms. For
example:

    /\<myword\>

The \< atom matches the beginning of the word and the \> atom matches the
end of the word.

For more information, read

    :help /\<
    :help /\


11.7. How do I search for the current word under the cursor?

You can press the * key to search forward for the current word under the
cursor.  To search backward, you can press the # key. Note that only whole
keywords will be searched using these commands.

For more information, read

    :help star
    :help #
    :help g*
    :help g#
    :help 03.8
    :help search-commands


11.8. How do I search for a word without regard to the case (uppercase or
      lowercase)?

To always ignore case while searching for a pattern, set the 'ignorecase'
option:

    :set ignorecase

To ignore case only when searching a particular pattern, use the special \c
directive:

    /\c<pattern>

For more information, read

    :help 'ignorecase'
    :help /ignorecase
    :help /\c


11.9. How do I search for words that occur twice consecutively?

You can use one of the following search commands to locate words that occur
twice consecutively:

     /\(\<\w\+\)\_s\+\1\>
     /\(\<\k\+\)\_s\+\1\>

The main difference is the use of '\w' and '\k', where the latter is based
on the 'iskeyword' option which may include accented and other language
specific characters.

For more information, read

    :help /\1
    :help /\(
    :help /\)
    :help /\<
    :help /\
    :help /\w
    :help /\k
    :help /\+
    :help /\_x
    :help 'iskeyword'


11.10. How do I count the number of times a particular word occurs in a
       buffer?

You can use the following set of commands to count the number of times a
particular word occurs in a buffer:

    :let cnt=0
    :g/\<your_word\>/let cnt=cnt+1
    :echo cnt

This only counts the number of lines where the word occurs. You can also
use the following command:

    :%s/\<word\>/&/g

To count the number of alphabetic words in a file, you can use

    :%s/\a\+/&/g

To count the number of words made up of non-space characters, you can use

    :%s/\S\+/&/g

For more information, read

    :help count-items
    :help word-count
    :help v_g_CTRL-G
    :help 12.5


11.11. How do I place the cursor at the end of the matched word when
       searching for a pattern?

You can use the 'e' offset to the search command to place the cursor at the
end of the matched word. For example

    /mypattern/e

For more information about search offsets, read

    :help search-offset
    :help /


11.12. How do I search for an empty line?

You can search for an empty line using:

    /^$

    or

    /^\s*$

For more information, read

    :help /^
    :help /$
    :help /\s
    :help /*
    :help search-commands


11.13. How do I search for a line containing only a single character?

You can search for a line containing only a single character using:

    /^\s*\a\s*$

For more information, read

    :help /^
    :help /\a
    :help /\s
    :help /*
    :help /$


11.14. How do I search and replace a string in multiple files?

You can use the 'argdo' or 'bufdo' or 'windo' commands to execute an ex
command on multiple files. For example:

    :argdo %s/foo/bar/g

For more information, read

    :help :argdo
    :help :bufdo
    :help :windo


11.15. I am using the ":s" substitute command in a mapping. When a search
       for a pattern fails, the map terminates. I would like the map to
       continue processing the next command, even if the substitute command
       fails. How do I do this?

You can use the 'e' flag to the substitute command to continue processing
other commands in a map, when a pattern is not found.

For more information, read

    :help :s_flags


11.16. How do I search for the n-th occurrence of a character in a line?

To search for the n-th occurrence of a character in a line, you can prefix
the 'f' command with a number. For example, to search for the 5th
occurrence of the character @ in a line, you can use the command 5f@. This
assumes the cursor is at the beginning of the line - and that this first
character is not the one your are looking for.

For more information, read

    :help f
    :help F
    :help t
    :help T
    :help ;
    :help ,


11.17. How do I replace a tab (or any other character) with a hard return
       (newline) character?

You can replace a tab (or any other character) with a hard return (newline)
character using the following command:

    :s/\t/\r/

Note that in the above command, if you use \n instead of \r, then the tab
characters will not be replaced by a new-line character.

For more information, read

    :help sub-replace-special
    :help NL-used-for-Nul
    :help CR-used-for-NL


11.18. How do I search for a character by its ASCII value?

You can search for a character by its ASCII value by pressing CTRL-V
followed by the decimal or hexadecimal or octal value of that character in
the search "/" command.  To determine the ASCII value of a character you
can use the ":ascii" or the "ga" command.

For more information, read

    :help i_CTRL-V_digit
    :help :ascii
    :help ga


11.19. How do I search for long lines?

You can search for long lines or lines containing more than a specific
number of characters using the Vim regular-expressions in the search
command. For example, to search for all the lines containing more than 80
characters, you can use one of the following commands:

    /^.\{80}.*$
    /^.*\%80c.*$

For more information, read

    :help /\{
    :help /\%c


11.20. How do I display all the lines in the current buffer that contain a
       specified pattern?

You can use the following command to display all the lines in the current
buffer that contain a specified pattern:

    :g/<pattern>/p

For example, the following command will display all the lines in the
current buffer that contain "vim":

    :g/vim/p

If you also want the corresponding line numbers, then you can use the
following command:

    :g/<pattern>/#

For more information, read

    :help :global
    :help :print
    :help :number


11.21. How do I search for a text string that spans multiple lines?

You can search for a text string that spans multiple lines using the \_x
regular expression atom. For example, to search for the text string "Hello
World", you can use the following search command:

    /Hello\_sWorld

This will match the word "Hello" followed by a newline character and then
the word "World" at the beginning of the next line. This will also match
the word "Hello" immediately followed by a space character and then the
word "World". When searching for the "Hello World" string, to include the
space characters at the end and beginning of the line, you can use the
following search command:

    /Hello\_s\+World

For more information, read

    :help 27.8
    :help pattern-atoms
    :help /\_
    :help pattern-searches


=============================================================================

SECTION 12 - CHANGING TEXT


12.1. How do I delete all the trailing white space characters (SPACE and
      TAB) at the end of all the lines in a file?

You can use the ":substitute" command on the entire file to search and
remove all the trailing white space characters:

    :%s/\s\+$//

For more information, read

    :help :%
    :help :s
    :help /\s
    :help /\+
    :help /$


12.2. How do I replace all the occurrences of multiple consecutive space
      characters to a single space?

You can use the following command to replace all the occurrences of
multiple consecutive space characters to a single space:

    :%s/ \{2,}/ /g

For more information, read

    :help :%
    :help :s
    :help /\{
    :help :s_flags


12.3. How do I reduce a range of empty lines into one line only?

You can use the following command to reduce a range of empty lines into one
line only:

    :v/./.,/./-1join

The explanation for this command is below:

    :v/./           Execute the following command for all lines not
                    containing a character (empty lines).
         .,         Use the current line as the start of the range of
                    lines.
           /./      Use the line containing a character as the last line.
              -1    Adjust the range of lines to end with the line before
                    the last line.
                j   Join the lines in the range.

Note that this will give an error message if the empty lines are at the end
of the file. To correct this, you have to add a temporary line at the end
of the file, execute the command and then remove the temporary line.

For more information, read

    :help :v
    :help :join
    :help cmdline-ranges
    :help collapse


12.4. How do I delete all blank lines in a file? How do I remove all the
      lines containing only space characters?

To remove all blank lines, use the following command:

    :g/^$/d

To remove all lines with only whitespace (spaces or tabs) in them, use the
following command:

    :g/^\s\+$/d

To remove all the lines with only whitespace, if anything, use the
following command:

    :g/^\s*$/d


12.5. How do I copy/yank the current word?

You can use the "yiw" (yank inner word without whitespace) command or the
"yaw" (yank a word with whitespace) command to copy/yank the current
word.

For more information, read

    :help 04.6
    :help 04.8
    :help iw
    :help yank
    :help text-objects
    :help objects


12.6. How do I yank text from one position to another position within a
      line, without yanking the entire line?

You can specify a motion command with the yank operator (y) to yank text
from one position to another position within a line. For example, to yank
from the current cursor position till the next letter x, use yfx or Fx or
tx or Tx. To yank till the nth column, use n|. To yank till the next
occurrence of a 'word', use /word. To do a yank till the nth column on
another line, first mark the position using the 'ma' command, go to the
start of the yank position, and then yank till the mark using y`a (note the
direction of the quote)

For more information, read

    :help yank
    :help motion.txt
    :help 4.6


12.7. When I yank some text into a register, how do I append the text to
      the current contents of the register?

When you specify the register for some operation, if you use the upper-case
for the register name, then the new text will be appended to the existing
contents. For example, if you have some text in the register "a". If you
want to append some new text to this, you have to use the "A" register
name. If you use the lowercase register name, then the contents of the
register will be overwritten with the new text.

For more information, read

    :help quote
    :help quote_alpha
    :help 10.1


12.8. How do I yank a complete sentence that spans over more than one line?

To yank a complete sentence that spans over more than one line you have to
use the yank operator followed by a motion command. For example:

        y)

From inside the sentence you can use 'yi)' to yank the sentence.

For more information, read

    :help yank
    :help {motion}
    :help object-motions
    :help 4.6


12.9. How do I yank all the lines containing a pattern into a buffer?

You can use the ":global" command to yank all the lines containing the
pattern into a register and then paste the contents of the register into
the buffer:

    :let @a=''
    :g/mypattern/y A

The first command, clears the contents of the register "a". The second
command copies all the lines containing "mypattern" into the register "a".
Note that the capital letter "A" is used to append the matched lines. Now
you can paste the contents of register "a" to a buffer using "ap command.
For more information, read

    :help :g
    :help :y
    :help let-register
    :help quote_alpha
    :help put
    :help registers
    :help :registers


12.10. How do I delete all the lines in a file that does not contain a
       pattern?

You can use ":v" command to delete all the lines that does not contain a
pattern:

    :v/pattern/d

or

    :g!/pattern/d

For more information, read

    :help :v
    :help :g


12.11. How do I add a line before each line with "pattern" in it?

You can use the following command to add a line before each line with
"pattern" in it:

    :g/pattern/normal Oi<line of text goes here>

Alternatively you can yank the line using the Y command and then insert the
line using the following command:

    :g/pattern/put!

For more information, read

    :help :g
    :help :put
    :help insert
    :help 0


12.12. Is there a way to operate on a line if the previous line contains a
       particular pattern?

You can use the ":global" command to operate on a line, if the previous
line contains a particular pattern:

    :g/<pattern>/+{cmd}

For more information, read

    :help :g
    :help :range


12.13. How do I execute a command on all the lines containing a pattern?

You can use the ":global" (:g) command to execute a command on all the
lines containing a pattern.

    :g/my pattern/d

If you want to use a non-Ex command, then you can use the ":normal"
command:

    :g/my pattern/normal {command}

Unless you want the normal mode commands to be remapped, consider using a
":normal!" command instead (note the "!").

For more information, read

    :help :global
    :help :v
    :help :normal


12.14. Can I copy the character above the cursor to the current cursor
       position?

In Insert mode, you can copy the character above the cursor to the current
cursor position by typing <Ctrl-Y>. The same can be done with the
characters below the cursor by typing <Ctrl-E>.

For more information, read

    :help i_CTRL-Y
    :help i_CTRL-E


12.15. How do I insert a blank line above/below the current line without
       entering insert mode?

You can use the ":put" ex command to insert blank lines. For example, try

    :put =''
    :put! =''

For more information, read

    :help :put


12.16. How do I insert the name of current file into the current buffer?

There are several ways to insert the name of the current file into the
current buffer. In insert mode, you can use the <C-R>% or the
<C-R>=expand("%") command. In normal mode, you can use the ":put =@%"
command.

For more information, read

    :help i_CTRL-R
    :help expand()
    :help !!


12.17. How do I insert the contents of a Vim register into the current
       buffer?

In insert mode, you can use the <C-R><register> command to insert the
contents of <register>. For example, use <C-R>a to insert the contents
of register "a" into the current buffer.

In normal mode, you can use the ":put <register>" command to insert the
contents of <register>. For example, use the ":put d" command to insert
the contents of register "d" into the current buffer.

For more information, read

    :help i_CTRL-R
    :help :put


12.18. How do I move the cursor past the end of line and insert some
       characters at some columns after the end of the line?

You can set the "virtualedit" option to move the cursor past the
end-of-line and insert characters in a column after the end-of-line. To
start the virtual mode, use

    :set virtualedit=all

For more information, read

    :help 'virtualedit'


12.19. How to replace the word under the cursor (say: junk) with
      "foojunkbar" in Vim?

There are several ways to do this. If the word is the first such word on
the line, use the following command:

    :exe "s/".expand("<cword>")."/foo&bar/"

Too match specifically you could use a more complex substitution like this:

    :exe 's/\<'.expand("<cword>").'\%>'.(col(".")-1).'c\>/foo&bar/'

You can also use the command: ciwfoo<C-R>"bar<Esc>

For more information, read

    :help :substitute
    :help expand()
    :help col()
    :help /\%c


12.20. How do I replace a particular text in all the files in a directory?

You can use the "argdo" command to execute the substitute command on all
the files specified as arguments:

    :args *
    :argdo %s/<your_text>/<replacement_text>/ge | update

For more information, read

    :help :args_f
    :help :argdo
    :help :s_flags


12.21. I have some numbers in a file. How do I increment or decrement the
       numbers in the file?

You can use the CTRL-A key to increment the number and the CTRL-X key to
decrement the number. You can also specify the number to
increment/decrement from the number by specifying a count to the key. This
works for decimal, octal and hexadecimal numbers.

For more information, read

    :help CTRL-A
    :help CTRL-X
    :help 'nrformats'


12.22. How do I reuse the last used search pattern in a ":substitute"
       command?

To reuse the last used search pattern in a ":substitute" command, don't
specify a new search pattern:

    :s/pattern/newtext/
    :s//sometext/

In the second ":s" command, as a search pattern is not specified, the
pattern specified in the first ":s" command 'pattern' will be used.

If you want to change the search pattern but repeat the substitution
pattern you can use the special right hand side, you can use the tilde
character:

    :s/newpattern/~/

For more information, read

    :help :s
    :help :&
    :help :
    :help &
    :help sub-replace-special


12.23. How do I change the case of a string using the ":substitute"
       command?

You can use special characters in the replacement string for a
":substitute" command to change the case of the matched string. For
example, to change the case of the string "MyString" to all uppercase, you
can use the following command:

    :%s/MyString/\U&/g

To change the case to lowercase, you can use the following command:

    :%s/MyString/\L&/g

To change the case of the first character in all the words in the current
line to uppercase, you can use the following command:

    :s/\<\(.\)\(\k*\)\>/\u\1\L\2/g

For more information, read

    :help sub-replace-special
    :help :substitute
    :help \U
    :help \L
    :help \u


12.24. How do I enter characters that are not present in the keyboard?

You can use digraphs to enter characters that are not present in the
keyboard. You can use the ":digraphs" command to display all the currently
defined digraphs. You can add a new digraph to the list using the
":digraphs" command.

For more information, read

    :help digraphs
    :help 'digraphs'
    :help 24.9


12.25. Is there a command to remove any or all digraphs?

No. The digraphs table is defined at compile time. You can only add new
ones. Adding a command to remove digraphs is on the todo list.


12.26. In insert mode, when I press the backspace key, it erases only the
       characters entered in this instance of insert mode. How do I erase
       previously entered characters in insert mode using the backspace
       key?

You can set the 'backspace' option to erase previously entered characters
in insert mode:

    :set backspace=indent,eol,start

For more information, read

    :help 'backspace'


12.27. I have a file which has lines longer than 72 characters terminated
       with "+" and wrapped to the next line. How can I quickly join the
       lines?

You can use the ":global" command to search and join the lines:

    :g/+$/j

This will, however, only join every second line. A couple of more complex
examples which will join all consecutive lines with a "+" at the end are:

    :g/*$/,/\(^\|[^+]\)$/j
    :g/+$/mark a | .,/\(^\|[^+]\)$/s/+$// | 'a,.j

For more information, read

    :help :g
    :help :j
    :help :mark


12.28. How do I paste characterwise yanked text into separate lines?

You can use the ":put" command to paste characterwise yanked text into new
lines:

    :put =@"

For more information, read

    :help :put
    :help quote_=


12.29. How do I change the case (uppercase, lowercase) of a word or a
       character or a block of text?

You can use the "~" command to switch the case of a character.

You can change the case of the word under the cursor to uppercase using the
"gUiw" or "viwU" command and to lowercase using the "guiw" or "viwu"
command.

You can switch the case (upper case to lower case and vice versa) of the
word under the cursor using the "viw~" or "g~iw" command.

You can use the "gUgU" command to change the current line to uppercase and
the "gugu" command to change the current line to lowercase.

You can use the "g~g~" command to switch the case of the current line. You
can use the "g~{motion}" or "{Visual}~" commands to switch the case of a
block of text.

For more information, read

    :help case


12.30. How do I enter ASCII characters that are not present in the
       keyboard?

You can enter ASCII characters that are not present in the keyboard by
pressing CTRL-V and then the ASCII character number. You can also use
digraphs to enter special ASCII characters.

For more information, read

    :help i_CTRL-V_digit
    :help digraphs
    :help 45.5


12.31. How do I replace non-printable characters in a file?

To replace a non-printable character, you have to first determine the ASCII
value for the character. You can use the ":ascii" ex command or the "ga"
normal-mode command to display the ASCII value of the character under the
cursor.

You can enter the non-printable character by entering CTRL-V followed by
the decimal number 1-255 (with no leading zero), or by x and a hex number
00-FF, or by an octal number 0-0377 (with leading zero), or by u and a hex
number 0-FFFF, or by U and a hex number 0-7FFFFFFF

Another alternative is to use the ":digraphs" ex command to display the
digraphs for all characters, together with their value in decimal and
alpha. You can enter a non-printable character by entering CTRL-K followed
by two alphanumeric characters (a digraph).

For more information, read

    :help :ascii
    :help i_CTRL-V
    :help i_CTRL-V_digit
    :help :digraphs


12.32. How do I remove duplicate lines from a buffer?

You can use the following user-defined command to remove all the duplicate
lines from a buffer:

:command -range=% Uniq <line1>,<line2>g/^\%<<line2>l\(.*\)\n\1$/d

Add the above command to your .vimrc file and invoke ":Uniq" to remove all
the duplicate lines.


12.33. How do I prefix all the lines in a file with the corresponding line
       numbers?

You can prefix the lines with the corresponding line number in several
ways. Some of them are listed below:

    :%s/^/\=line('.'). ' '
    :%s/^/\=strpart(line(".")."     ", 0, 5)
    :g/^/exec "s/^/".strpart(line(".")."    ", 0, 4) 

For more information, read

    :help sub-replace-special
    :help line()
    :help expr6
    :help strpart()
    :help :execute
    :help :global


12.34. How do I exchange (swap) two characters or words or lines?

You can exchange two characters with the "xp" command sequence. The 'x'
will delete the character under the cursor and 'p' will paste the just
deleted character after the character under the cursor. This will result
in exchanging the two characters.

You can exchange two words with the "deep" command sequence (start with the
cursor in the blank space before the first word).

You can exchange two lines with the "ddp" command sequence. The 'dd' will
delete the current line and 'p' will paste the just deleted line after the
current line. This will result in exchanging the two lines. 

All of the above operations will change the " unnamed register.

You can use the ":m +" ex command to exchange two lines without changing the
unnamed register.

For more information, read

    :help x
    :help p
    :help dd
    :help d
    :help e
    :help linewise-register
    :help quotequote
    :help :move


=============================================================================

SECTION 13 - COMPLETION IN INSERT MODE


13.1. How do I complete words or lines in insert mode?

In insert mode, you can complete words using the CTRL-P and CTRL-N keys.
The CTRL-N command searches forward for the next matching keyword.
The CTRL-P command searches backwards for the next matching keyword.

In insert mode, you can use the CTRL-X CTRL-L command sequence to complete
lines that starts with the same characters as in the current line before
the cursor. To get the next matching line, press the CTRL-P or CTRL-N keys.
There are a lot of other keys/ways available to complete words in insert
mode.

Vim supports completion of the following items:

    CTRL-X CTRL-F    file names
    CTRL-X CTRL-L    whole lines
    CTRL-X CTRL-D    macro definitions (also in included files)
    CTRL-X CTRL-I    current and included files
    CTRL-X CTRL-K    words from a dictionary
    CTRL-X CTRL-T    words from a thesaurus
    CTRL-X CTRL-]    tags
    CTRL-X CTRL-V    Vim command line

For more information, read

    :help 24.3
    :help ins-completion


13.2. How do I complete file names in insert mode?

In insert mode, you can use the CTRL-X CTRL-F command sequence to complete
filenames that start with the same characters as in the current line before
the cursor.

For more information, read

    :help compl-filename


13.3. I am using CTRL-P/CTRL-N to complete words in insert mode. How do I
      complete words that occur after the just completed word?

You can use CTRL-X CTRL-N and CTRL-X CTRL-P keys to complete words that are
present after the just completed word.

For more information, read

    :help i_CTRL-X_CTRL-P
    :help i_CTRL-X_CTRL-N
    :help ins-completion


=============================================================================

SECTION 14 - TEXT FORMATTING


14.1. How do I format a text paragraph so that a new line is inserted at
      the end of each wrapped line?

You can use the 'gq' command to format a paragraph. This will format the
text according to the current 'textwidth' setting.

Note that the gq operator can be used with a motion command to operate on a
range of text. For example:

    gqgq - Format the current line
    gqap - Format current paragraph
    gq3j - Format the current and the next 3 lines

For more information, read

    :help gq
    :help formatting
    :help usr_25.txt
    :help motion.txt


14.2. How do I format long lines in a file so that each line contains less
      than 'n' characters?

First set the 'textwidth' option to the desired value:

    set textwidth=70

Now to break the long lines to the length defined by the 'textwidth'
option, use

    :g/./normal gqq

For more information, read

    :help gq


14.3. How do I join short lines to form a paragraph?

First, make sure the 'textwidth' option is set to a high value:

    :set textwidth=99999

Next, join the short lines to form a paragraph using the command:

    1GgqG

The above command will operate on the entire file. To do the formatting on
all paragraphs in a specific range, use:

    :'a,'bg/\S/normal gq}

For more information, read

    :help gq
    :help G
    :help gqq


14.4. How do I format bulleted and numbered lists?

You can configure Vim to format bulleted and numbered lists using the
'formatoptions' option. For example, you can format the list of the
following format:

  - this is a test.