Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

For instance, :echo strftime(%c) will show the current time on the bottom, but how to insert this time string to the text (right after the cursor)?

share|improve this question

5 Answers 5

up vote 17 down vote accepted

You can use the expression register, "=, with p (or P) in normal mode or <C-R> in insert mode:

In normal mode:
(<C-M> here means Control+M, or just press Enter/Return)

"=strftime('%c')<C-M>p

In insert mode:
(<C-M> has the same meaning as above, <C-R> means Control+R)

<C-R>=strftime('%c')<C-M>

If you want to insert the result of the same expression many times, then you might want to map them onto keys in your .vimrc:
(here the <C-M> and <C-R> should be typed literally (a sequence of five printable characters—Vim will translate them internally))

:nmap <F2> "=strftime('%c')<C-M>p
:imap <F2> <C-R>=strftime('%c')<C-M>
share|improve this answer
    
+1 Off course! The "= register. :-/ –  Eelvex Feb 25 '11 at 7:42
    
to get the value of a vim variable (for example, sessionoptions): <C-R>=&sessionoptions -- it even does wildmode tab-completion! –  Justin M. Keyes Oct 25 '12 at 18:22
:call append(line('.'), strftime("%c"))

Will put it on the next line, then you could press J (Shift+J)to join it up to the current position.

Or if you need it all in one command, you could do

:call setline(line('.'), getline(line('.')) . strftime("%c"))

or

:call setline(line('.'), getline(line('.')) . " " . strftime("%c"))

depending on whether you want a space inserted before the date or not.

share|improve this answer

:r!date +\%c

see :help :r!

share|improve this answer
    
date is an external command and ! calls external commands while OP asks for vim commands. –  Eelvex Feb 25 '11 at 8:51
    
@eelvex no he didn't. and the ! is a vim, and vi, command. This is the canonical method. Works for many other things as well. –  Keith Feb 25 '11 at 11:29
2  
@Keith: yes ! is a vi(m) command that calls external commands. You may be right OP not wanting to output only vim commands but if (s)he does, ! will not do. –  Eelvex Feb 25 '11 at 17:19

These commands will insert the output of strftime("%c") right where your cursor is:

:exe ":normal i" . strftime("%c")

and

:call feedkeys("i". strftime("%c"))

There are other ways to do what you want (like, for example, those on Mikel's answer).

Edit: Even better, for in-place insert, use the = register as Chris Johnsen describes

share|improve this answer

If you want to insert the output of a vim command (as opposed to the return value of a function call), you have to capture it. This is accomplished via the :redir command, which allows you to redirect vim's equivalent of standard output into a variable, file, register, or other target.

:redir is sort of painfully inconvenient to use; I would write a function to encapsulate its functionality in a more convenient way, something like

funct! Exec(command)
    redir =>output
    silent exec a:command
    redir END
    return output
endfunct!

Once you've declared such a function, you can use the expression register (as explained by Chris Johnsen) to insert the output of a command at the cursor position. So, from normal mode, hit i^R=Exec('ls') to insert the list of vim's current buffers.

Be aware that the command will execute in the function namespace, so if you use a global variable you will have to explicitly namespace it by prefixing it with g:. Also note that Exec(), as written above, will append a terminating newline to even one-line output. You might want to add a call to substitute() into the function to avoid this.

Also see http://stackoverflow.com/questions/2573021/vim-how-to-redirect-ex-command-output-into-current-buffer-or-file/2573054#2573054 for more blathering on about redir and a link to a related command.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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