This page looks best with JavaScript enabled

7 most useful vim mappings

 ·  ☕ 3 min read  ·  ✍️ Syed Dawood

01 Leader

Vim's default mapping for leader is /. Most peoples remap it to , ,which I personally think is not a good choice. Remap it to either CAPSLOCK or <SPACE> key, and find out which works better for you.For me CAPSLOCK didn't workout well.

  let mapleader=" " # or
  noremap <Space> <Leader>

02 ESCAPE

In modal editor like VIM, we constantly need to switch modes.Switching to normal mode vim uses esc key which lies too far on the keyboard and ctrl-[ is painful to reach.So I've mapped it to keys available on the home row.It might seem an overkill , using 2 keys to do a job that just required 1 key.But trust me you will thank yourself for dropping this mapping into your .vimrc file after few days.

  imap fj <esc>
  imap jf <esc>

03 : (command-mode)

If you are a regular vim user you might have felt that switching to command mode is requires you to hit 2 keystrokes.We can fix it by mapping : ; in normal mode.Hardly many peoples use semi-colon functionality .If I am right most peoples don't even know wat it does.

  nnoremap ; :

04 Function Key

We have function keys at our disposal, use your creativity and map it to either a command or combination or even vim functions.Below are a mapping from my vimrc

  set pastetoggle=<F2>
  nnoremap <F3> :noh<CR><CR>
  nnoremap <F5> :set spell!<CR>

05 Shift+Function key

Yes you read it correct.You have more 12 keys to fiddle with and make your vimrc more awesome.if you are a programmer map function keys to compile and format code ,call linter. If you are using vim to write notes or write anything else map fn keys to help you out.Possibilities are endless.

  nnoremap <S-F5> ggvG= "shift <F5> to format code

06 Arrow keys

If you are beginner map arrow keys to do nothing, i.e just disble these keys.So that you force yourself to use vim as it is supposed to be used.

  noremap <Up> <NOP>
  noremap <Down> <NOP>
  noremap <Left> <NOP>
  noremap <Right> <NOP>

If you are already in the habit of using hjkl keys to navigate.these mapping are not of much importance to you. Level 2 user can map arrows keys to navigate windows.

  map <Up>   <C-W>k
  map <Down> <C-W>j
  map <Left> <C-W>h
  map <Right> <C-W>l

07 Tab

Tab key in normal mode is useless.Map tab key to switch tab.Tab to switch tab, hahahahaha Below is the vimrc snippet that will help you do that.

nmap gt nmap gT

wrapping up

I have focussed only on the things you need to often in any text editor.There are a lots of articles out there which show you mapping to do just about anything.

Share on

ALLSYED
WRITTEN BY
Syed Dawood
< frontend | backend | fullstack > Developer