The following is my handy post for vim notes, tips, and tricks.

Tutor

Simply type vimtutor into the terminal to open up the tutor. Go through it a few times until it becomes familiar.

Basic Commands

  • u - undo
  • U - undo all changes on a line
  • Ctrl + r - redo

Note that u and undo U

Basic Movements

png

  • hjkl - move around like cursors
  • w - start of next word
  • e - end of the word
  • b - beginning of the word (sort of like opposite of w)
  • gg - beginning of file
  • G - end of file
  • 3w - move 3 words
  • 3igo - write the word “go” three times

  • fo - find next “o”

  • 3fq - find third instance of “q”

  • % - jump from a { to a } or ( to a )

  • 0 - beginning of line
  • % - end of line
  • * - find the next instance of the word under the cursor
  • # - find the previous instance of the word under the cursor

  • 2G - go to line 2

Often, you can use shift to do the reverse of what you were trying to do:

  • n - next
  • N - previous

  • O - insert new line above (The letter O, not the number 0)
  • o - insert new line below

  • x - delete character under cursor
  • X - delete character to the left of cursor

  • A - append to end of line

  • r - replace single character without switching to insert mode

  • d - delete
  • dw - delete first word on right side of cursor
  • d$ - delete to the end of the line
  • p - print the deleted word

  • d2e - delete two words?
  • . - keep deleting

  • v - visual mode

show line numbers - set number (or set nu for short)

Operator and Motion

Motions:

  • w - until the start of the next word, EXCLUDING its first character.
  • e - to the end of the current word, INCLUDING the last character.
  • $ - to the end of the line, INCLUDING the last character.

Typing a number before a motion means it repeats that many times.

Change

Using c to change text automatically puts you in insert mode.

  • ce - change to end of word
  • c$ - change to end of line
  • cc - delete entire line

Operator number motion

Searching for Text

To find a word in Vim, simply type / or ?

Debugging

  • set breakpoint: 158 b

Explore - lets you look at other files; navigate the file structure

ls to see multiple files open in vim

Copy and Paste

yy to yank one line p to paste

So in a lot of ways yy is like dd

Cursor and File Status

  • Ctrl + g to get file info

Find and Replace

First, you’ll need to change your username

  • :%s/jsimonelli/yourusernamehere/g

To get confirmation before you make each change:

  • :%s/old/new/gc

Then, your folder path

If you’re in data ops, it would be

  • :%s/orig_path1\/orig_path2\/orig_path3/new_path1\/new_path1/g

If you’re in computer vision, it would be

  • :%s/orig_word/new_word/g

Substitute:

  • g means global (change every case, not just the first)
  • % means all lines

  • :%s/username/[NAME]/g

Running External Commands

  • Type : to get the cursor to the bottom of the screen.
  • Type ! then the command. For example !ls

In VSCode

use kj to enter vim mode in vscode

https://www.barbarianmeetscoding.com/blog/boost-your-coding-fu-with-vscode-and-vim

Configuring Defaults

Edit your .vimrc file in your home directory.

For example, you can add set number here to always show the line numbers.