Search multiple terms in Vim

 

Vim has support for extended regular expressions in its search. So, it is possible to search for multiple terms at the same time. This support for alternation using extended regular expressions is not present in Vi.

The operator that is suitable for this is \|. For example, to search for either NewWidth or NewHeight in the source:

/NewWidth\|NewHeight

Thanks to Edward for this tip! :-)

Note that if you have highlighting enabled, which is a very useful feature, all the matches will be highlighted with the same color. If you would like each term to be highlighted with a different color, try a plugin like multisearch.

Tried with: Vim 7.3

Vim: Tabs

[ Tabs open in Vim and GVim on Windows. ]

Like all modern code editors, Vim has support for tabs since version 7.0. Not only are tabs supported in the GVim (the GUI version of Vim), but they are also fully supported in the console Vim!

Tabs are just the latest in a long history of means for editing multiple files in Vi or Vim. First came buffers, of which only one could be seen on the editor display at a time. Then came windows, which allowed the user to split the editor display into multiple panes, each capable of displaying a different file. And now tabs, each tab of which can be split into multiple windows.

Here are the basic tab commands to get started:

  • :tabedit (or :tabe for short) to open a new empty tab.
  • :tabedit Foo.c (or :tabe Foo.c) to open the file Foo.c in a new tab.
  • :tabnext (or :tabn for short) to switch to the next tab. An easier alternative for this command is gt (remember it as Goto next Tab).
  • :tabclose (or :tabc for short) to close the tab. Since I usually have only window open in a tab, I find it easier to close a tab using the good old :q

Tried with: Vim 7.3

Vim: Set Color of ColorColumn

One or more guidelines can be added in Vim using the colorcolumn setting. Vim picks the color for this colorcolumn based on whether it is in GUI or non-GUI mode and on the background setting. If you are not happy with the color of the colorcolumn, it can be changed easily by setting the color of the ColorColumn highlighting group.

For non-GUI Vim running in the Windows command prompt, the color is set using the ctermbg argument to the ColorColumn highlighting group. For example, to set the colorcolumn to a light grey color try:

highlight ColorColumn ctermbg=7

The 16 color values that can be used with ctermbg argument can be seen by typing :help ctermbg

For GVim, the color is set using the guibg argument to the ColorColumn highlighting group. For example, to set the colorcolumn to a black color try:

highlight ColorColumn guibg=Black

To see the list of colors that can be used for the guibg argument, type :help guibg in GVim. A detailed chart of Vim color names can be seen here.

Tried with: Vim 7.3

Vim: Chart of Color Names

I prefer to set the colors in Vim using available named colors rather than RGB values. To make it easy to pick from among the named colors, here is a chart of all the color names available in Vim (see below). This was generated using the script from here.

Vim: Use background to Match Colors of Windows Console

( Left: Default colors of Vim are horrible with background=dark. Right: With background=light )

The default text, background and highlighting colors used by GVim are white-on-black and that for non-GUI Vim is black-on-white. If you have set your Windows command prompt to white-on-black or any other choice of dark-over-light colors, then non-GUI Vim renders in ugly and unreadable colors.

This can be fixed easily by switching the background property of Vim. This is not the background color per se, but influences the colors used by Vim for all aspects of its display like text, background and highlighting. The only two possible values for this property are light and dark. By default, background is set to light for GVim and is set to dark for non-GUI Vim.

So, to make non-GUI Vim use sane colors on a white-on-black Windows command prompt, use the command :set background=light. To make this permanent, add this setting to your vimrc file.

Note: All of this assumes that you have not changed the theme, colors and related settings applied by default by Vim.

Tried with: Vim 7.3

Vim: Delete All Lines of a File

Problem

To delete all the lines of a file that is open in Vim.

Solution

  1. Type gg to move the cursor to the first line of the file, if it is not already there.
  2. Type dG to delete all the lines.

Deleting all the lines of a file is not a frequently used operation. So, I need a command that is easy to recall, even though it is not used much. I find dG easy to remember since I frequently use dd to delete a line, and also gg and G, which move the cursor to the first and last line of the file respectively.

Tried with: Vim 7.3

Windows Environment Variables in Vim

All the Windows environment variables are read by Vim and they are available in Vim as Vim environment variables. Vim environment variables begin with the character $. Hence, the Windows environment variable name is prefixed with the $ character when it becomes a Vim environment variable.

For example, the Windows environment variable %USERPROFILE% will be available as $USERPROFILE inside Vim.

Vim: Pathogen Plugin

Pathogen is a simple Vim plugin written by Tim Pope that makes managing plugin files easy. Typically, plugin files are scattered all over the plugin, autoload and doc subdirectories of vimfiles. It becomes hard to distinguish which .vim files belongs to which plugin. This makes updating or uninstalling plugins quite hard.

Pathogen allows the user to place the files of each plugin in their own private subdirectory. To update or uninstall a plugin, just manage that plugin’s subdirectory. By default, the root directory that Pathogen looks for is named bundle. At runtime, Pathogen forms a list of all these plugin subdirectories and appends them all to runtimepath, which is the path that Vim looks through for loading plugins.

