Skip to content

Vim - Using Vim as IDE

Vim can be configured and extended to function much like a modern Integrated Development Environment (IDE). This section explores how to enable several IDE-like features in Vim:

  • Syntax highlighting for code readability
  • Smart indentation for structured code
  • Navigating code blocks and matching pairs
  • Executing external shell commands
  • Code navigation using ctags and cscope
  • Built-in auto-completion and suggestions

Syntax highlighting is a crucial feature for code readability, displaying different parts of code (keywords, comments, strings) in various colors and styles. To enable syntax highlighting in Vim, use the command:

:syntax on

For instance, when editing a C file, keywords like int, void, if, and for will be colored differently from variable names, comments, or strings, making the code easier to parse visually.

To ensure syntax highlighting is automatically enabled based on the file type, you can add the following to your Vim configuration file (usually ~/.vimrc or ~/.config/nvim/init.vim for Neovim):

filetype plugin indent on
syntax on

To disable syntax highlighting for the current session, use:

:syntax off

When syntax highlighting is disabled, all text will typically appear in a single color, making it harder to distinguish code elements.

Proper indentation makes code more readable and maintainable. Vim offers several options for automatic and smart indentation:

:set autoindent " Copies indent from the current line to the new line
:set smartindent " Enables smarter indentation for C-like languages (e.g., after '{')

For C and C-like languages, you can also use C-style specific indentation:

:set cindent

A more general approach is to enable filetype-specific indentation, which loads indentation rules based on the current file’s type. Add this to your Vim configuration file:

filetype indent on

This command, along with filetype plugin on, allows Vim to automatically apply appropriate indentation settings for various programming languages.

In many programming languages, parentheses (), brackets [], and curly braces {} are used to group statements or define blocks. Vim’s % command is invaluable for navigating between these matching pairs.

Place your cursor on an opening or closing parenthesis, bracket, or brace, and press % in Normal mode. Vim will jump to its corresponding match. Pressing % again will jump back. This also works for C-style comments /* ... */ and preprocessor conditionals like #if/#endif in some filetypes.

For example, if your cursor is on an opening curly brace { at the beginning of a function, pressing % will move the cursor to the corresponding closing brace } at the end of the function.

Vim allows you to execute external shell commands without leaving the editor.

To execute a single shell command:

:!command

Replace command with the actual command you want to run. For instance, to see the current working directory:

:!pwd

To insert the output of a command directly into your buffer at the current cursor position:

:read !command

For example, :read !date will insert the current date and time into your file.

If you need to run multiple shell commands or want an interactive shell session, use:

:shell

This command opens a new shell session. Once you are done, type exit in the shell to return to your Vim session.

For larger projects, quickly navigating code (e.g., jumping to function definitions, finding where a function is called) is essential. ctags and cscope are classic tools that provide this functionality in Vim.

ctags (preferably Universal Ctags, which supports more languages) generates an index file (usually named tags) of language objects found in source files. Vim can then use this file to quickly jump to definitions.

  1. Install Universal Ctags: Use your system’s package manager (e.g., sudo apt install universal-ctags on Debian/Ubuntu, brew install universal-ctags on macOS).
  2. Generate tags: Navigate to your project’s root directory in the terminal and run:
  3. $ ctags -R .
  4. This command recursively scans all files in the current directory (.) and generates a tags file.
  5. Configure Vim: Ensure Vim knows where to find the tags file. You can set this explicitly:
  6. :set tags=./tags,tags;/向上搜索
  7. Adding tags;/ allows Vim to search for a tags file in the current directory and then search upwards in the directory tree, which is very convenient.
  8. Navigate: Move your cursor over a function name, variable, or class and press Ctrl + ] to jump to its definition. Use Ctrl + T to jump back to the previous location. You can also use :tag <name> to jump to a specific tag, or g<Ctrl+]> if there are multiple matches for a tag.

cscope offers more advanced code searching capabilities than ctags, such as finding where a symbol is used, functions calling another function, etc.

  1. Install cscope: Use your system’s package manager (e.g., sudo apt install cscope on Debian/Ubuntu, brew install cscope on macOS).
  2. Generate cscope database: In your project’s root directory, run:
  3. $ cscope -b -R -k
  4. This command builds the cscope.out database recursively (-R), in the background (-b), and in kernel mode (-k which disables using /usr/include).
  5. Connect cscope to Vim: Add the cscope database to Vim:
  6. :cs add cscope.out
  7. You might want to add this command to your .vimrc or use a plugin to manage cscope connections automatically.
  8. Use cscope queries: Vim provides commands to query the cscope database. For example:
  9. You can create mappings for these commands for easier access. For more details, see :help cscope.

Note: For many modern programming languages, Language Server Protocol (LSP) clients (often integrated via Vim plugins) provide more sophisticated code navigation, auto-completion, and diagnostics. While LSP is becoming the standard, ctags and cscope remain fast, lightweight, and effective tools, especially for C/C++ and projects where LSP servers might be unavailable or heavy.

Vim provides several built-in mechanisms for auto-completion, which can significantly speed up coding. These commands are typically used in Insert Mode:

  • Ctrl + N or Ctrl + P: Keyword completion. Scans the current buffer and other open buffers for words matching the one you are typing. Ctrl+N finds the next match, Ctrl+P finds the previous match.
  • Ctrl + X Ctrl + L: Whole line completion. Suggests entire lines to complete the current one.
  • Ctrl + X Ctrl + F: File name completion. Helps complete file paths and names.
  • Ctrl + X Ctrl + O: Omni completion. Provides language-specific, context-aware completion. This requires filetype plugin on and often a corresponding ftplugin for the language you are using.

While Vim’s built-in completion is quite powerful, the Vim ecosystem offers many plugins (e.g., coc.nvim, YouCompleteMe, or LSP-based completers) that provide more advanced, IDE-like completion features, including fuzzy finding, snippet integration, and detailed signature help. Exploring these can further enhance your Vim experience.

Remember to use these commands while in Insert Mode. Vim will typically show a pop-up menu with suggestions, which you can navigate using Ctrl+N and Ctrl+P (or arrow keys in some configurations) and select with Enter or Ctrl+Y.