1

I would like to add a new line in a certain line in a buffer. So far my starting position is as follows:

let newl = "\n"
" set cursor to beginning of buffer
call cursor(1,1)
" search up to 10 lines down
search("somestring",'',10)
" get the line number of the match
let cursor_pos = getpos(".")
let cursor_line = cursor_pos[1]
" put a newline directly behind it
(cursor_line+1)put =newl

Now, I cannot combine a variable with put like I would with a number: 11put. How can I accomplish that?

The second problem is rather of a aesthetic nature: When using something like 11put ="\n" Vim behaves weird, it somehow prints out a value of a variable that has nothing to do with a newline.

Is there a way to get the task done in an overall better way - putting a newline at a certain position?

2

What about this?

/somestring
normal! o

If you want to have it dynamic, take a look at :exe.

Or:

:call append(cursor_line, '')
  • I think execute is what he's getting at. An example that might work for you: exe (cursor_line+1) . "put = newl" – Tumbler41 Mar 20 '17 at 21:25
  • That example did help! – d-nnis Mar 20 '17 at 23:43
1

Use the blackhole register, "_ with :put

:99put_

Or use an expression register, "=, with an array of empty strings:

:99put=['']
:99put=repeat([''], 3)

If you want to put a specific string on a line:

:let str = 'hello world'
:99put=str
  • Interessting those register. <Falling into a blackhole about :help registers> ;-). So many things to see and learn... – d-nnis Mar 20 '17 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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