e.g. if I type :im it outputs a long list of messages of the insert mode mappings.
i <Plug>(fzf-maps-i) * <C-O>:call fzf#vim#maps('i', 0)<CR>
The i is for insert mode.
:help <Plug> says that you use it for "internal mappings which is not to be matched with any key sequence". What is this good for? If you manage to press an unpressable key combination it will open the :Maps terminal buffer? Aside from this specific situation, why would you want to bind an unpressable binding?
Next, we have the *. What does it refer to? :helpgrep star or :helpgrep * fail me.
Next we have this line:
i <Plug>(fzf-complete-file-ag) * fzf#vim#complete#path('ag -l -g ""')
Why does the first line above use * and then still use <C-O>:call ... <CR> while this one just mentions the function name?
<Plug> [...] What is this good for? If you manage to press an unpressable key combination it will open the :Maps terminal buffer?
It is an unpressable key combination. It doesn't mean it cannot be generated. If you look at the docs of fzf.vim, you will see an example of a related mapping that user can insert into their .vimrc:
nmap <leader><tab> <plug>(fzf-maps-n)
If the user then presses LeaderTab, it will generate <plug>(fzf-maps-n). This mapping is not noremap, so it will trigger the map <plug>(fzf-maps-n) mapping defined by the plugin. This way the plugin exposes a mappable key sequence, but without any side-effects for the users where they might accidentally trigger it; though they can still map to it if they wish to.
Next, we have the *
:help map-listing will tell you * means it is not remappable (i.e. created by inoremap instead of imap).
Why does the first line above use * and then still use <C-O>:call ... <CR> while this one just mentions the function name?
It is not visible from the listing, but the command that created it was:
inoremap <expr> <plug>(fzf-complete-file-ag) fzf#vim#complete#path('ag -l -g ""')
Note the <expr> modifier. :help :map-<expr>
Related
I am trying to create a mapping the allows me to execute my current node.js file when I press comma + n. I am trying to use the following:
:map <cn> :!node .
When I type this in it simply jumps my cursor to a random line in my current file. How do I create this mapping?
You've specified the mapping keys in the wrong format, see :help key-notation. To trigger the mapping via , followed by N, use this:
:nnoremap ,n :!node %<CR>
Additional notes:
You should use :noremap; it makes the mapping immune to remapping and recursion.
Likewise, you should be as specific in the modes, so :nmap instead of :map for normal mode only.
A mapping works as typed. As you invoke an Ex command from normal mode, you need to conclude command-line mode via <CR>, just as you would press Enter when typing this interactively.
You probably want to pass the current file to node; that's done by the special % identifier, not by .. See :help cmdline-special
noremap :hsp :botright new
noremap :vsp :botright vnew
"Not an editor command: hsp"
I'm probably googling the wrong thing, but I can't find many results on aliasing vim commands. I can find tons of info about mapping keys to commands like my one for tabs:
noremap <C-t> :tabnew<CR>
But can't find commands mapped to other commands.
What you're doing is simulating a command with a mapping. You're saying that when you press the 4 keys :hsv in normal mode, it should type out the keys :botright new (which would need a <CR> to run, as others have said), but it's not actually making the command hsv. You could make an actual command with a user command (:h user-commands). These must start with a capital letter, though.
:command Hsp botright new
:command Vsp botright vnew
Now you can type :Hsp and hit enter to run the command botright new.
Did you try command abbreviation?
ca hsp botright new
ca vsp botright vnew
You will have to initialize the expansion of the abbreviation by hitting the space key afterwards. Depending on the global vim configuration, expansion also happens automatically just when enter is pressed.
with your same mapping, I cannot get the Not an editor command: hsp error message with my vim (v7.4).
Your mapping works fine, but you don't have <cr> at the end, so when you press :hsp in normal mode, your mapping will switch to commandline mode, and put the mapped command there, without executing it. You have to manually press Enter.
#XZS's answer works, but keep in mind that it is an abbreviation(ab), not a mapping. ab is not command aliases, it is not exactly same as mapping. For example, you have to press another key (like space) after hsp to trigger the ab. also, you cannot ab some special keys, this would be another limitation of ab.
There is c(nore)map for command mapping.
e.g. you could have:
cnoremap hsp botright new
with above line, as same as your original one, you have to manually press Enter, if you want it to be executed, you need add <CR> at the end of the line.
I think if I do this, I would create mapping.
Creating command alias can be tricky:
Using a simple cabbrev and/or cmap will cause expansions and mappings to fire in unexpected places like during searches with / and in the middle of filenames.
cmap's will have a visible delay in outputting to the screen which is why cabbrev is often used.
However there are a few ways to create a proper alias:
Create a command via :command.
e.g. command W w
command's first letter must be a uppercase letter
must supply -nargs, -bar, -complete, and -range options according to the needs of your alias
Expression :cabbrev to guard the abbreviation from expanding in in proper places.
expression mapping use the <expr> option
verify getcmdtype() is equal to :
verify the abbreviation is at the beginning of command line via getcmdline() or getcmdpos()
e.g. cnoreabbrev <expr> W getcmdtype() == ':' && getcmdline() ==# 'W' ? 'w' : 'W'
Use :Alias via the cmdalias.vim plugin by Hari Krishna Dara
e.g. Alias W w
uses an expression cabbrev under the covers similar to the technique above
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.
For example, with VimOrganizer:
* one
Then, when I hit Enter, this is what ends up in the buffer:
* oneorg#tbl#kbd_cr()
It looks like you're mixing an expression mapping with a standard mapping.
org#tbl#kbd_cr() is a function that returns keystrokes to an expression mapping (:help map-expr). <SNR>17_AutoPairsReturn is a (script-scoped) mapping itself. To be able to concatenate the two, use an intermediate mapping for the function:
:inoremap <expr> <SID>org-mapping org#tbl#kbd_cr()
:imap <Enter> <SID>org-mapping<SNR>17_AutoPairsReturn
At least for the .vimrc, there are different modes to map a key, for example, imap for insert mode, nmap for normal mode. If you map something with imap, it needs to be aware that it's in insert mode and generate commands appropriately, for example:
:inoremap <F2> <C-R>=expand('%:p:h')<CR>
See Mapping keys in Vim for me info.
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