Installation

  1. Place pathogen.vim in vimfiles/autoload directory.
  2. Add this line to your vimrc:
    call pathogen#runtime_append_all_bundles()
    

Usage

  1. Create a bundle subdirectory inside the vimfiles subdirectory.
  2. Create a subdirectory inside bundle for each plugin you use. You can use the plugin name as the directory name.
    1. For plugins that ship with a plugin-autoload-doc directory structure, just unzip the plugin files inside this subdirectory.
    2. For plugins that ship as a single .vim file, create a further plugin or autoload subdirectory (as required) and place the plugin file in it.

Note: If you prefer that installation, updation and uninstallation of plugins be automated, check out the vim-addon-manager plugin.

Using vim-addon-manager on Windows

vim-addon-manager is a Vim plugin written by Marc Weber that can be used to automatically manage Vim plugins. Most popular plugins install many files into several directories, thus making the task of installing, updating and uninstalling them quite tedious. vim-addon-manager attempts to make these tasks easy and automatic. It is still a work-in-progress, especially so on Windows, on which I found it to be quite flaky.

Before Installation

vim-addon-manager automatically pulls off plugins from the Internet both for installation and for updating. So, it requires a set of tools to be installed on Windows before it can be used.

  1. Curl: vim-addon-manager uses Curl to download files from the web.
    • Install a Windows binary of Curl and remember to add the curl.exe directory to the PATH environment variable.
  2. 7-Zip: Many Vim plugins are packaged as .gz or .bz2 files. vim-addon-manager requires a tool to uncompress these files. I prefer to use the awesome 7-Zip tool for this purpose.
    • Install 7-Zip if you do not already have it. Add the 7z.exe directory to the PATH environment variable.
  3. Git: A lot of Vim plugins are hosted on GitHub and other public Git repositories. A Git installation is convenient since vim-addon-manager itself is hosted on GitHub. (If you do not want to install Git, zip files of vim-addon-manager are available from GitHub. See Marc Weber’s comment below.)
    1. Currently, there are 2 versions of Git for Windows available. I prefer to install the MsysGit version. During its installation, make sure to choose the option Run Git from the Windows Command Prompt.
    2. The MsysGit installer should have added the Git\cmd directory to the PATH environment variable. Add it to PATH if this is not the case.
  4. Hg or SVN: A few Vim plugins are hosted on Sourceforge, Bitbucket and other public repositories that use Mercurial or Subversion. If the plugin you use is hosted on such a service, install Hg or SVN binaries for Windows and add their directories to the PATH environment variable.

We are now ready to install vim-addon-manager! :-)

Installation

  1. vim-addon-manager will store its own files and the files of all the plugins it will handle in one directory. %USERPROFILE% on Windows is a good place to host such a directory. %USERPROFILE% on Windows 7 resolves to C:\Users\Joe. Create a directory named vim-addons in %USERPROFILE%.
  2. Open a command prompt in vim-addons and install vim-addon-manager here using:
    $ git clone git://github.com/MarcWeber/vim-addon-manager.git
    
  3. Open your vimrc and add these lines:
    "-------------------------------------------------------------------------------
    
    " Vim Addon Manager
    
    fun LoadVimAddonManager()
    
        " Load vim-addon-manager
        set runtimepath+=$USERPROFILE/vim-addons/vim-addon-manager
    
        " Install/load these plugins
        call scriptmanager#Activate( [ "vim_plugin_1", "vim_plugin_2" ] )
    endf
    
    " Activate the manager after Vim startup
    au VimEnter * call LoadVimAddonManager()
    
    "-------------------------------------------------------------------------------
    

The first time you run vim-addon-manager it will load another plugin named vim-addon-manager-known-repositories. This plugin is used as a database of all Vim plugin names and URLs to obtain them from. After this, vim-addon-manager will try to install the plugins listed in scriptmanager#Activate().

Usage

  • To install a new plugin, just add its name to the list in the call to scriptmanager#Activate(). It will installed the next time Vim is executed.
  • All the existing plugins listed in the call to scriptmanager#Activate() are loaded at runtime everytime a new Vim instance is invoked.
  • To update all the installed plugins from the web use the command :UpdateAddons
  • To uninstalla plugin:
    1. Open vimrc and remove the plugin name from the list in scriptmanager#Activate(). This means that it will not be loaded at runtime. However, the plugin files are still present on the harddisk.
    2. To remove the plugin files, close all Vim instances, open a new Vim window and invoke :UninstallNotLoadedAddons the_plugin_name

Notes

  • vim-addon-manager relies heavily on being able to handle inputs of paths and output paths to other tools it invokes. So, it breaks down if you have shellslash enabled in your vimrc. Remove any :set shellslash in your setup files.
  • I found that invoking scriptmanager#Activate() directly in vimrc at startup does not work. It only works properly if invoked after all the Vim setup is complete. Mark Weber suggests using the event GUIEnter to trigger scriptmanager#Activate(). I found that this breaks down since the input function does not work this early in the Vim startup. The VimEnter event seems to work fine for me, so I use it to trigger scriptmanager#Activate().
  • If you want to a simpler plugin to only manage each installed plugin in their subdirectory, check out Pathogen.