24

I get a variable's value in vim's script, and how to write it into the file I'm editing now.

e.g.

"=== get date
let TodayDate=system("date")
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
songhir
  • 3,393
  • 3
  • 19
  • 27

2 Answers2

52

You can use :put to put the contents of the variable (or expression) into the current buffer

:put =TodayDate

The help for :h :put

                                                        :pu :put
:[line]pu[t] [x]        Put the text [from register x] after [line] (default
                        current line).  This always works linewise, thus
                        this command can be used to put a yanked block as new
                        lines.
                        The cursor is left on the first non-blank in the last
                        new line.
                        The register can also be '=' followed by an optional
                        expression.  The expression continues until the end of
                        the command.  You need to escape the '|' and '"'
                        characters to prevent them from terminating the
                        command.  Example: 
                                :put ='path' . \",/test\"
                        If there is no expression after '=', Vim uses the
                        previous expression.  You can see it with ":dis =".

For mappings and editing <C-R>= is probably better than :put since it allows you to use the expression register and output the contents at the cursor location. (Take a look at :h <C-R>)

FDinoff
  • 30,689
  • 5
  • 75
  • 96
  • very great! I had read Learn vi & vim editor but didn't found this! thank you! It works well. – songhir Aug 13 '13 at 04:13
  • @FDinoff I must've fat-fingered the downvote when voting. I can't change my vote unless you edit your answer. If you edit, I'll change it to upvote. Thanks, by the way! – Chris Schmich Aug 22 '14 at 22:11
  • 1
    Any way of putting the text in the cursor's position? – Eduardo Wada Dec 29 '17 at 12:12
  • 3
    To put vim option values to text, you need to add `&` before the option. See an related post [here](https://stackoverflow.com/questions/23604388/how-to-copy-the-value-of-a-vim-option-to-a-register). – jdhao Mar 28 '19 at 12:49
  • To be clear, if mapping, do you recommend to save variable (TodayDate) to a register, and afterwards to `:put` the register. Is that right? – Xopi García Nov 11 '20 at 12:07
3

How about the following?

execute "normal! i" . TodayDate

This will put you in insert mode and insert the content of TodayDate at the cursor position.

joemrt
  • 161
  • 5
  • +1. If textwidth not zero (i.e. inf), then next map save-change and finaly reset it. To not having problems if the string (TodayDate) pass the tw char. `nnoremap zzz \ : let tw_ztc=&tw \ :set tw=0 \ :execute 'normal! o' . str_zzz_01 \ :execute 'normal! o' . str_zzz_02 \ :set tw=&tw_ztc ` – Xopi García Nov 11 '20 at 12:00
  • This way is more computer consuming than the accepted, but it holds auto-indentantion. – Xopi García Nov 11 '20 at 12:08