How to edit an existing key mapping in vim? - vim

How do I edit an existing mapping in vim? I set the mapping in my .vim file using one of the standard mapping commands:
map ^A o]]></code>^M<GRD minus="" summary="">^M^M<p></p>^M^M</GRD>^M^M<code><![CDATA[0kkkkkk5ehi
I want to edit this long command to use "matlab" instead of code, e.g.:
map ^A o]]></matlab>^M<GRD minus="" summary="">^M^M<p></p>^M^M</GRD>^M^M<matlab><![CDATA[0kkkkkk5ehi
However, I don't want to edit the .vim file -- I will use the original mapping again. I just want to change the mapping for my current session. I tried :map ^A, but this only displays the current mapping, and I cannot copy the displayed text.
P.S. Note that the ^M and ^A characters are inserted with Ctrl-Q Ctrl-M, etc.

Tweaking a mapping for a current session only is unusual; probably that's why it's supported poorly. I would guess that you're not actually concerned about a session, but rather a Matlab filetype. For that, there's excellent support. If the 'filetype' is detected as matlab, you can put a buffer-local mapping variant in ~/.vim/after/ftplugin/matlab.vim:
map <buffer> ^A ...
Direct editing
If you'd rather stick with your original plan, I would do it like that:
Open configuration: :split ~/.vimrc
Edit the mapping in-place
Yank the line: yy
Execute to activate for the current session: :#"
Abort the edit without saving: :bdelete!
Alternative
For a more general solution, you could also capture the output of the original :map ^A command via :redir, then :put that into a :newscratch buffer, and finally :source that. It's more manual steps and typing, but could be automated by a custom command. Worthwhile if you need this often.

To set your map for the current session only, do
:map ^A o]]></matlab>^M<GRD minus="" summary="">^M^M<p></p>^M^M</GRD>^M^M<matlab><![CDATA[0kkkkkk5ehi

Related

Insert mode default keys in vim

The following items are useful to me in editing text, and I was wondering if vim had something for this built out of the box (though I didn't see it on the https://vimhelp.org/index.txt.html#index.txt page), or I had to create mappings for it:
Forward-delete a character. This is X in normal mode.
Forward-delete all text to the right of the cursor on the line. This is the inverse of ctrl-u.
Are either of these mappings available? And if not, are there 'standard' mappings for this that are common (for example, how it might be done in another unix program).
Note that this is the keyboard I have -- there is only one delete key (which acts like a normal backspace key) and there is no backspace key:
Note: for forward-delete, I am currently mapping ctrl-d as:
"Ctrl-d to forward-delete when in insert or command mode
noremap! <C-d> <Delete>
However, this interferes with the tab in insert mode (which I don't use) and the help-options in command mode (which I do use!) so I may have to modify this later, or hopefully someone suggests a better solution.
though I didn't see it on the https://vimhelp.org/index.txt.html#index.txt page
If you can't find it in the documentation, then it doesn't exist.
You can use fn+delete for "Forward-delete a character".
"Forward-delete all text to the right of the cursor on the line" is ctrl+k in MacOS, but Vim has its own use for that combo, :help i_ctrl-k so it is up to you to create a mapping for it.
Something like:
inoremap <key> <C-o>ld$

Vim: Use file name in mapping without extension

I have a command that let me jump to a specific nested yaml key called :YamlGoToKey, and I've mapped it to a shortcut.
nmap <leader>yx :YamlGoToKey<space>
Now I want to prepend the language of the file to the command, to save me some time when typing (and I can copy paste keys without the leading locale), e.g. it should look like this after the shortcut
:YamlGoToKey en.
I tried it with this mapping
nmap <leader>yx :YamlGoToKey<space>!shellescape(expand('%:t:r')).
but the result is
:YamlGoToKey<space>!shellescape(expand('%:t:r')).
So, how can I get the name of the current file, without the extension, to show up in my command?
Use Ctrl-R= (:help c_CTRL-R_=):
:nmap <leader>yx :YamlGoToKey <c-r>=expand('%:t:r')<cr>.

How to refer to key combinations in vim keybinding that works on the query line

I use pandoc markdown in text files and want to automate links that refer to internal textnodes. For example I have a link like [\%110lund] going to the word "und" in line 110. To automate the jumping process I defined a keybinding:
nnoremap <Leader>l vi[y/<ctrl+r>0<CR>
Unfortunately <ctrl+r> is written as the query string instead of performed to copy the visual selection.
So my question is how do I have to notate <ctrl+r>0 at this location so that it is actually performed instead of written out
Use c+r instead of ctrl+r.
In order to avoid confusion, I am striking out the incorrect edit that someone else made, rather than reverting it. In the context of a vim mapping (such as the :nnoremap of this question) the following should be typed literally. For example, <c-r> really means 5 characters.
Use <c-r> instead of <ctrl+r>.
See :help keycodes for more options.

Clear all currently defined vim macros

I have a vim macro that I keep mistyping, usually when I'm trying to save something so I do it quickly and I can't work out what keys I pressed. It is annoying as it pastes some irrelevant bash code into my file so I have to undo the erroneous pasting which also undos the last thing I typed that I do want.
I was looking for a way to either list the currently defined macros (so I can redefine the offending one), or a way to clear out them completely. I only use macros in the very short term so I don't mind losing them all.
Cheers
Macros
What is mostly called a macro in vim is initiated with # and a letter. It executes the contents of a register with the said letter as its name.
List the register contents
:reg
You can clear the register a with
:let #a = ''
Sometimes mappings or abbreviations are confused for macros, therefore here is some information about them.
List mappings
all mappings
:map
all mappings that start with \
:map \
normal mode
:nmap
insert mode
:imap
visual mode
:vmap
command mode
:cmap
List abbreviations
all abbreviations
:abbr
all abbreviations starting with email
:abbr email
insert mode
:iabbr
command mode
:cabbr
You can use :verbose before the previous commands to get more info about where the mapping/abbreviation was last set, as in :verbose nmap. These commands also show all the mappings that have been set by plugins and other configuration files.
Removing a mapping/abbreviation
(only a couple of modes listed here, you should be able to remove one just for a certain mode just like with the listing commands above.)
insert mode, remove a mapping that is defined as \:
:iunmap \\
normal mode:
:nunmap \\
remove an abbreviation defined as email:
:unabbr email
insert mode:
:iunabbr email
Clear mappings/abbreviations
I wouldn't recommend clearing all mappings/abbreviations since you would lose everything your plugins have mapped/abbreviated. However, you can clear mappings by putting the letter c after the above listing command, as in :imapc to clear insert mode mappings. Abbreviations can be cleared with :abclear, or just for insert mode :iabclear and just for command mode :cabclear.
To clear a single macro - record over it - with nothing.
For your situation in normal mode you'd just type "qwq".
q to begin to record a macro
w to set the buffer space to w
q again to save the (now empty) macro
Verify with :reg[ister] w
This is only slightly quicker than :let #w = '' but for myself is easier to remember.

View a list of recent documents in Vim

Is there a way to view the list of recent documents you've opened in Vim?
I realize I could view the cursor jump list, :ju, and then go to a cursor position in the list but this is not ideal because there will be multiple listings of the same document in the list.
Is there another command which would do what I'm looking for?
Don't use a plugin, unless you want a nice menu. From Vim Documentation: Starting (or :help old):
:ol[dfiles]
Then to open one of the listed files, use: '0, '1, '2, ... '9
List the files that have marks stored in the viminfo file.
:bro[wse] ol[dfiles][!]
List file names as with :oldfiles, and then prompt for a number. When the number is valid that file from the list is edited. Use ! to abandon a modified buffer.
The Most Recently Used (MRU) plugin provides an easy access to a list of
recently opened/edited files in Vim. This plugin automatically stores the
file names as you open/edit them in Vim.
http://www.vim.org/scripts/script.php?script_id=521
Besides :oldfiles, fzf.vim has :History.
Start Vim and hit Ctrl-o-o to open previously edited file. Keep hitting o (while still pressing the Ctrl key) to cycle back through earlier files. See https://dev.to/jovica/3-little-known-but-useful-vim-tips-1pbg
vim plugin: minibufexpl may help you.
the opened file list is displayed on the top or bottom of the screen:
in vim normal mode, type :b${bufid} to jump to the ${bufid}_th buffer, for example: type :b13 to jump to the 13th buffer, ie. ngx_mail_ssl_module.c.
besidies, you can map some convenient bindings in your vimrc, such as:
" ------------------------------- minibufexpl mappings -----------------------------------
"let g:miniBufExplSplitBelow=1
nnoremap <silent> <leader>bn :bn<cr>
nnoremap <silent> <leader>bp :bp<cr>
nnoremap <silent> <leader>bf :bf<cr>
nnoremap <silent> <leader>bl :bl<cr>
nnoremap <silent> <leader>bt :TMiniBufExplorer<cr>
Get the plugin from here: https://github.com/fholgado/minibufexpl.vim
In addition to oldfiles there's a nice thing called tinyMRU.
Vim-tinyMRU's only purpose is to provide an intuitive alternative to the built-in :oldfile command. Nothing more, nothing less.
It's very simple:
https://github.com/romainl/vim-tinyMRU/blob/master/plugin/tinymru.vim
A good plugin is https://github.com/Shougo/denite.nvim
You can call :Denite file_old in order to have fuzzy search on the list of old files. In particular, just hitting Enter will re-open the last opened file. Assigning a shortcut to this is useful:
nnoremap <leader>o :Denite<space>file_old<CR>
This saves few keystrokes compared to :browse oldfiles, q, 1, Enter
The easiest way for me to access recent files is to add the following to one's .gvimrc file:
let g:netrw_sort_by = 'time'
let g:netrw_sort_direction = 'r'
These lines get netrw to sort files by those most recently modified. Then one simply calls :e. and selects the file one wants.
This solution presupposes files are saved in one main directory so specified in .gvimrc. E.g.
cd ~/vim
No directly the answer but related.
you can define aliases to open the last opened file(s) by vim:
alias vil='vim -c "normal! '\''0"' # open the last file
alias vil1='vim -c "normal! '\''1"' # open the second last file ...
alias vil2='vim -c "normal! '\''2"'
:ol works, but why not use fuzzy search to get the exact match quickly from a long list of file?
There's a very handy plugin, ctrlp which allows you to use :CtrlPMRU, and you can quickly get what you looking for.
For many many years, ctrlp is one of the first thing I would install for vim!
https://github.com/ctrlpvim/ctrlp.vim

Resources