Skip to content

vim

intro

  • vim modes
    1. normal : useful for navigating between files
    2. insert : type code
    3. replace: code you are writting replaces the previous
    4. visual
    5. select : 1. line 2. block
    6. command line mode : command shell from inside vim

buttons in normal mode:

    i // insert mode
    esc // back to normal mode
    r // replace mode
    v // visual mode
    Shift + v // select line
    ctrl + v // select block
    : // command line mode
  • Basic movement: hjkl (left, down, up, right)
  • Words: w (next word), b (beginning of word), e (end of word)
  • Lines: 0 (beginning of line), ^ (first non-blank character), $ (end of line)
  • Screen: H (top of screen), M (middle of screen), L (bottom of screen)
  • Scroll: Ctrl-u (up), Ctrl-d (down)
  • File: gg (beginning of file), G (end of file)
  • Line numbers: :{number} <CR> or {number}G (line {number})
  • Misc: % (corresponding item)
  • Find: f{character}, t{character}, F{character}, T{character}
  • find/to forward/backward {character} on the current line , / ; for navigating matches
  • Search: /{regex}, n / N for navigating matches

buttons in command line mode:

    :q // quit without saving
    :w // save, don't exit

Vim in VS Code

  • you start in Normal mode:

    /* button */
    I, i // insert mode, type normally
    H // top of the Screen
    L <upper L> // end of Screen
    M // middle of the Screen
    
    esc // back to normal mode
    
    j // next line
    k // previous line
    l <lower L> // right one letter
    h <lower H> // left one letter
    
    w // move one word
    e // end of the next word
    g > e // move to the previous word
    f > { char } // move to specific char in the line
    
    0 // Moves to the first character of a line
    ^ // Moves to the first non-blank character of a line
    $ // Moves to the end of a line
    g_ // Moves to the non-blank character at the end of a line
    
    
    } // jumps entire paragraphs downwards
    { // similarly but upwards
    CTRL-D // let’s you move down half a page
    CTRL-U // let’s you move up half a page
    
    
    /{pattern} // to search forward inside a file
    ?{pattern} // to search backwards
    * // searchfor the word under the cursor
    
    { number } > { pattern } // execute the pattern n times like: 2 > j : moves down 2 lines.
    
    
    g > d // to jump to definition of whatever is under your cursor
    g > f // to jump to a file in an import
    
    
    
    g > g // to go to the top of the file
    {line} > g > g // to go to a specific line
    G // to go to the end of the file
    % // jump to matching ({[]})
    
Command Description
:w Save the file
:q Quit the editor
:wq Save and quit the editor
:q! Quit without saving
:wqa Save and quit all open files
:qa! Quit all open files without saving
:w {filename} Save the file as
:e {filename} Open
:e! Reload the file from disk
:sp {filename} Open {filename} in a new horizontal split
:vsp {filename} Open {filename} in a new vertical split
:tabe {filename} Open {filename} in a new tab
:tabnew {filename} Open {filename} in a new tab
:tabc Close the current tab
:tabo Close all other tabs
:tabp Go to the previous tab
:tabn Go to the next tab
:tabfirst Go to the first tab
:tablast Go to the last tab
:tabm {n} Move the current tab to the {n}th position
:tabm + Move the current tab to the right
:tabm - Move the current tab to the left
:tabm 0 Move the current tab to the far left
:tabm $ Move the current tab to the far right
:tabdo {command} Run {command} on all tabs
:tabdo argadd {filename} Add {filename} to the argument list on all tabs
:tabdo argdelete {filename} Delete {filename} from the argument list on all tabs
:tabdo argedit {filename} Edit {filename} in all tabs
:tabdo argglobal {pattern} {command} Run {command} on all tabs matching {pattern}
:tabdo arglocal {pattern} {command} Run {command} on all tabs not matching {pattern}
:tabdo argnew {filename} Open {filename} in all tabs
:tabdo argu List the argument list on all tabs
:tabdo buffer {n} Go to buffer {n} in all tabs
:tabdo buffer {pattern} Go to buffer matching {pattern} in all tabs
:tabdo buffer {filename} Go to buffer {filename} in all tabs
:tabdo buffer # Go to the current buffer in all tabs
:tabdo buffer {n} {filename} Go to buffer {n} in all tabs, or open {filename} if it doesn’t exist
:tabdo buffer! {n} Go to buffer {n} in all tabs, or open a new buffer if it doesn’t exist

Vim in VS Code (Useful commands):

Normal mode (motion commands)

Command Description
0 Move to the start of the line
h Move cursor left
j Move cursor down
k Move cursor up
l Move cursor right
w Move to the start of the next word
e Move to the end of the next word
b Move to the start of the previous word
gg Move to the start of the file
G Move to the end of the file
nG Move to line n
H Move to the top of the screen
M Move to the middle of the screen
L Move to the bottom of the screen
f{char} Move to the next occurrence of char
t{char} Move before the next occurrence of char
F{char} Move to the previous occurrence of char
T{char} Move after the previous occurrence of char
; Repeat the last f, t, F, or T command
, Repeat the last f, t, F, or T command in the opposite direction
r{char} Replace the character under the cursor with char
R Enter replace mode
v Enter visual mode
V Enter visual line mode
CTRL-V Enter visual block mode
o Move to the other end of the highlighted area
O Move to the other corner of the highlighted block
CTRL-O Move to the previous cursor position
CTRL-I Move to the next cursor position
zz Move the line with the cursor to the center of the screen
= , == Auto-indent the current line
> , >> Indent the current line
ggyG copy the whole file to the clipboard
cc Change the whole line
dd Delete the whole line
yy, Y Copy the whole line
p Paste the clipboard after the cursor
P Paste the clipboard before the cursor
D Delete from the cursor to the end of the line
C Change from the cursor to the end of the line
ciw Change the whole word
caw Change the whole word and the trailing whitespace
s , ch Delete the character under the cursor and start insert mode
S Delete the whole line and start insert mode
~ Switch the case of the character under the cursor
x , dl Delete the character under the cursor
X , dh Delete the character before the cursor
u Undo the last change
CTRL-R Redo the last change
. Repeat the last change
{cmd}a{text-object} runs cmd around the text object. text-objects: w,s,p,b,B, ",', (), {}, <>,[],t
{cmd}i{text-object} runs cmd inside the text object. text-objects:w,s,p,b,B, ",', (), {}, <>,[],t

Ex-mode (command mode)

Command Description
:w Write the file
:wq Write the file and quit
:q Quit
:q! Quit without saving
:wqa Write all modified files and quit
:qa Quit all windows
:qa! Quit all windows without saving
:w {filename} Write the file to
:e {filename} Edit
:e! Reload the file
:sp {filename} Open {filename} in a new split
:vsp {filename} Open {filename} in a new vsplit

References