Vim keybindings

From wikinotes


Mapping Keys

OS notes

macvim:
  - use `set macmeta` to allow binding 'Alt/Meta' within macvim

mapping modes

vim has a different set of hotkeys for different modes. When binding your keys, you map to a specific mode.

:nmap   " bind in normal mode
:vmap   " bind in visual mode (selection)
:imap   " bind in insert mode

:nnoremap "
:vnoremap "
:inoremap "

Listing all mappings

" list all mappings (scroll in vim)
:map

" if fzf.vim installed, search keybindings
:Map

" otherwise, dump all mappings to a buffer
:redir @b|sil map|redir end     " capture output from 'map' (press 'q' to quit)
:(ctrl + p) b                   " paste output into buffer

Finding Available Keys

Not all combinations of keys can be bound in vim when it is run within a terminal. You can test keycodes to see if they are possible with/without modifiers with the command cat, then trying different keycombos to see if they are unique.

vim can make use of two types of keycodes:

  • terminal keycodes (specific to your terminal)
  • vim keycodes (global across all platforms, but may not always work based on the availability of terminal keycodes)

You can also check if a mapping is already being used with:

map <key-mapping>

terminal keycodes

cat            ## type keysequences.
               ## ctrl-d to escape

vim keycodes

:set termcap   ## print all vim keycodes



Default Keybindings

modes

i               "insert mode (type freely)
<esc>, <c-]>    "normal mode (navigation)
:               "command-mode
v               "interactive character-range selection
<c-v>           "interactive character-range selection (bounding-box style)
V               "interactive line-range selection

navigation

code centric

h           "left
l           "right
k           "up
j           "down

0           "jump to beginning of line
$           "jump to end of line

w, e, b     "skip word forward/backwards (until symbol)
W, E, B     "skip word forwards/bacwards (until space)

{, }        "jump to beginning/end of paragraph
[, ]        "jump to beginning/end of scope

f<anychar>  "jump to next occurrenct of whatever the next character is on line
;           "repeat last command forwards
,           "repeat last command backwards

:<line-num> "goto that line-number

screen centric

<c-p>, <c-n>     "next tab, previous tab
<c-w><c-[hjkl]>  "select window to the left,down,up,right
H                "move cursor to top of screen
M                "move cursor to middle of screen
L                "move cursor to bottom of screen

text-insertion

a             "jump to after cursor-char, enter insert mode
A             "jump to end of line, enter insert mode
d             "delete (something)
dd            "delete current line, or selected lines

o             "create line below current line, and enter insert mode
O             "create line above current line, and enter insert mode

x             "delete character under cursor
J             "add the line direectly underneath currentline to currentline

~             "toggle upper-case/lower-case

splits

:vs          "open new vertical split with current file
:sp          "open new horizontal split with current file
:e           "open a new file

:vs /path/to/file   "open a specific file in a new vertical split

tabs

:tabnew       "create a new tab
:tabnew %     "create a new tab with current file in it

<c-p>         "jump to next tab
<c-n>         "jump to previous tab
gt<num>       "jump to tab with the number N