Skip to content

Vim - Tips & Tricks

With a foundational understanding of Vim, let’s explore some tips and tricks to enhance your productivity. This section covers practical techniques for common tasks.

  • Convert spaces to tabs and vice-versa
  • Highlight misspelled words
  • Word completion
  • Copy characters from adjacent lines in Insert Mode
  • Indent code
  • Change file format (line endings)

Managing whitespace (spaces vs. tabs) is crucial for code consistency. Vim provides settings to control this behavior.

To make Vim insert spaces whenever you press the Tab key (for new code):

:set expandtab

To define how many spaces a tab character should represent and the width for auto-indentation (commonly set to 2 or 4):

:set tabstop=4
:set shiftwidth=4

To convert existing tab characters in the current file to spaces, after setting expandtab, tabstop, and shiftwidth as desired:

:retab

It’s best practice to put these settings in your ~/.vimrc (or equivalent) to apply them automatically. For project-specific settings, consider using an EditorConfig file or Vim’s local vimrc features (:help exrc).

To make Vim use actual tab characters when you press Tab, and to convert existing leading spaces in the current file to tabs:

:set noexpandtab
:retab!

The ! in :retab! is important; it tells Vim to convert spaces back to tabs. Ensure tabstop and shiftwidth are set appropriately for your desired tab width.

Vim has a built-in spell checker. To enable it for the current buffer:

:set spell

Misspelled words will be highlighted. You can set the language for spell checking, for example, for American English:

:set spelllang=en_us

Common spell check commands (in Normal mode):

  • ]s - move to the next misspelled word.
  • [s - move to the previous misspelled word.
  • z= - suggest corrections for the word under the cursor.
  • zg - add word under cursor to the dictionary (good word).
  • zw - mark word under cursor as a wrong word (bad word).

For example, if the word “mispelled” is highlighted, moving your cursor to it and pressing z= will show a list of suggestions like “misspelled”.

Vim offers basic built-in word completion. In Insert Mode, type the beginning of a word and then press:

  • Ctrl + p - complete backward in the file for a match.
  • Ctrl + n - complete forward in the file for a match.

If multiple matches are found, you can cycle through them using Ctrl + p or Ctrl + n again. This type of completion scans the current buffer and other open buffers for matching words.

For more advanced, context-aware code completion, modern Vim and Neovim setups often utilize Language Server Protocol (LSP) clients and completion engines like nvim-cmp (for Neovim) or coc.nvim.

Copy Characters from Adjacent Lines in Insert Mode

Section titled “Copy Characters from Adjacent Lines in Insert Mode”

While in Insert Mode, you can copy characters from the line directly above or below the cursor’s current position:

Command (Insert Mode)Description
Ctrl + yInsert the character from the line above that is in the same column as the cursor.
Ctrl + eInsert the character from the line below that is in the same column as the cursor.

These are useful for quickly duplicating single characters vertically without leaving Insert Mode. For instance, if you have:

Lnem ipsum
Lrem ipsum

And your cursor is in Insert Mode on the second line, at the ‘r’ of ‘Lrem’. Pressing Ctrl + y would insert ‘n’, changing ‘Lrem’ to ‘Lnem’.

Vim can help you indent code according to language-specific rules or general settings.

To enable filetype-based indentation (highly recommended, place in your ~/.vimrc):

filetype plugin indent on

Common indent commands (in Normal mode):

  • >> - indent the current line.
  • << - unindent the current line.
  • =% - re-indent the code block surrounded by matching parentheses, braces, etc. (cursor must be on one of the delimiters).
  • =i{ or =a{ - re-indent the inner or ‘a’ block of curly braces.
  • gg=G - re-indent the entire file.

For example, if you have poorly indented Python code:

def my_function():
print("Hello")
print("World")

Positioning your cursor anywhere in the function and using gg=G (if indent settings for Python are correct) would reformat it to:

def my_function():
print("Hello")
print("World")

Different operating systems use different line ending characters (e.g., LF for Unix/Linux/macOS, CRLF for Windows). Vim can detect and convert between these.

To check the current file’s format:

:set fileformat?

To change the file format for the current buffer (this only sets the format to be used when saving):

  • For Unix/Linux/macOS (LF): :set fileformat=unix
  • For Windows (CRLF): :set fileformat=dos
  • For older Macs (CR - rarely used now): :set fileformat=mac

After setting the desired fileformat, saving the file (:w) will write it with the new line endings. For example, to convert a file to Unix line endings:

:set fileformat=unix
:w

You can also configure Vim to automatically detect file formats using :set fileformats=unix,dos,mac (usually the default).