vim

Topics related to vim:

Getting started with vim

Inserting text

Movement

Macros

Split windows

When called from the command line, multiple files can be provided in the argument and vim will create one split for each file. When called from ex mode, only one file can be opened per invocation of the command.

Manipulating text

Modes - insert, normal, visual, ex

Configuring Vim

Buffers

Vim Options

Scrolling

Searching in the current buffer

Building from vim

Tips and tricks to boost productivity

This automatic reload will only happen if you edit your vimrc in a version full version of vim which supports autocmd.

Command-line ranges

Substitution

Use set gdefault to avoid having to specify the 'g' flag on every substitute.

Example

When gdefault is set, running :s/foo/bar on the line foo baz foo will yield bar baz bar instead of bar baz foo.

Find and Replace

Key Mappings in Vim

Spell checker

Extending Vim

A plugin is a script or set of scripts that changes Vim's default behavior, either by adding non-existing features or by extending existing features.

Often added "non-existing features" include:

  • commenting,
  • indentation detection,
  • autocompletion,
  • fuzzy-matching,
  • support for a specific language,
  • etc.

Often extended "existing features" include:

  • omni-completion,
  • text-objects & motions,
  • yanking & putting,
  • status line,
  • search & replace,
  • buffer/window/tab page switching,
  • folding,
  • etc.

The dot operator

How to Compile Vim

Folding

Folding causes multiple lines of text to be collapsed and displayed as a single line. It is useful for hiding portions of a document considered unimportant for the current task. Folding is purely a visual change to the document: the folded lines are still present, unchanged.

A fold is persistent. Once created, a fold can be opened and closed without needing to re-create it. When closed, folds can be moved over or yanked and put as if they were a single line, even though the underlying operation will operate on all of the text underneath the fold

Converting text files from DOS to UNIX with vi

The ^M character stands for a carriage return in Vim (<c-m> or just <CR>). Vim displays this character when at least on line in the file uses LF line endings. In other words, when Vim consider a file to have fileformat=unix but some lines do have carriage returns (CR), the carriage returns are displayed as ^M.

A file that has a single line with LF line ending and several lines with CRLF line endings is most often created by wrongly editing a file created on a MSDOS based system. For example, by creating a file under an MSDOS operating system, copying it to a UNIX based system, and then prepending a hash-bang sting (e.g. #!/bin/sh) using tools on the UNIX based operating system.

:global

Vim's "global" command is used to apply an ex command to every line where a regex matches.

Vim Text Objects

Motions and Text Objects

A text object in Vim is another way to specify a chunk of text to operate on. They can be used with operators or in visual mode, instead of motions.

Vim Registers

Easter Eggs

Autocommands

Surround autocmd commands

autocmd is an additive command, and you probably don't want this behaviour by default.

For example, if you re-source your .vimrc a few times while editing it, vim can slow down.

Here's proof:

:autocmd BufWritePost * if &diff | diffupdate | endif " update diff after save
:autocmd BufWritePost * if &diff | diffupdate | endif " update diff after save

If you now type :autocmd BufWritePost *, you'll see both lines in the output, not just one. Both get executed.

To avoid this behaviour, surround all your autocmds as follows:

if has ('autocmd')       " Remain compatible with vi which doesn't have autocmd
  augroup MyDiffUpdate   " A unique name for the group.  DO NOT use the same name twice!
      autocmd!           " Clears the old autocommands for this group name
      autocmd BufWritePost * if &diff | diffupdate | endif   " Update diff after save
      " ... etc ...
  augroup END
endif

Exiting Vim

Command-line mode is entered through normal mode. You will know you are in command-line mode when there is a : in the bottom left corner of your terminal window.

Normal mode is the default mode of vi/vim and can be switched to by pressing the ESC.

Vi/Vim have built-in checks to prevent unsaved work from being lost and other useful features. To bypass these checks, use the override ! in your command.

In Vi/Vim it is possible to have more than one file displayed (in different windows) at the same time. Use a to close all the opened windows.

Vimscript

The commands in a Vimscript file are executed in command mode by default. Therefore all non-command mode directives should be prefixed.

Normal mode commands (Editing)

Using Python for Vim scripting

Normal mode commands

See sorting in the vim manual for the canonical explanation

Indentation

Vim Resources

This Topic is about Source Code mirrors, Books, Vim-Wikis. It is NOT about Blog entries, Wikipedia, Tutorials. The resources should not be opinion based.

Saving

Regular expressions in Ex Mode

Regular expressions

execute :h pattern to see a lot of regex related information

Useful configurations that can be put in .vimrc

Using ex from the command line

Filetype plugins

Differences between Neovim and Vim

Enhanced undo and redo with a undodir

Auto-Format Code

Whitespace

Get :help (using Vim's built-in manual)

Ask to create non-existant directories upon saving a new file

Solarized Vim

Advantages of vim

vglobal: Execute commands on lines that do not match globally

Plugins