+ All Categories
Home > Documents > Vim Key Mapping

Vim Key Mapping

Date post: 05-Apr-2018
Category:
Upload: sergio-luiz-araujo-silva
View: 221 times
Download: 0 times
Share this document with a friend

of 23

Transcript
  • 7/31/2019 Vim Key Mapping

    1/23

    Vim Key Mapping

    Bill Odom (

    [email protected])

    St. Louis UNIX Users Group

    1

  • 7/31/2019 Vim Key Mapping

    2/23

    What?

    Maps bind a series of keystrokes to a key sequence

    Better for defining behavior (vs. abbreviations)

    Better for long-term customization (vs. macros)

    Can be defined across all of Vims modes

    Fundamental to customizing Vim

    2

    Abbreviations are most useful for content expansion, like correcting typos and insertingsnippets/templates.

    By long-term customization, I mean things you want to use beyond your current session.

    Macros are great, but if you have one you like, convert it to a map and stick it in your .vimrc.

  • 7/31/2019 Vim Key Mapping

    3/23

    {cmd} {attr} {lhs} {rhs}map command attributes left-hand side right-hand side

    Map Command Structure

    3

  • 7/31/2019 Vim Key Mapping

    4/23

    Map Command Structure

    " Example map commandmap :echo 'Hello!'

    4

    is an attribute, and is the key being mapped.

  • 7/31/2019 Vim Key Mapping

    5/23

    Map Commands

    nmap

    imapvmap

    xmap

    smap

    omap

    cmaplmap

    map

    map!

    5

    These are the mapping commands that Vim supports, but you probably wont use all of them,and you usually dont want to use them in this form.

  • 7/31/2019 Vim Key Mapping

    6/23

    Map Commands

    nnoremap

    inoremap

    vnoremapxnoremap

    snoremaponoremap

    cnoremap

    6

    These are the ones youll use most of the time.

    Ive left out :map and :map! since Vims mode-specific commands are more precise andunderstandable. The :lmap command is missing, too, since its very rarely used. (See :help

    language-mapping if youre curious.)

    But whats with the nore thats shown up in the middle of the command names?

  • 7/31/2019 Vim Key Mapping

    7/23

    Map Commands

    nnoremap

    inoremap

    vnoremapxnoremap

    snoremaponoremap

    cnoremap

    7

    The usual Vim map commands (without nore in the middle) create maps that allowremapping. That is, any mapped keystrokes on the right-hand side get expanded when themapped key sequence (the left-hand side) is pressed. This is often very useful, but it can be aproblem if it happens unexpectedly.

    The nore -- a.k.a. non-remapping or non-recursive -- versions dont allow remapping;keys mentioned on the right-hand side have their default, built-in meaning. I use the non-remapping commands as a safe default, and only use the remapping versions when Ispecifically want the right-hand side of a map to expand other embedded maps. (Well seeexamples of this in the Sample Maps.)

  • 7/31/2019 Vim Key Mapping

    8/23

    Map Attributes

    8

    Map will not be echoed on the command line Map is local to the current bufer Right-hand side is result of an expression evaluated each time the map isinvoked

    Command fails if the left-hand side is not unique Remapping allowed, but only for maps local to a script Allows use of symbolic key names, even when cpoptions doesnt contain gv

    vnoremap

  • 7/31/2019 Vim Key Mapping

    19/23

    " Disable paste-on-middle-click

    "

    inoremap

    Sample Maps

    19

    A single is how you disable Vim features you dont want. I dont want to unexpectedlypaste my clipboard into random files because I accidentally pressed my scrollwheel too hard.

  • 7/31/2019 Vim Key Mapping

    20/23

    " Center display line after searches

    "

    nnoremap n nzz

    nnoremap N Nzznnoremap * *zz

    nnoremap # #zz

    nnoremap g* g*zz

    nnoremap g# g#z

    Sample Maps

    20

    Great for noisy searches when youve lost your cursor -- just look in the middle of the screen.

  • 7/31/2019 Vim Key Mapping

    21/23

    " Edit vimrc in new tab

    "

    nnoremap ,ev :tabedit $MYVIMRC

    Sample Maps

    21

    A good example of how tab pages can be used to quickly get in and get out of something youneed to do, but that isnt the main thing youre working on at the moment. I update my .vimrcall the time, but I hate to mess up my current window arrangement just to tweak my editorconfiguration.

  • 7/31/2019 Vim Key Mapping

    22/23

    " Delete to end of line, bash-style

    "

    inoremap D

    Sample Maps

    22

    You get used to something in one environment, you want it in another.

  • 7/31/2019 Vim Key Mapping

    23/23

    " Control+Up/Down move lines & selections up and down.

    " (Based on )

    "

    " Define maps for Normal and Visual modes, then re-use

    " them for Insert and Select modes."

    nnoremap :move -2

    nnoremap :move +

    xnoremap :move '


Recommended