Skip to content

Instantly share code, notes, and snippets.

@opdavies
Last active July 1, 2021 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opdavies/f944261b54f70b43f2297cab6779cf59 to your computer and use it in GitHub Desktop.
Save opdavies/f944261b54f70b43f2297cab6779cf59 to your computer and use it in GitHub Desktop.

Vim cheatsheet

Opening windows

:new [filename]

Open a new window above the current window

:vnew [filename]

Open a new window beside the current window

:split <filename>

Edit the specified file in new window above the current window

:vsplit <filename>

Edit the specified file in new window beside the current window

Moving between windows

<C-w>h,j,k,l

Navigate to the window in the given direction (<C-w>j navigates down)

<C-w>H,J,K,L

Move the current window in the given direction (<C-w>J moves it down)

Visual Block mode

Visual block mode allows for selecting a column of text. Unlike standard (aka characterwise) and linewise visual mode, visual block mode is likely the best tool for certain jobs and we recommend using it. Some ideal use cases for visual block mode are editing a git rebase summary

Visual block mode can be started with <C-v> from normal mode. Once started, use normal mode motions to move the highlight region.

Visual block mode uses similar mappings to normal mode, but not identical. The following is a list of the common visual block operations and their mapping:

Mapping

Operation

d, or x

Delete the visual block selection

c

Change

r

Replace all characters in the block with the next character you type

I

Insert text before the block

A

Insert text after the block

Increasing or decreasing numbers

In normal mode, typing Ctrl-A will increment the next number, and typing Ctrl-X will decrement the next number

Creating new relative files

From within Vim, new files are created like existing files are edited, via commands like :edit filename or :split filename. To persist them to disk, you need to (optionally type in contents and) persist them via :write.

Like a command prompt, Vim has a notion of current directory (:pwd lists it). All file paths are relative to it. You don't need to duplicate the path to your current file, there are some nice shortcuts for them: % refers to the current file, :h is a modifier for its directory, minus the file name (cp. :help filename-modifiers). So,

:e %:h/filename
:w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment