Vim tips

January 03, 2020

This is a list of various vim tricks I find useful and get asked / forget about.

Alternate File & Jumplists

Although both of those nav methods are quite useful, they’re not as good as fuzzy finder would be. Bear that in mind when you’re considering your plugins.

  • If you’ve edited more than one file in that session, you’d have populated the # register that holds a reference to the previous file. Type in reg to check that. Once you’re sure that register is populated, you can jump between current and previous file by pressing <C-^>, which also works just fine as <C-6> on a QWERTY keyboard. Super useful for testing, when you can have the file and its respective test open right where you need it.
  • Jump list help: :h jumplist. :jumps to see a list of jumps. To navigate between them, use <C-i> and <C-o>. SUPER useful for when you want to jump to a specific place and come back, for example gg to go to the top of the file, and then jump back with <C-i>.

Commands

  • :set scrollof=8 sets auto-scroll after you’re 8 lines over from the edge of the file
  • :set number to turn on line numbers
  • :set rnu to set relative line numbers

 Copy and paste to a global system buffer

  • Copy: "*y
  • Paste: "*p

Delete

  • <shift-D> deletes from cursor to end of line

Edit

  • :e allows you to open and edit a file. Useful in NetRw or otherwise, but not the greatest solution, supports globbing, but not fuzzy finding. That makes you have to actually traverse the file tree.

Expanding

  • You can expand text by pressing tab, for example when typing the : commands. If you’re typing in a command, and press , you will see a little pop up menu with all the possible options. Neat.

Explorer

  • To enter the explorer, simply type in :Ex.

Find and replace

  • s/a/b/gc will replace every a with b globally, but will ask you first on every instance.

 Indentation

  • < indents one level to the left.
  • > same, but to the right.

Inserting

  • a and i get you into insert mode, but on different sides of the cursor
  • I goes to the first non-whitespace character of this line
  • o and O makes a newline, respects the indenting, and puts you into insert mode
  • <C-v> tells vim to treat the next character literally. Enables inserting tabs for example

Jumps (while editing)

  • f followed by a char takes you to that char
  • t followed by a char takes you one char before it

Marks

  • m followed by a capital letter sets a mark in a file
  • ' followed by a capital letter goes to that mark
  • Same applies to lowercase letters. Uppercase are global, lowercase marks are local to a buffer

Pane splits

  • Open the explorer and split panes vertically/horizontally use :Vex and :Sex.
  • Open a file named bread.bd in a new pane :sp bread.bd
  • Move between splits by using ctrl-w and press directional key (arrows or home row)
  • To close all but the current pane you’re in, do <C-w> followed by o. Boom.

Quick editing tips

  • ciw deletes the word under a cursor and puts you in insert mode
  • cw does the same but doesn’t delete the whole word, deletes until the end of the word.
  • . performs the last action again. Super useful

Registers

  • :h reg opens help on registers
  • :reg opens register view

Ripgrep

  • To search with ripgrep use :Rg.
  • It’ll open a fzf finder, use <tab> to mark files that you want to have in the quicklist.

 Sorting

  • To sort a bunch of lines, select them in visual mode, and run the :sort command.

Searching

  • To start a search, type in the /, followed by the pattern you’re looking for. Then use n to go forward to the next match, and N to go to the previous match.
  • :set hls ic sets highlighting and incremental search.

Spellchecking

  • To enable spellcheck, use :set spell spelllang=en_us. To disable it, :set nospell.

Other

  • zz to center the screen on the current line
  • P and p are different modes of posting
  • :so % source current file (useful for editing vimrcs)

Quicklist

  • :cdo enables you to execute a command against quicklist. Then, you can execute a write all command :wa to make it happen. It’s great for a global find-and-replace.

Vim blogposts I found useful


Written by Daniel Kaczmarczyk, a software engineer and educator. you can find me on twitter or email me at daniel.kaczmarczyk@hey.com

a pale blue and yellow circle