33% Off / An IDE on top of Vim? Easy with "Vim From Scratch" book

How To Do This In Vim?

6 months ago

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 :help).

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
  • :wqa to save all files and quit
  • ZZ in normal mode writes the current file and exits
  • :x same as above

Learn more from :help quit.

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 + Z to suspend Vim (or any other foreground process)
  • fg sends 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:

  • gcc comment/uncomment the current line
  • gc comment/uncomment the selection in Visual mode
  • gcap comment the entire paragraph (it supports the text objects (:help text-objects))

How to rename a file?

  • There is a command called :saveas which saves the current file under a different name. But you will need to remove the old file yourself.
  • You can use the built-in netrw file explorer (start with :Explore, learn more with :help netrw)
  • 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 :%bd|e#.

  • %bd closes 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
  • A (mnemonic append) to move to the end and start typing (go into the Insert mode)

Go to the first line in a file

  • gg to get to the very first line in a file
  • G to the last line

How to clear the last search highlighting

  • The command to clear the highlighting is :noh[lsearch].
  • 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 :%s/this/that/g.

  • %s is the substitute command,
  • g flag that means change all the occurrences in the line instead of just the first,
  • while % 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:

  • /text\c

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 /\s\+$.

  • You can set Vim to show the trailing whitespace characters with

set list
set listchars+=trail:<char>

Replace <char> with any character you want.

  • To always highlight trailing whitespaces you can create a match (:help 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 :%s/\s\+$//

  • %s runs 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 registers (: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 yyp.

  • yy will copy the whole line into the unnamed register
  • p will paste the line after the current line (you can also type P to insert the line above the current one)

How to move (scroll) the screen without moving cursor?

  • Ctrl + y moves the screen one line down
  • Ctrl + e moves 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 (:help >).

  • > 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:

  • :scriptnames lists the loaded scripts

Where to next?

If you're a Vim newbie, you might like some other articles from this website:

Vim From Scratch Book

Learn Vim and boost your productivity to the next level.

LEARN MORE