Learn Vim Mappings in 5 minutes

Table of contents

Vim mappings - to map keys on your keyboard to specific Vim actions

Basics

Open Vim and type

:map <space> diw

The above mapping maps space key to diw (delete word) in Normal Mode.

:map <c-d> dd

Press Ctrl+d and the whole line gets deleted.

You get the idea.


To remove the above mappings, type

:unmap <space>
:unmap <c-d>

But what if we map x to diw. As we know, pressing x in Normal Mode deletes the character under the cursor.

:map x diw

Now pressing x will delete a word, deviating from its default action.

Btw, these mapping also work in Visual Mode. What if we want to map keys to work only in Normal Mode? In that case, we have to be more specific. Read on.


Modal Mapping

  • nmap - Normal Mode mapping

  • vmap - Visual Mode mapping

  • imap - Insert Mode mapping

:nmap - dd

Pressing - will delete the whole line only in Normal Mode.

:vmap - U

Pressing - will change the selected text to uppercase only in Visual Mode.


Insert Mode mapping is a bit different. Let’s map - to delete a word in Insert Mode.

:imap - diw

The above mapping won’t work. It will insert the text "diw".

Solve this using the following mapping

:imap - <esc>diwi
  • <esc> - switch to Normal Mode

  • diw - delete word

  • i - switch to Insert Mode


You can remove the above mappings using

  • :nunmap

  • :vunmap

  • :iunmap


Continued...

0
Subscribe to my newsletter

Read articles from Shailesh Kumar Sundram directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shailesh Kumar Sundram
Shailesh Kumar Sundram

Terminal & Vim aficionado