4

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.

Josiah Yoder
  • 3,321
  • 4
  • 40
  • 58

2 Answers2

6

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:

  1. Open configuration: :split ~/.vimrc
  2. Edit the mapping in-place
  3. Yank the line: yy
  4. Execute to activate for the current session: :@"
  5. 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.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Thanks! I ended up using `:redir @t` to save to a register (t) instead of a file. Then, after running my command, I did `:redir END` followed by `"tp` to paste from the register. This got me the text I was looking for, which I successfully recreated the edited map from. – Josiah Yoder Jan 08 '15 at 23:03
  • That's :redir @t :map ^A :redir END "tp to get the text used to map the ^A character. – Josiah Yoder Feb 18 '15 at 20:21
  • I was able to use copy & paste to retrieve the existing mapping after typing :map ^A but that's also a bit clunky. Probably prefer the :split /etc/vim/gvimrc solution suggested here – zzapper Nov 30 '21 at 12:49
0

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
drodrigues
  • 118
  • 6
  • 1
    Sorry, this doesn't answer my question. I was trying to get the text of an existing map; I already knew how to do this command. – Josiah Yoder Jan 08 '15 at 23:05