3
I'm trying to create a vim keymapping that will initiate the creation of a new file, with the name of the file prefixed with the current date, waiting for me to finish typing the filename.
Put more precisely, I want to hit <Leader>n
, and have vim enter for me in the command line:
:new 2014-08-05-
... waiting for me to complete the filename and hit <CR>
.
How can I do this? I've tried this, but it didn't do quite what I expected, as it just echo-ed the date. How can I get vim to dynamically insert characters into the command line? Am I missing something simple?
function! NewNotesFile()
echon ":new " . strftime("%F")
endfunction
nnoremap <Leader>n :call NewNotesFile()<CR>
Hello. I tried the first, but it doesn't quite work. I should have been clearer; I don't want to insert the <CR>, as I want to leave the cursor in place to finish off the filename manually. But when I remove it, it just opens a command-line and inserts the literal text
=strftime('%F')
. Am I doing something wrong? – Andrew Ferrier – 11 years agoThe second doesn't work either; I just get the error "Unknown mark". – Andrew Ferrier – 11 years ago
Correction, the second does work; I made a typo. – Andrew Ferrier – 11 years ago
I've considered that (in the first mapping, too); the
<CR>
is just to conclude the<C-r>=
expression. Try it out! – Ingo Karkat – 11 years agoHuh. Just tried the first one again and it worked this time. I guess I'm just typo-prone. Sorry! Thanks for the help in figuring this out, I just learnt about <expr> in mappings :) – Andrew Ferrier – 11 years ago