2

My context: I frequently take notes in VIM. I'd like a VIM function to type a standard-header into the command line (specifically, a timestamp such as :sav 20180418_) without executing; control would return with VIM still in command-mode (so that user could append remainder of the filename and execute).

My fundamental difficulty: I cannot seem to get a Vim function/macro to enter command-mode, supply text to the command line, then exit while staying in command-mode and not executing the text supplied.

Is this possible?

Thank you.

0

1 Answer 1

3

You can use expand() function. For example if you are currently editing file 20180418_.txt you can type:

:sav <c-r>=expand("%:r")<cr>

where <c-r>= should be typed as Ctrl+R followed by =. Enter key is <cr>. This will expand the text in the command line into:

:sav 20180418_
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Is it possible for this to be coded in a Vim-function (so I can initiate it with a key-binding)? Rather than expand("%:t") (which returns nothing for me), I'd likely use strftime("%Y%m%d_"). There's something basic about command-mode content in Vim functions that I seem to not understand: I tried entering :sav <C-R>=strftime("%Y%m%d_")<CR> as the one line of a function (didn't run).
You can use a command-line map: cnoremap 'e <c-r>=strftime("%Y%m%d_")<cr>. Now typing 'e in command line will expand into 20180418_
Thanks, that's a close-enough workaround for my purposes. :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.