How To Do This In Vim?
I've compiled a list of commonly asked questions, or rather How To's for those who are only starting with Vim.
I tried to keep the answers short and to the point, while also providing useful links and resources for those who want to dive deeper (spoiler alert, that's usually
Ready? Let's go.
How to exit Vim?
Make sure you are in the
normal mode by pressing
Esc. Then depending on what is your situation, you have several options:
:qa!to close all files and quit abandoning the changes
:wqato save all files and quit
ZZin normal mode writes the current file and exits
:xsame as above
Learn more from
How to suspend (temporary exit) Vim to go back later
In a Unix-like system, you can suspend a process that means pausing it and sending it to the background. You can use it to switch between Vim and terminal.
Ctrl + Zto suspend Vim (or any other foreground process)
fgsends it back into the foreground
Making Git use Vim as editor
Type this in your shell
git config --global core.editor vim
Or, open the
~/.gitconfig file and modify that variable manually (to learn more refer to the Git documentation).
How to comment / uncomment multiple lines?
There's no good build-in way to comment/uncomment lines. I suggest using the tpope/vim-commentary plugin that supports a wide range of languages:
gcccomment/uncomment the current line
gccomment/uncomment the selection in
gcapcomment the entire paragraph (it supports the
How to rename a file?
- There is a command called
:saveaswhich saves the current file under a different name. But you will need to remove the old file yourself.
- You can use the built-in
netrwfile explorer (start with
:Explore, learn more with
- You can always run an external command
:!mv <oldname> <newname>
How to close all buffers except the current one
The nicest way is to run
%bdcloses all buffers
|a way to combine commands
e#opens the last buffer
How do I move to the end of the current line?
$to move to the end of a line in normal mode
append) to move to the end and start typing (go into the
Go to the first line in a file
ggto get to the very first line in a file
Gto the last line
How to clear the last search highlighting
- The command to clear the highlighting is
- You can map it to
Enter(which doesn't do anything useful anyway)
nnoremap <CR> :noh<CR>
How to search and replace
If you need to replace all the occurrences of "this" to "that" in the entire file, run
%sis the substitute command,
gflag that means change all the occurrences in the line instead of just the first,
%means that it should apply to all lines in that files.
How to make search ignore the case?
You can specify that explicitly while searching by adding a special flag:
Or you can set
set ic in the settings to make this a default behavior.
How to show the trailing whitespace characters?
You can always search for trailing whitespaces, with a regex
You can set Vim to show the trailing whitespace characters with
set list set listchars+=trail:<char>
<char> with any character you want.
- To always highlight trailing whitespaces you can create a match (
highlight TrailingWhitespace ctermbg=red guibg=red match TrailingWhitespace /\s\+$/
or even better (this will not highlight while typing - which is quite annoying):
match TrailingWhitespace /\s\+\%#\@<!$/
How can you auto-remove all trailing whitespace?
To remove the trailing whitespaces, you can use this replace pattern
%sruns the replace for all lines in the current buffer
\s\+$that's a regular expression meaning "one or more spaces before the end of line" (
//replacing with nothing
How to copy to the system clipboard
In Vim, there are multiple
:help registers) each of which can hold text. To make it work with the system register by default, set this in your configuration file.
set clipboard^=unnamedplus " Use the system register for everything
How to duplicate a line?
You can do this by typing
yywill copy the whole line into the
pwill paste the line after the current line (you can also type
Pto insert the line above the current one)
How to move (scroll) the screen without moving cursor?
Ctrl + ymoves the screen one line down
Ctrl + emoves the screen one line up
How to increase the split window size?
Ctrl + w,
>to make the current split wider
Ctrl + w,
<to make the current split narrower
How to indent multiple lines?
There's a special operator for indenting in Vim (
>indent the whole block to the right (visual mode)
<indent the whole block to the left (visual mode)
<<indent the current line (normal mode)
>ap(indent the whole paragraph, any text object works)
.to repeat it multiple times
How do I list the loaded plugins in Vim?
A plugin is just a set of scripts. You can list all the loaded scripts with this command:
:scriptnameslists the loaded scripts
Where to next?
If you're a Vim newbie, you might like some other articles from this website: