How do you disable all key mappings in vim? - vim

The effect that I would like is to remove all key mappings/bindings from vim such that I can then only specify the bindings I use and build from there.
What is the best way to disable everything so that I can configure a whitelist of my own?

The only way to do what you want is to map every built-in command to :help <Nop> in all relevant modes:
nnoremap a <Nop>
xnoremap a <Nop>
...
and then to create your own custom mappings:
nnnoremap a <do something>
Note that Vim's commands are not implemented as mappings. The above overrides every key combination with a new empty mapping, essentially disabling it, and then creates your own custom mappings.
But Vim's default commands a) are plenty, and b) very expressive so why bother? What value, exactly, are you expecting to get out of this? Why would you want to remove features from a tool whose primary selling point is that it has so many features?

When you define a new key mapping with noremap, or nnoremap inoremap, the default key mappings are overwritten.

Related

Can some share how to write vim map

new to vim, recently came across mapping keyboards issues, what get on google and stackoverflow is only something like this map <C-\> :tab split<CR>:exec("tag ".expand("<cword>"))<CR> and map <A-]> :vsp <CR>:exec("tag ".expand("<cword>"))<CR>, then I searched Vim documentation: map http://vimdoc.sourceforge.net/htmldoc/intro.html#<>, but what it also does not explains the syntax, the meanning of the symbols, can someone suggest some reference materials for this? Thanks very much!
A mapping like the ones you mentioned has three parts:
the type of mapping (map/nmap/imap/vmap/... and the corresponding noremap versions). This determines in what modes the mapping is active. map means normal mode, visual mode and operator-pending mode. When using noremap/nnoremap/... the mapping is made with respect to Vims standard keybindings, otherwise the mappings can themselves call mappings.
The key(s) to bind. This can be a normal key, a control character like <C-\>, or a sequence of keys.
a key sequence to map to.
In your first case, you map the key <C-\> (control-backspace) to :tab split<CR>:exec("tag ".expand("<cword>"))<CR> in normal, visual, and operator-pending mode.

in Vim key mapping, is there a way to repeat a key for multiple times?

I want to add quick key macro in vim for fast editing. Something like
imap <buffer> <A-S-p> printf("\n");<Left><Left><Left><Left><Left>
My question is, is there a multiply operand in key mapping? Something like
imap <buffer> <A-S-p> printf("\n");<Left>x5
Every key in :help key-notation needs to be specified on its own. Most movement commands (like h) take a (prepended, optional) [count], but not <Left>.
If you're looking for a way to type the mapping faster, stop inserting after the ;, and then append the following multiple times: 5a<Left>Esc.

Vim - make comment shortcut

I often write LaTeX using Vim. And I have been taught that one can comment a number of selected lines (in visual mode) using the following command:
:'<,'>s!^!%!
And similarly, one may uncomment lines in visual mode by using this command:
:'<,'>s!^%!!
Here, '%' denotes the commenting symbol for LaTeX. But I would very much like to make a shortcut to make it easier for myself to use these commands. For instance a keybinding or some sort of function so that I do not have to remember this syntax. How does one do that?
First, there are several commenter plugins, that do this very well, and those are generic (and often extensible) so that they work for any filetype, not just Latex:
NERD Commenter plugin
tComment plugin
commentary.vim plugin
are just a few popular plugins.
Custom mapping
That said, it's a good learning experience to develop a mapping on your own. Here's how:
First, mappings are just instructions that when certain key(s) are pressed, Vim translates them into other keys (on the right-hand side). Your mapping is for visual mode, so the command is :vmap. What do you normally do? You select the lines to be commented, and press :; Vim automatically inserts the '<,'> for you. You write the :s command, and conclude by pressing Enter.
Translation:
vmap <Leader>c :s!^!%!<CR>
The <Leader> is a configurable, unused key, defaulting to backslash. So, your mapping is invoked by pressing \ and then C. Put that into your ~/.vimrc to make it permanent, and you're done. Wait! There's more.
Advanced mappings
First, you should use :vnoremap; it makes the mapping immune to remapping and recursion.
Second, that mapping is global, but it applies only to the Latex filetype. So, it should apply only to Latex buffers; there's the <buffer> modifier for that.
You can define that for certain filetypes by prepending :autocmd Filetype tex ..., and put that into your ~/.vimrc. But that gets unwieldy as you add mappings and other settings for various filetypes. Better put the commands into ~/.vim/ftplugin/tex_mappings.vim. (This requires that you have :filetype plugin on.)
vnoremap <buffer> <Leader>c :s!^!%!<CR>
Technically, you should use <LocalLeader> instead of <Leader>. They default to the same key, but the distinction allows to use a different prefix key for buffer-local mappings (only if you need / like).
Let's add the alternative mapping for uncommenting, triggered via \ and Shift + C:
vnoremap <buffer> <LocalLeader>c :s!^!%!<CR>
vnoremap <buffer> <LocalLeader>C :s!^%!!<CR>
Note that you could combine both into one, using :help sub-replace-expression with a conditional expression. If anything here is over your head, don't worry. You should be using one of the mentioned plugins, anyway :-)

Execute <Plug> commands in vim

I've recently found a vim plugin using something called <Plug>. For example there is a command <Plug>abc_def which I would like to execute.
I've tried everything like :<Plug>abc_def and similar. Nothing worked. And :help <Plug> gave no information.
However, I've been able to execute it by creating a mapping :map x <Plug>(unite_redraw). Then I can execute it by pressing x.
Now, is there any way to execute :<Plug>abc_def without creating a dummy mapping just to run it? The actual plugin I use is Unite.
<Plug> mappings are meant to be mapped and called via the map. A <Plug> map is a device to expose a clean interface of plugin actions to the user.
Example: Instead of mapping some key to some plugin function in the plugin's hard code, such as "map zz to the action 'center cursor aesthetically'":
nnoremap <expr> zz 'zz'.float2nr(winheight(0)*0.1).'<C-E>'
it is better to expose only a named <Plug> mapping from the plugin:
nnoremap <expr> <Plug>NiceCenterCursor 'zz'.float2nr(winheight(0)*0.1).'<C-E>'
that the user can then remap in their config, without having to copy and paste the "action":
nmap zz <Plug>NiceCenterCursor
This is then easy to override, reuse, plug into by the user.
<Plug> mappings are active only in the modes they have been defined for. To execute a <Plug> mapping that is defined for normal mode, you can do as with any normal command: use :normal (without the exclamation mark).
:execute "normal \<Plug>NiceCenterCursor"
Since <Plug> actually represents a special magic key, we need to use :normal together with :execute and escape the <Plug>.
The <Plug> mechanism is described in depth at :h 41.11. See also this article about this topic by a Vim master.

What is the best way to add a custom key mapping in vim/vi? (Does there exist a unbound keyspace for customization?)

I know you can add custom keymappings to vim with imap in the .vimrc file. My question is, if one is to create additional custom operators, motions, commands and map them to keys, what is the best way to add them in so they don't conflict with existing bindings?
An analogy, in emacs, a lot of custom key commands are added via the C-c sequence. To be more technical, are there unbound keys in vim that allow for user customization? In other words, is there a free keyspace available to users?
:imapEnter will show a list of your insert mode mappings.
My question is, if one is to create additional custom operators,
motions, commands and map them to keys, what is the best way to add
them in so they don't conflict with existing bindings?
In my opinion the best way is creating custom command, they start with uppercase, native ones with lowercase.
Additionally, you can use a <leader> I use , some use \.
e.g. :let mapleader = ","
then, you can use the leader to combine it with other keys, like ,p to call a command, native or custom, see below:
:map <leader>p :MyCustomCommand<CR>
A custom motion example. Delete the third word after the cursor.
nnoremap <leader>x :normal 2wdw<CR>
Several commands can be used to create new, remove and list the mappings, here is a list of working modes, from :help map-overview
Normal Visual+Select Operator-pending ~
:map :noremap :unmap :mapclear yes yes yes
:nmap :nnoremap :nunmap :nmapclear yes - -
:vmap :vnoremap :vunmap :vmapclear - yes -
:omap :onoremap :ounmap :omapclear - - yes
further info
:help map
Here's an example function, converted to a custom command
function! MoveLastLines(f)
exe '$-9,$w ' . a:f "write last ten lines to the passed filename
$-9,$d "delete last ten lines
endfunction
command! -nargs=1 -range MoveTo :call MoveLastLines(<f-args>)
Now, both lines below are equivalent
:call MoveLastLines('newFile.txt')
:MoveTo newFile.txt

Resources