Vim tips

Tips

Help

  • :h[elp] <command>: Help for a specific command, :q to quit

Modes

Command Mode

  • Use TAB key to autocomplete file name from where vim is run.
  • Use Ctrl + D to display options for the command

Set options

  • Options

    • List all available options

      :h set-option

    • boolean : on or off

      • :se number Turn on line numbers

      • :se nonumber Turn off line numbers

      • :se invnumber or :se number! Toggle line numbers

      • :se number& Set option to default value

      • :se number? Show current value of the option

    • number : an integer value (e.g. :help textwidth)

    • string : a string value (e.g. :help 'backspace')

  • Backup

  • :se mouse=a

  • Color Scheme

    • GitHub - flazz/vim-colorschemes (opens in a new tab)

      Download Vim color schemes into Vim config directory

      git clone https://github.com/flazz/vim-colorschemes.git ~/.vim

    • To see a list of ready-to-use themes

      :colo[rscheme] <SPACE> <Ctrl + d>

    • Change color scheme for the session

      :colo[rscheme] <colorscheme_name>

    • Change color scheme permanently

      append colo[rscheme] <colorscheme_name> into ~/.vimrc

    • Resources

Files

Undo all changes since last disk write

  • Use :e!

Open a file in terminal searching for a string

  • vim +/<string> filename

    Cursor at first occurrence of string

Open a file in terminal with particular options

  • vim +18 filename

    Cursor at a specific line

Diff text

  • vim -d -R 1.sql 2.sql vim -d is equivalent to vimdiff

Output Redirection to Vim

  • vim <(diff 1.sql 2.sql) -R Bash process substitution
  • diff 1.sql 2.sql | vim -R - Using - to open a new buffer

Use vim <archive file> to view contents of an archive file

  • <archive file> could be tar.gz, tgz, zip, etc.

Piping from stdin

  • docker inspect 547a7778d5f0 | vim -

Copy & Paste

  • Vim's unnamed/default register is now the system clipboard, which means if you yank some words in Vim, you can Ctrl + v it somewhere. Reversely, if you Ctrl + c some text, you can p them in Vim as well.

  • When using named registers, just insert "<name of register> before y when copying and p when pasting. Note the double quote before the name. For example:

    1. Select the text you want to copy
    2. type "<register_key>y (<register_key> must be a single character)
    3. Move to where you want to paste it
    4. type "<register_key>p
  • View current registers

    :reg

  • Paste in NORMAL mode

    "<register_key>

  • Paste in INSERT mode

    <Ctrl + r> + <register_key>

Navigation

Occurrence of character in the current line

  • t<character> moves cursor before the next occurrence of character
  • T<character> moves cursor after the next occurrence of character
  • f<character> moves cursor at the next occurrence of character
  • F<character> moves cursor at the previous occurrence of character
  • ; pushes the search forward
  • , pushes the search backward

Navigate to the matching bracket

  • The % key can be used for the following:
    • To jump to a matching opening or closing parenthesis, square bracket or a curly brace: ([{}])
    • To jump to start or end of a C-style comment: /* */.
    • To jump to a matching C/C++ preprocessor conditional: #if, #ifdef, #else, #elif, #endif.
    • To jump between appropriate keywords, if supported by the ftplugin file, eg: between begin and end in a Pascal program.

Reload the current file

  • :e[dit]

Using tabs

  • Open file(s) in new tab(s)

    • From shell

      vim -p <file...>

      Could open multiple files

    • In Vim

      :tabe[dit] <file>

      Edit specified file in a new tab, one file only

  • Commands

    • List all tabs including their displayed windows

      :tabs

    • Go to next tab

      gt / :tabn / Ctrl + PgDown

    • Go to previous tab

      gT / :tabp / Ctrl + PgUp

    • on macOS, pageUp and pageDown might be missing

      • Custom key bindings

        • map <C-]> :tabn<CR>
        • map <C-[> :tabp<CR>

Netrw

  • :E[xplore]

    Open file explorer (Netrw) in the current pane

  • :Le[xplore]

    Open file explorer in the left pane, and file explorer will open file in the right pane

    • Enter

      Open file in the right pane

    • p

      Preview file in the right pane with focus still in file explorer

    • Ctrl + w + z

      Close file preview

    • %

      Create a new file

  • Resources

File system interaction

  • :e <file>

    Open a file in the current pane

  • Split

    • :sp <file>

      Open a file in a new buffer and split window horizontally

    • :vsp <file>

      Open a file in a new buffer and split window vertically

    • Ctrl + w + (h / j / k / l) - move cursor to another pane (using the move direction key)

    • Split resizing

      Ctrl + w + [number] + "+"/"-"

  • List/Open recent files

    • :bro[wse] ol[dfiles]

Search & Replace

  • Settings

    • By default, searching is case sensitive.

    • :se ignorecase

      Turn on case insensitive

    • :se smartcase

      Only works when ignorecase is set, automatically switch search to case-sensitive when search query contains an uppercase letter

    • :se incsearch

      Show match as search proceeds

    • :se hls[earch]

      Turn on highlighting for search match

  • Search

    • Before search

      • Search forward for pattern

        /PATTERN

      • Search forward for pattern in case-sensitive (\C) or case-insensitive (\c) mode, overriding default setting

        • /PATTERN\C or /\CPATTERN

        • /PATTERN\c or /\cPATTERN

      • Search backward

        ?PATTERN

      • very magic pattern

        Any Vim search pattern can include the special sequence \v (very magic), and this will make every following character interpreted as special regex symbols (no escaping needed) except alphanumeric characters (a-zA-Z0-9 and _).

        \vPATTERN

      • no-magic pattern: Using \V has the opposite effect: all characters have their literal meaning and must be preceded by \ to activate their special meaning.

        /\VPATTERN

      • Search whole word

        Specify the word to be searched in the angle brackets, and the brackets must be escaped

        /\<PATTERN\>

    • After search

      • n - repeat search forward
      • N - repeat search backward
      • * - can be used to search the word under cursor
      • g* - search for partial word under cursor (repeat with n)
      • Ctrl + o, Ctrl + i - go through jump locations
      • [I - show lines with matching word under cursor
      • :noh - turn off highlighting until next search
      • Type / and use and to access history
  • Replace

    • :%s/<search>/<replace>/gc
      • % means search the entire file.
      • Alternatively, % can be replaced with line range: 8,10
      • If neither % nor 8,10 is supplied, only the current line is searched and only the first occurrence is matched.
      • g means global, which tells Vim to replace every occurrence on a line, and not just the first occurrence.
      • c means confirm, with which Vim will give you a prompt before replacing.

Terminal

Run a shell command without closing Vim

  • :!<shell command>

Open a terminal window

  • :ter[minal]

Paste register in terminal

  • CTRL - w + " + <Register>

Language support

  • Change syntax coloring

    :se syntax=perl

Editing

  • Change case of characters

    • Normal mode

      • Toggle case HellO to hELLo with g~ then a movement (for this example, g~e)
      • Uppercase HellO to HELLO with gU then a movement (for this example, gUe)
      • Lowercase HellO to hello with gu then a movement (for this example, gue)
    • Visual mode

      • Select text then press ~ to toggle case, or U to convert to uppercase, or u to convert to lowercase.

Resources