I would like to create the map
:map <F2> :map <F12> etcetcmy_map
which would have the result of: when I hit <F2>, the string map <F12> etcetcmy_map is typed into my console. but I'm having problems mapping the < key/char. Most pointedly, this is not being done; the string isn't being typed into my console. What happens instead is the the char under the cursor is case fliped (from upper to lower and vice versa) and then the cursor is moved forward by 1 char.
I've tried reading the documentation and have tried the Ctrl-V method, but I'm not getting it right. Does anyone how to do this, or if it is possible?
In your mapping's right-hand side, the <F12> is executed as a key press of the F12 key (with the unexpected side effects you've described), but you want a literal insertion of the string <F12>. For that, escape the < char as <lt>:
:map <F2> :map <lt>F12> etcetcmy_map
Also, you should use :noremap; it makes the mapping immune to remapping and recursion.
If you want to map F12 to etcetcmy_map when you press F2, you can:
:map <F2> :map <F12> etcetcmy_map<cr>
Related
I would like to set up a key mapping so that when I type this key (specifically the F7 key in my example), it replaces whatever character is in column 6 with an ampersand (&).
I am trying
:nnoremap <F7> 6| R &<ESC>
Yet that doesn't seem to be doing the trick and I cannot seem to grasp why.
Why is what I'm trying wrong?
Yet that doesn't seem to be doing the trick and I cannot seem to grasp why.
So… what does it do instead of what you expect?
Anyway, there are a number of problems with your mapping:
You can't use a literal pipe character in a mapping. You must escape it (\|) or use <bar> instead, which gives us the following:
nnoremap <F7> 6\| R &<Esc>
nnoremap <F7> 6<Bar> R &<Esc>
See :help map-bar.
In your macro, the <Space> character () is interpreted as the <Space> command and thus moves the cursor one character to the right. If you don't mean <Space> (the command) don't use <Space> (the character):
nnoremap <F7> 6\|R&<Esc>
R puts you in replace mode. This is useless because you are only replacing one character and it forces you to <Esc> to normal mode. Use r instead:
nnoremap <F7> 6\|r&
See :help R and :help r.
Alternatively, it may also be useful to move the cursor back to its original position (i.e. the location before the replacement). To do this use:
nnoremap <F7> :let a=getpos(".")<cr>6\|r&:call cursor(a[1],a[2])<cr>
this map is broken up like this:
:let a=getpos(".")<cr> saves the cursor position on variable a
6\|r& is explained in an earlier answer
:call cursor(a[1],a[2])<cr> returns the cursor to its original position
Here you go:
:map <F7> 05lr&
No escape. Just type that and hit enter. The is really typing out those characters -- less than sign, capital F, the number 7, etc..
Is there any way to achieve that remapping? I looked a bit into
map
and
inoremap
but they appear to affect within a given mode, not how to enter a given mode.
You could use nmap, e.g.,
:nmap ; :
to map semicolon to colon. The 'n' in nmap indicates normal mode.
If you wanted to use, say, the <F2> function key to enter command-line mode from insert mode, you can do:
:imap <F2> <Esc>:
There's an exhaustive 3-part tutorial about key mappings in vim here.
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.
There is a line below in vimrc example file
inoremap Ctrl-u Ctrl-G u Ctrl-u
What's the meaning of inoremap and what's the function of this line?
For more on why the command has such a bizarre name see this excellent description between the difference between map and noremap. Really good to know!
To summarise that article, here's a choice quote:
One downside of the *map commands is the danger of recursing...
Vim offers another set of mapping commands that will not take mappings
into account when they perform their actions.
So noremap came about to avoid horrible recursion of mappings like
:nmap dd O<esc>jddk
where the dd in the right-hand side of the map recurses back to the left-hand side definition of the map, and Vim gets stuck in an infinite loop!
The vim :help inoremap is very poetic about this:
:ino[remap] {lhs} {rhs} mapmode-i :ino :inoremap
:ln[oremap] {lhs} {rhs} mapmode-l :ln :lnoremap
:cno[remap] {lhs} {rhs} mapmode-c :cno :cnoremap
Map the key sequence {lhs} to {rhs} for the modes
where the map command applies. Disallow mapping of
{rhs}, to avoid nested and recursive mappings. Often
used to redefine a command. {not in Vi}
Thus it makes some insert-mode mappings for ^U that show the filename (^G, undo the most recent change (u), and scrolls the buffer upwards by half a screen (^U).
I have no idea why someone would want this specific sequence of commands, except to demonstrate the inoremap feature -- the ^U at the refers to the meaning the command had when the definition was created, rather than calling back into the redefined ^U mapping.
I also wondered about this. See http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-Tutorial(Part_1)#Insert_mode_maps :
Insert mode maps
To map keys that work only in the insert and replace modes, use the 'imap' or 'inoremap' command.
Example: The following command maps to insert the directory name of the current buffer:
:inoremap <F2> <C-R>=expand('%:p:h')<CR>
To display the currently defined insert mode maps, use the 'imap' command without any argument:
:imap
To remove a keymap from insert mode, use the ':iunmap' command. For example, the following command removes the insert mode map for .
:iunmap <F2>
As printable keys insert a character in the current buffer in insert mode, you should use non-printable keys to create insert mode maps. Some examples for non-printable keys include the function keys , keys prefixed with the Ctrl or Alt key.
[snip]
So, for example, in my ~/.vimrc I have
inoremap jk <ESC>
inoremap jj <Esc>
which when pressed in Insert mode return me to Normal mode.
In my case the move-right button is ;
I want Ctrl; to move the cursor 7 characters to the right.
I've tried the below .vimrc mapping, but it doesn't work:
nmap <c-;> 7;
Like previous comment says, it seems that ";" cannot be in the form <C-;>.
You can test typing Ctrl+V + key sequence.
Ctrl+V + ; gives only ; whereas Ctrl+V + L give ^L.
So I suppose that vim cannot recognize <C-;>.
You have some more information on the key codes help pages:
:help keycodes
:help <C-
I am not sure, but it might be because <C-;> does not map to an ASCII character. Only #, A-Z, [, \, ], ^ and _ map to ASCII characters (0 through 31 respectively) when combined with Ctrl.
EDIT
I did some searching and found this thread. In it, it is said that gvim.exe works the way I suggest: only use valid control characters, no other. Interestingly vim.exe works differently and you can do the mapping you want.
As others said <c-;> can't be mapped.
The best solution is:
nmap <C-l> 7l
nmap <C-h> 7h
You can remap the regular cursor keys instead.
something like this also would work:
nmap <C-Right> 7l
nmap <C-Left> 7h
Other side example for resizing windows:
" resize horzontal split window
nmap <C-Up> <C-W>-<C-W>-
nmap <C-Down> <C-W>+<C-W>+
" resize vertical split window
nmap <C-Right> <C-W>><C-W>>
nmap <C-Left> <C-W><<C-W><
You can hack your keyboard by using sxhkd + xkvbd. Just to give you an idea I was able to map C-LefMouse so my system recognize as if it was MiddleMouse:
# put this in your sxhkdrc
ctrl + button1
xvkbd -no-jump-pointer -xsendevent -text '\m2'
~/.config/nvim/lua/core/utils
local M = {}
-- https://blog.devgenius.io/create-custom-keymaps-in-neovim-with-lua-d1167de0f2c2
-- https://oroques.dev/notes/neovim-init/
M.map = function(mode, lhs, rhs, opts)
local options = { noremap = true }
if opts then
options = vim.tbl_extend("force", options, opts)
end
vim.api.nvim_set_keymap(mode, lhs, rhs, options)
end
return M
In our mappings.lua
local map = require('core.utils').map
-- copy to the primary selection on mouse release
map("v", "<LeftRelease>", '"*y')
map("i", "<C-MiddleMouse>", '<C-o>"*p')
map("n", "<C-MiddleMouse>", '"*p')
The idea is to make nvim recognize your Ctrl+; as if it was something it can handle, similarly as I did with this primary selection solution. Eventually, these ideas could help someone figure out solutions for other issues.
In KDE konsole terminal, you can add key bindings.
terminal right click menu --> Edit Current Profile --> keyboard --> Edit
I have added a value like this:
Key Combination => ;+Ctrl
Output => \E[9;8~
then you can check the value with Ctrl-V Ctrl-; in termnal.
if successfully printed ^[[9;8~ then you can use the value in vim key bindings something like this
inoremap ^[[9;8~ <esc>A;
you also need to type Ctrl-V Ctrl-; for the value after inoremap