VIM: Mappable (unused) shortcut letters?

10,285

Solution 1

vim help has a section :he map-which-keys

1.7 WHAT KEYS TO MAP                                    *map-which-keys*

If you are going to map something, you will need to choose which key(s) to use
for the {lhs}.  You will have to avoid keys that are used for Vim commands,
otherwise you would not be able to use those commands anymore.  Here are a few
suggestions:
- Function keys <F2>, <F3>, etc..  Also the shifted function keys <S-F1>,
  <S-F2>, etc.  Note that <F1> is already used for the help command.
- Meta-keys (with the ALT key pressed). |:map-alt-keys|
- Use the '_' or ',' character and then any other character.  The "_" and ","
  commands do exist in Vim (see |_| and |,|), but you probably never use them.
- Use a key that is a synonym for another command.  For example: CTRL-P and
  CTRL-N.  Use an extra character to allow more mappings.

See the file "index" for keys that are not used and thus can be mapped without
losing any builtin function.  You can also use ":help {key}^D" to find out if
a key is used for some command.  ({key} is the specific key you want to find
out about, ^D is CTRL-D).

Solution 2

Many Vim plugins use an initial <Leader> to start their key sequences; this is an (otherwise normally) unused key that is configurable by the user.

                                        *<Leader>* *mapleader*
To define a mapping which uses the "mapleader" variable, the special string
"<Leader>" can be used.  It is replaced with the string value of "mapleader".
If "mapleader" is not set or empty, a backslash is used instead.  Example:
        :map <Leader>A  oanother line<Esc>
Works like:
        :map \A  oanother line<Esc>
But after:
        :let mapleader = ","
It works like:
        :map ,A  oanother line<Esc>

Note that the value of "mapleader" is used at the moment the mapping is
defined.  Changing "mapleader" after that has no effect for already defined
mappings.

Solution 3

Every single ASCII character, upper and lower case, is used for something in Vim. So you're going to wind up overwriting something--just pick something that you don't use. It may help to use a common idiom for your own extensions. I use a leading comma, for example:

map ,w :w!<CR>
map ,e :e #<CR>
imap ,, <ESC>

(The last is particularly useful for me, since I pretty much never need to write two consecutive commas in insert mode, and it's nice not to have to go all the way to the Esc key.)

Solution 4

Typically I use control + [letter] or alt + [letter] for most mappings and it's safe, but watch out for 'w' since that's needed for window commands. You might also be interested in arpeggio.vim which lets you create mappings to simultaneously pressed groups of keys - it will massively expand the possibilities for your mappings with no danger of over-mapping something. For example, you could map "dp" (pressed simultaneously) to execute "ddp" to delete and paste in one command.

Solution 5

Uhmm, no, don't. When creating your mappings try not to overwrite anything ... not so much because you don't use the command you're overmapping, but because some plugin which you have/or will have maybe using it.

And then you overmap it, and then you have to worry.

Personally, for commands such as you gave as an example, I like Ctrl+some key combinations. There are a lot of free ones in vim, and the letters on the left side near Ctrl make a nice pair then.

Btw, what are you trying to do with those mappings ... I understand the second one (delete word by word), but the first doesn't make sense to me. What is it supposed to do ? Transpose lines ?

Share:
10,285

Related videos on Youtube

meder omuraliev
Author by

meder omuraliev

I began learning front end web development around 04-05, started my first program around 06 with PHP. Currently, I am a Web technologist specializing in full stack development and linux administration specializing with the LAMP stack ( HTML5, CSS3, PHP, Apache, MySQL). I also like to dabble in node.js, meteorjs, Python, django and in general like to mess with new technology/stacks. LinkedIn | [email protected]

Updated on July 13, 2020

Comments

  • meder omuraliev
    meder omuraliev almost 4 years

    I'm trying to create two mappings which are efficient for myself:

    map X ddp
    

    Which I'd use to delete and paste in one go.

    map X "_dw
    

    Which would delete a word without yanking into a register.

    However I don't want to break any existing, useful shortcuts so I'm wondering what keys I could use - any suggestions? Am I being too uptidy?

  • hora
    hora over 14 years
    That's actually a really good idea, I think I'm going to copy it.
  • stephenmm
    stephenmm almost 14 years
    Just started using arpeggio and so far I am liking it.
  • Thomas Hunter II
    Thomas Hunter II almost 13 years
    Using <Leader> instead of hard-coded commas would be the preferred solution
  • idbrii
    idbrii over 11 years
    In general, plugin writers should use normal! and noremap. (But not everyone does and I think there are some cases where they cannot.)
  • darjab
    darjab over 11 years
    @pydave - Yes, they should use them where appropriate. Both nmap/nnoremap have their proper usage, and neither should be advocated as being a silver bullet. Best to know the differences.
  • Brian
    Brian almost 11 years
    If you're looking for control keys and what they mean, you may want to quote them with ^V. You could type :help ^V^C to find help for ^C. (^Q may work on toy operating systems.)
  • WalksB
    WalksB almost 4 years
    Thank you so much! The 3rd point noted (using ,) is extremely helpful. It unlocks dozens of empty bindings that are easy to reach.