1

Context

  • vimrc:
    • maps <leader> to ,
    • maps ',?' to an action

From a terminal, vim '+normal ,?' runs the action just fine.

Problem

Run the same terminal command without hardcoding the character in the terminal command.

Failed solutions (so far)

  • vim "+normal <leader>?" does not execute the action
  • vim :exe "+normal \<leader>?" does not execute the action (yet vim :exe "+normal ,?" works)

1 Answer 1

1

You can use the value of the mapleader global variable (see :help mapleader) like this

vim +'execute "normal "..g:mapleader.."?"'

See :help :execute how this will create a command. It also explains how .. are used for string concatenation without inserting whitespace.

You don't show what action you map <leader>? to but chances are it's more concise than the expression above. You might want to create a function or command you can call from the command line and map to <leader>?.

Your vimrc would contain these lines:

command MyAction " perform the action

nnoremap <leader>? <Cmd>MyAction<CR>

And this is how it would be called from the shell:

vim +MyAction
3
  • 1
    "chances are it's more concise than the expression above" agreed
    – romainl
    Jan 16 at 10:16
  • 1
    Even better: make it an Ex command called :Action and call it with $ vim +Action. I have :EV for "edit vimrc", which I use quite often as $ vim +EV. Also, the value of g:mapleader can change between uses of <Leader> so there is no "safe" way to query it. One more reason for not using that half-assed feature.
    – romainl
    Jan 16 at 15:58
  • @romainl your earlier comment was so agreeable I started to feel concerned about your well-being. Now I'm very relieved to see you're your true self again. Thank you for your suggestion on using a command. I modified my answer accordingly. The pitfall of mapleader changing is always there. This answer doesn't make it any more likely to happen. In my experience, it works as long as you don't try to be clever.
    – Friedrich
    Jan 16 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.