10

In my ~/.vimrc, I have a command defined approximately like this:

nnoremap <expr> <Leader>n ':new ~/Notes/' . strftime('%F') . '-'

It is designed for creating notes files that contain the current date in the filename. The keybinding is designed to leave my cursor on the command line so that I can type the topic of the note and hit Return.

This works from within vim fine. However, I am also trying to specify a terminal command that I can use to open vim and then fire this command. I've tried:

vim -c '\n'

(my Leader is set to the default of \, which I would prefer not to change)

However, this doesn't work - instead I just get Error detected while processing command line: E10: \ should be followed by /, ? or &. I'm not sure if this is a bug in vim or if I'm doing something wrong. I also tried:

vim -c '<Leader>n'

But this gives: Error detected while processing command line: E488: Trailing characters: n Press ENTER or type command to continue.

Is there a way to achieve what I want?

3 Answers 3

7

You could make a function that creates the new entry, and then use the function as part of your mapping:

nnoremap <leader>t :call NewEntry()<cr>
function! NewEntry()
  let title = expand('~/Notes/') . strftime('%F') . '-' . input("Title: ")
  execute 'edit ' . title
endfunction

The mapping may be called as described by @Carpetsmoker:

vim -c 'execute "normal \\t"'
4
  • Karl, excellent, thank you! This also handles the input of the name a little more elegantly too, which is great, and it all seems to work, including the calling of the mapping. Commented Feb 18, 2015 at 8:31
  • No problem, I'm happy it works. Commented Feb 18, 2015 at 8:43
  • What if my mapleader is <space>? I use vim -c 'execute "normal \ t"' and vim -c 'execute "normal <space>t"' both failed. Thanks in advanced!
    – roachsinai
    Commented Jun 9, 2020 at 3:12
  • Try: vim -c 'execute "normal \<space>t"' Commented Jun 9, 2020 at 6:08
8

-c doesn't execute keybinds in normal mode; it executes commands (what you normally do with :). So you need to use the execute command, which seems to work:

$ vim -c 'execute "normal \\t"'

We need the double \\ because we're issuing a shell command (and don't want the shell to interpret this as an escape sequence).

Tested with the keybind:

nnoremap <Leader>t :echo "TEST!"<CR>

I can't get :execute "normal <Leader>t" to work; getting the value of mapleader is also slightly more involved since you get an error if you didn't set mapleader to anything and use the default of \.

4
  • Yes, your test seems to work for me also. But it doesn't work with my nnoremap mapping - I am wondering if that's because I am using <expr> in my mapping. Investigating... Commented Feb 16, 2015 at 13:36
  • I tried simplifying my mapping to nnoremap <Leader>x ':new ~/Desktop/x.txt' to remove the <expr>. But vim -c 'execute "normal \\x"' gives "Error detected while processing command line: E78: Unknown mark". Any thoughts why? Commented Feb 16, 2015 at 13:38
  • @AndrewFerrier Yeah, I can't get it to work with your mapping either :-/ ... Perhaps a better solution for this problem is to use the VimEnter autocmd or some such? ... I don't have time to investigate right now, if no one else comes up with an answer this evening, I'll look into it (I'll leave this answer to be for the time), because this seems like a sort if thing I would want to have/use as well :-) Commented Feb 16, 2015 at 13:39
  • no worries, no rush. It's just a curiosity for me right now. Thanks for your pointers so far. Commented Feb 16, 2015 at 13:40
3

If your goal is to use mapleader and you have it redefined in your .vimrc

let mapleader = ','

that wouldn't work. You can use

$ vim -c 'execute "normal ".get(g:,"mapleader","\\")."t"'

instead.

but if you plan to use that functionality from command line, is better to encapsulate functionality on a function and call it directly.

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.