Alexander Kleinhans
Alexander Kleinhans

Reputation: 6258

Unable to successfully use custom mapped vim commands from vimrc (mac)

I am trying to add a command to my .vimrc and use it.

I have recorded a macro (in the register h) that prints the following (ending with a newline):

one
two
three

I can see this in the register h by typing :reg. It looks like:

"h   ione^Mtwo^Mthree^M^[

I've pasted this as well as an alternate version in my .vimrc under test and test2 respectively:

map <Leader>test ione^Mtwo^Mthree^M^[
map <Leader>test2 ione<C-R>two<C-R>three<C-R><ESC>

My understanding is that test uses characters that can't be displayed such as ^M and ^[, so I've created test2 with what I've read is the .vimrc equivalent.

After restarting vim (which I assume means starting another vim session after this .vimrc has been written to), I test these out.

In normal mode (after hitting ESC multiple times), I try both of the following:

:test

:test2

In both cases, I'm given the error for each respectively:

E492: Not an editor command: test

E492: Not an editor command: test2

After no avail, I check to see if what I've mapped has been mapped. In normal mode, I type :map and do indeed see what I've added, but they appear with a backslash:

   \test2        ione<C-R>two<C-R>three<C-R><Esc>
   \test         ione^Mtwo^Mthree^M^[

Finally, in normal mode, I try again, this time with a backslash. I try both of the following:

:\test

:\test2

This time, I receive the following error for both commands:

E10: \ should be followed by /, ? or &

I am on a mac and have tried using both Terminal (which comes with OSX) as well as iTerm2.

Could someone please lend me some guidance?

Upvotes: 1

Views: 54

Answers (1)

padawin
padawin

Reputation: 4476

your mappings are executed if you press your leader key followed by the keys test or test2. You did not create commands (:).

If you want them as command, then you don't need a mapping, but something along the lines of this in your .vimrc:

function! Test()                
    execute "normal ione"      
    execute "normal otwo"      
    execute "normal othree"    
    execute "normal o"         
endfunc                        
command -nargs=0 Test call Test()

Which you can then use as :Test.

However, if you want to make a mapping and not a command, you might want:

  • a shorter mapping to type (this is very subjective),
  • to use nnoremap instead of map (To be usable in normal mode only, and to not recursively execute mappings),
  • your test2 is what you need (that I change here into <leader>t)
  • <C-R> is Control R, you want <CR> for the return key.

Here's an example:

nnoremap <Leader>t ione<CR>two<CR>three<CR><ESC>

Upvotes: 3

Related Questions