Skip to content

Instantly share code, notes, and snippets.

@Micrified
Last active November 24, 2023 15:08
Show Gist options
  • Save Micrified/a92d736f995687b99e83948d92dac8d5 to your computer and use it in GitHub Desktop.
Save Micrified/a92d736f995687b99e83948d92dac8d5 to your computer and use it in GitHub Desktop.
VIM User Manual

VIM

Basic Operations

Basics

Input Action
CTRL+U Page up 1/2
CTRL+D Page dw 1/2
<< Unindent (normal mode)
>> Indent (normal mode)
. Repeat last
2dw Delete 2 words
dd Delete line
yy Copy line
p Paste
* Jump to next occurrence of word under cursor
# Jump to previous occurrence of word under cursor

Others

Find-and-navigate

How to search around in normal mode?

Input Action
/<word> Search for a word in normal mode
/\<word>\> Search for a word with whitespace around it (otherwise substring search)
n Jump to next occurrence of it
p Jump to previous occurrence of it
  • To search backwards, use ? instead of /.
  • To search case-insensitive, use /\c with escaped c
  • To search case-sensitive, use /\C with escaped C
  • Use arrow keys after / or ? to navigate search history

Search-and-replace

How to both search and replace in normal mode?

Input Action
:100,200s/a/b Replace a with b between lines 100 and 200
:%/a/b Replace a with b across all lines
:%/a/b/g Same, but applies globally (not just first occurrence)
100,200/\<a>/b/gic Word + Case-insensitive + Commit + Global
  • Use g at the end to search globally
  • Use gi to apply with case insensitivity
  • Use gI to apply with case sensitivity
  • Use gic to apply globally and commit it

Setup and Configuration

Actions

Input Action
:set nu Enable line numbers
:set ai Enable auto-indent (using previous line indentation)
:set si Enable smart indent (using syntax)
:colorscheme Change colorscheme. Get options with CTRL+D
:set shiftwidth=N Set indent size to N spaces
:set list Show whitespace
:set expandtab Use spaces instead of tabs
:set hlsearch Enable search result highlighting
:set incsearch Enable incremental highlighting
:set scrollbind Enable parallel scrolling (auto)

.vimrc

My personal ~/.vimrc file at this moment

syntax on
filetype indent on

"Personal run-command file
:set nu
:set ai
:set shiftwidth=2
:set hlsearch
:set incsearch
:colorscheme Papercolor
:set background=light
:set splitright
:set complete+=kspell
:set completeopt=menuone,longest
:set shortmess+=c
:set encoding=utf-8
:set colorcolumn=80
:set backspace=indent,eol,start

"Adjust scrolling behaviour
map <C-U> 6<C-Y>
map <C-D> 6<C-E>

"Re-assign arrow keys to NOP (also in insert)
nnoremap <Up>      <Nop>
nnoremap <Down>    <Nop>
nnoremap <Left>    <Nop>
nnoremap <Right>   <Nop>

"Re-assign enter key when autocomplete menu shown
inoremap <expr> <CR> pumvisible() ? '<C-p><C-y>' : '<CR>'

"Filetype sensitive tab directives
autocmd Filetype python setlocal expandtab tabstop=4 sw=4 sts=4
autocmd Filetype c      setlocal expandtab tabstop=2 sw=2 sts=2

"Autocorrection directives
abbr ture true
abbr flase false

"Tabbing keybind
nnoremap <C-n> :tabnext<CR>

"Lookup rebind for ctags
nnoremap @ g]

"Custom Zoom-in/Zoom-out toggle function
nnoremap <nowait><expr><silent> <Space><Space>
    \ <SID>toggleZoom() ? "200\<C-W>>200\<C-W>+" : "\<C-W>="
let s:_toggle_zoom=0
function! s:toggleZoom() abort
  let s:_toggle_zoom = s:_toggle_zoom ? 0 : 1
  return s:_toggle_zoom
endfunction

