Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vimscript more Mappings


May 24, 2021 Vim


Table of contents


We've said a lot about mappings so far, but now we're going to do it again. Mappings is one of the quick and easy ways to make Vim editing more efficient.

One concept that has appeared in many examples, but none of us have explicitly explained it, is the continuity of multi-character mappings.

Run the following command:

:nnoremap jk dd

Make sure you enter jk quickly out of jk Vim deletes the current row.

Now try j first and pause. If you j without entering k Vim will decide that you don't want to j as the default (move down one line).

This mapping can cause trouble for cursor movement, so let's delete it first. Run the following command:

:nunmap jk

Now quickly jk mode moves down one line and then up one line as usual.

A more complex Mapping

You've seen a lot of simple mappings, and it's time to look at some of the complexity. Run the following command:

:nnoremap <leader>" viw<esc>a"<esc>hbi"<esc>lel

That's an interesting mappings! Y ou can try it yourself. E nter normal mode, move the cursor to a word, and enter the word <leader>" Vim surrounds that word with double quotes!

How does it work? Let's split this map and explain it one by one:

viw<esc>a"<esc>hbi"<esc>lel
  • viw : Highlight selected words
  • <esc> where the cursor is on the last character of the word
  • a : After moving the cursor to its current position and entering ininsert mode
  • " : Insert one "
  • <esc> Return to normal mode
  • h : Move one character to the left
  • b : Move the cursor to the head of the word
  • i : Move the cursor to the current position and enter in insert mode
  • " : Insert one "
  • <esc> Return to normal mode
  • l : Move one character to the right and place the cursor at the head of the word
  • e : Move the cursor to the end of the word
  • l : Move one character to the right, with the cursor positioned on the first quotation marks added

Remember: because we're nnoremap nmap even though you map characters in a character sequence, it doesn't make a name for itself. Vim executes the characters in it as the default feature.

I hope you can see the potential of Vim mappings and the reading difficulties that come with it.

Exercises

Create a mapping, as you just did, with single quotes instead of double quotes.

Try adding vnoremap so that it can wrap your highlighted text in quotation marks. You may need <​``​ so first :help `<`` the help documentation. >

Map H in normal H to move to the first part of the current row. h is left shift, so you can think H as "enhanced" h

Map L in normal L to move to the end of the current row. l is a right shift, so you can L as an "enhanced" l

Read the help :help H and :help L which commands you overwrite. Consider whether this will affect you.

Add these mappings to your ~/.vimrc and make sure you use your "Edit ~/.vimrc and "Reread ~/.vimrc mapping operations