This draft deletes the entire topic.
Examples
-
Run vimtutor as many times as needed to feel comfortable with the basics.
h
,j
,k
andl
correspond to the cursor keys←
,↓
,↑
and→
. They can be prefixed with a "count", e.g.3j
moves down 3 lines.Command Description i
(insert) enters insert mode before the current cursor position I
enters insert mode before the first printable character of the current line a
(append) enters insert mode after the current cursor position A
enters insert mode after the last printable character of the current line x
delete character at the current cursor position w
move to next word b
move to previous word 0
move to the beginning of line $
move to the end of line s
substitute – enters insert mode and text you type will replace the character at the current cursor position S
substitue entire line that the cursor is currently on <Esc>
,<C-c>
exit insert mode and returns to normal mode u
undo <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 ofd$
cc
,cw
,cl
change 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
,P
put ("paste") after, or before current position, respectively o
to create a new empty line, under the current one :w
write the current buffer to disk :q!
,ZQ
quit without writing :x
,:wq
,ZZ
write and quit qz
begin 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 -
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 ofquit
,w
, ofwrite
andx
, ofexit
(you can also type:quit
,:write
and:exit
if you want). Shortcuts not starting with:
such asZZ
andZQ
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!" -
-
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
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:
-
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. -
The Vim on your machine—if there is one—is very likely to be a "small" build that lacks useful features like clipboard support, syntax highlighting or even the ability to use plugins.
This is not a problem if all you need is a quick way to edit config files but you will soon hit a number of walls if you intend to make Vim your main editor.
It is therefore generally recommended to install a more complete build.
Installation on Linux/BSD
On those systems, the trick is simply to install the GUI version which comes with both a
gvim
command for starting the GUI and avim
command for starting the TUI.Arch and Arch-based distributions
$ sudo pacman -R vim $ sudo pacman -S gvim
Debian and Debian-based distributions
$ sudo apt-get update $ sudo apt-get install vim-gtk
Gentoo and Gentoo-based distributions
$ sudo emerge --sync $ sudo emerge app-editors/gvim
RedHat and RedHat-based distributions
$ sudo yum check-update $ sudo yum install vim-X11
Fedora
$ sudo dnf check-update $ sudo dns install vim-X11
Slackware and Slackware-based distributions
$ sudo slackpkg update $ sudo slackpkg install-new vim-gvim
OpenBSD and OpenBSD-based distributions
$ sudo pkg_add vim-x11
FreeBSD and FreeBSD-based distributions
$ sudo pkg install editors/vim
Installation on Mac OS X
The strategy is similar on Mac OS X: we install the GUI version to get both the GUI and the TUI. In the end, we should be able to:
- double-click the MacVim icon in the Finder,
- click on the MacVim icon in the Dock,
- issue
$ mvim
in the shell to open the MacVim GUI, - issue
$ mvim -v
in the shell to open the MacVim TUI.
Regular install
Download and install an official snapshot like you would with any other Mac OS X application.
Place the
mvim
script that comes bundled with MacVim somewhere in your$PATH
.Package manager
MacPorts:
Homebrew:$ sudo port selfupdate $ sudo port install macvim
$ brew install macvim
To make MacVim the default console Vim:
$ brew install macvim --with-override-system-vim
Installation on Windows
There is no Vim on Windows systems by default. You can download and install Vim from the Tuxproject site for more up-to-date and complete builds or you can download and install Vim from the official Vim site.
Chocolatey
> choco install vim
Building Vim from source
If the methods above don't suit your needs it is still possible to build Vim yourself, with only the options you need.
This topic will be discussed in its own section (currently in draft).
-
When using
vim
from the command line, you can suspendvim
and get back to your prompt, without actually quittingvim
. Hence you will later be able to get back yourvim
session from the same prompt.When in Normal mode (if not, press esc to get there), issue either of these commands:
:st
enter:sus
enter:stop
enter:suspend
enterAlternatively, on some systems, when in Normal or Visual mode, issuing Ctrl+Z will have the same effect.
Note: If
autowrite
is set, buffers with changes and filenames will be written out. Add a!
before enter to avoid, eg.:st!
enter.Later, when you want to return to your
vim
session, if you haven't suspended any other jobs, issuing the following will restore vim as your foreground job.fg
enterOtherwise you will need to find your
vim
sessions's job ID by issuingjobs
enter and then foregrounding the matching jobsfg %[job ID]
enter eg.fg %1
enter.
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
Topic Outline
- Exiting Vim
- Saving a read-only file edited in Vim
- Basics
- Built-in vim tutorial (vimtutor)
- Installation
- Suspending vim
Versions
Sign up or log in
Save edit as a guest
Join Stack Overflow
Using Google
Using Facebook
Using Email and Password
We recognize you from another Stack Exchange Network site!
Join and Save Draft