vim


This draft deletes the entire topic.

expand all collapse all

Examples

  • 21

    Run vimtutor as many times as needed to feel comfortable with the basics.

    h, j, k and l correspond to the cursor keys , , and . They can be prefixed with a "count", e.g. 3j moves down 3 lines.

    CommandDescription
    i(insert) enters insert mode before the current cursor position
    Ienters insert mode before the first printable character of the current line
    a(append) enters insert mode after the current cursor position
    Aenters insert mode after the last printable character of the current line
    xdelete character at the current cursor position
    wmove to next word
    bmove to previous word
    0move to the beginning of line
    $move to the end of line
    ssubstitute – enters insert mode and text you type will replace the character at the current cursor position
    Ssubstitue entire line that the cursor is currently on
    <Esc>, <C-c>exit insert mode and returns to normal mode
    uundo
    <C-r>redo
    dd, dw, dl, d$cut the current line, from the cursor to next word, or the character, current position to end of current line respectively, note: D is the equivalent of d$
    cc, cw, clchange the current line, from the cursor to next word, or the character, respectively
    yy, yw, yl, y$yank ("copy") the current line, from the cursor to next word, or the character, current position to end of current line respectively
    p, Pput ("paste") after, or before current position, respectively
    oto create a new empty line, under the current one
    :wwrite the current buffer to disk
    :q!, ZQquit without writing
    :x, :wq, ZZwrite and quit
    qzbegin recording actions to register z, q to end recording, @z to play back the actions. z can be any letter: q is often used for convenience. Read more: Macros
  • 71

    In order to exit Vim, first make sure you are in Normal mode by pressing Esc.

    • :q Enter (will prevent you from exiting if you have unsaved changes - short for :quit)

    To discard changes and exit Vim:

    • :q! Enter to force exit and discard changes (short for :quit!, not to be confused with :!q),
    • ZQ is a shortcut that does the same as :q!,
    • :cq Enter (quit and return error - helpful when using Vim with Git)

    To save changes and exit Vim:

    • :wq Enter (shorthand for :write and :quit),
    • :x Enter (same as :wq, but will not write if the file was not changed),
    • ZZ is a shortcut that does the same as :x (Save workspace and quit the editor),
    • :[range]wq! Enter (write the lines in [range])

    To close multiple buffers at once (even in multiple windows and/or tabs), append the letter a to any of the Commands above (the ones starting with :). For example, to write and quit all windows you can use:

    • :wqa Enter or
    • :xa Enter — Write all changed buffers and exit Vim. If there are buffers without a file name, which are readonly or which cannot be written for another reason, Vim will not quit
    • :xa! Enter — Write all changed buffers, even the ones that are readonly, and exit Vim. If there are buffers without a file name or which cannot be written for another reason, Vim will not quit
    • :qa Enter — try to quit, but stop if there are any unsaved files;
    • :qa! Enter — quit without saving (discard changes in any unsaved files)

    If you have opened Vim without specifying a file and you want to save that file before exiting, you will receive E32: No file name message. You can save your file and quit using:

    • :wq filename Enter or;
    • :x filename Enter

    Explanation:

    The : keystroke actually opens Command mode. The command q is an abbreviation of quit, w, of write and x, of exit (you can also type :quit, :write and :exit if you want). Shortcuts not starting with : such as ZZ and ZQ refer to Normal mode key mappings. You can think of them as shortcuts.

    The ! keystroke is sometimes used at the end of a command to force its execution, which allows to discard changes in the case of :q!. Placing the ! at the beginning of the command has a different meaning. For example, one can mistype :!q instead of :q! and vim would terminate with a 127 error.

    An easy way to remember this is to think of ! as a way of insisting on executing something. Just like when you write: "I want to quit!"

  • 29

    Sometimes, we may open a file which we do not have permission to write in Vim without using sudo.

    Use this command to save a read-only file edited in Vim.

    :w !sudo tee > /dev/null %
    

    Which you could map to :w!! in your .vimrc:

    cmap w!! w !sudo tee > /dev/null %
    

    You will be presented a prompt as shown in the image.

    Press ENTER or type command to continue. [O]K, (L)oad File:.

    Press O and the file will be saved. It remains open in vi/vim for more editing or reading and you can exit normally by typing :q! since the file is still open as read-only.

    Command Explanation

    :w ............................ isn't modifying your file in this case, 
       ............................ but sends the current buffer contents to 
       ............................ a substituted shell command
       !sudo ...................... call the shell 'sudo' command
             tee .................. the output of the vi/vim write command is redirected 
                                    using the 'tee' command
                 > /dev/null ...... throws away the standard output, since we don't need 
                                    to pass it to other commands
                             % .... expands to the path of the current file
    

    Sources:

  • 17

    vimtutor is an interactive tutorial covering the most basic aspects of text editing.

    On UNIX-like system, you can start the tutorial with:

    $ vimtutor
    

    On Windows, “Vim tutor” can be found in the “Vim 7.x” directory under “All Programs” in the Windows menu.

    See :help vimtutor for further details.

Please consider making a request to improve this example.

Remarks

Vim (or "Vi IMproved") is a console-based modal text editor. It is widely used and available by default on all Unix and Linux systems. Vim has a large active community and wide user base. The editor supports all popular programming languages, and many plugins are available to extend its features.

Developers like the editor for its speed, many configuration options, and powerful expression based editing. In "command" mode the editor is controlled by keyboard commands, so the user is not distracted by a GUI or mouse pointer.

Vim is based on the old Unix "vi" editor and it has been in continuous development since 1991. With macros and plugins the editor offers most of the features of a modern IDE. It is also uniquely capable of processing large amounts of text with its scripting language (vimscript) and regular expressions.

Main Topics (todo):

  • installation
  • editing modes
  • navigation
  • basic editing
  • advanced editing
  • configuration
  • plugins
  • vimscript
  • macros
  • community
  • related projects

Vim standard startup message

Versions

VersionRelease Date
8.02016-09-12
7.42013-08-10
7.32010-08-15
7.22008-08-09
7.12007-05-12
7.02006-05-07
6.02001-09-26
5.01998-02-19
4.01996-05-29
3.01994-08-12
2.01993-12-14
1.141991-11-02
Still have a question about Getting started with vim? Ask Question

Topic Outline