Stanimirovv
Stanimirovv

Reputation: 3172

vim "put" to paste on same line

In my function I want to paste some generated text there at the position of the cursor (in insert mode). I am using put to do so, however it pastes it on a new line. Is there a way to make put paste it on the same line? If not what is the proper command ?

Upvotes: 2

Views: 2565

Answers (2)

René Nyffenegger
René Nyffenegger

Reputation: 40603

Where is the text to be inserted stored?

Edit

So, per your your comment, the text is stored in a variable. Assuming, the variable is g:text, then, while in insert mode, you press ctrl-r followed by = followed by g:text.

See also help i_CTRL-R_=


I leave the old answer here. Maybe someone else comes accros and finds it useful:

Assuming, it's in a register (as per your mentening :put), you press CTRL-R and then register name (for example a) - (while being in insert mode).

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172758

You cannot use :put, it will always use a new line. You have the following options:

  1. Issue a normal-mode command to insert the text: :execute "normal! amy text\<Esc>". This needs :execute to properly leave insert mode.
  2. Assign your text to a register: :let @@ = 'my text' Then, use :normal! p (or P, optionally with prepended motions to position the cursor). Alternatively, use the expression register: :execute "normal! \"='my text'\<CR>"
  3. From insert mode, you can also use <C-r>{register} or <C-r>={expr}<CR>.
  4. Use :setline(), splicing in the new contents into the existing (getline()) line with the help of strpart().

The first ones are easier, and usually what you want. The last is using a lower-level API, therefore more involved to use, but it doesn't trigger any messages, clobbers register contents, creates a separate change for undo, etc.

Upvotes: 6

Related Questions