"Git blame line function
function BlameLine(line)
  let blameText=system('git blame -L ' . a:line . ',+1 ' . expand('%'))
  call popup_notification("Last modified: " . blameText, #{highlight: 'WildMenu'})
endfunction

"Git blame keybind
nnoremap <C-b> :call BlameLine(line('.'))<CR>

"Language support
let g:lsc_auto_map = v:true
let g:lsc_server_commands = {
      \ 'c': 'clangd', 
      \ 'go': 'gopls', 
      \ 'typescript': 'typescript-language-server --stdio'
      \}

"Plugin manager (vim-plug)
call plug#begin('~/.vim/plugged')

Plug 'https://github.com/vim-scripts/AutoComplPop', { 'tag': '2.14.1' }

Plug 'https://github.com/natebosch/vim-lsc.git', { 'tag': 'v0.3.1' }

call plug#end()

Visual Block Mode

Input Action
CTRL+V Enter visual block mode
SHIFT+I Insert to the left of the selection. ESC to apply
c Change selection
d Delete selection
r Replace entire selection with next typed character
$ Select until EOL
A Append text to selection
SHIFT+R Replace-mode so changes in place

Buffers

Input Action
:buffers See current buffered content
:ls List all buffers (current marked with %a)
:bp Go to previous buffer
:bn Go to next buffer
:b<N> Go to buffer at index N
:bd Delete current buffer
:e! Restore current file to last saved
CTRL+o Jump to previous buffer
Ctrl+i Jump to next buffer

Windows

Input Action
:sp Split current window (top/bottom)
:vsp Split current window (left/right)
CTRL+W S Split current window horizontally
CTRL+W V Split current window vertically
CTRL+Q Close window
:vsp <filename> Open filename in new window
CTRL+W X Swap windows
:set splitright Always open new window on right side
CTRL+W R Rotate current windows
CTRL+W T/B Go to Top or Bottom window
CTRL+W H/J/K/L Navigate windows
:resize <N> Adjust length by N
:vertical resize <N> Adjust width by N
CTRL+W = Make windows equal size
CTRL+W _ Expand to max height
`CTRL+W `
CTRL+W o Fullscreen current window
:ba Show all buffers

Tabs

Input Action
:tabe <filename> Edit filename in new tab
:tabn Go to tab on the right
:tabp Go to previous tab
:tabnew Open new empty tab
:tabm <0> Move tab to far left
:tab ball Open all buffers in tabs

VIM File Explorer

Input Action
vim <directory> Open VIM manager
/<pattern>/ Auto-highlight directory entry matching pattern. Hit ENTER to goto
s Change sort mode
d Delete file selected
o Open file selected
v Open selected file in vertical split window
:let g:netrw altv=1 Open split to the right always
:Vex Show the vertical explorer window
I Toggle the banner
i Show file information

VimDiff

Input Action
vimdiff <a> <b> Open VIM diff tool with horizontal stacking
vimdiff -o <a> <b> Open VIM diff tool with vertical stacking
vim -d <a> <b> Alternative way to open with horizontal stacking
]c Go to next difference
[c Go to previous difference
:diffget For the present seelcted file and change, grab the opposing change
:diffupdate Rescan a file after a change (to undo the change basically if you didn't write)
:diffput Put the change on the left file to the right file for the given change
CTRL+WW Switch the files being compared Left <-> Right
:zo Unfold collapsed text under the cursor
:zc Fold the text under the cursor
:zr Unfold everything
:zm Fold everything

Quick-guides

  1. Indent the whole file (using autoindent):

    a. gg : Goto top of file b. v : Goto visual mode c. G : Select until end of file d. = : Apply auto indentation

  2. Indent function (in visual mode):

    a. Navigate to just after the opening brace { b. v : Enter visual mode c. Type } To select up-to the closing brace d. Type = to apply the formatting within the block

  3. Lookup (normal mode): Assumes you have Exuberant ctags installed a. Jump to definition under cursor: @ b. Preview: :pts, jump next :tn, jump last :tp

TODO

Here are a few handy things I'd like to know how to do still:

  1. Auto-align a bunch of text together (starting from next word)
  2. Auto-format a markdown table if selected (probably pipe to another program to format)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment