Monday, February 21, 2011

Re: Including into the current buffer from an external source

Hi Rostyslaw!

On Sa, 19 Feb 2011, Rostyslaw Lewyckyj wrote:

> Christian Brabandt wrote:
>> Are you looking for the :r command? You can input either another file
>> or even read in the ouput of another command.
>>
>> See
>> :h :read
>> :h :read!
>>
>>
> Well sort of. :read inserts the whole file after the current line.
> But my memory insists that there is/was a way to select a specified
> range of records from the source file and insert them at a specific
> place in the current file. For example: Insert records 25 through 50
> from file source.txt after record 101 of the current buffer.

Not that I know of. But you can easily script it yourself.

com! -bang -nargs=* -range -complete=file Read :<line1>,<line2>call
\<sid>Read(<q-bang>, <f-args>)

fun! <sid>Read(bang, cmd, ...) range
if a:0 == 0
let args = [0,-1]
else
if (a:1 !~ '^\d\+$' && a:1 !~ '^\d\+$') || len(a:000) > 2
echohl Error
echom "Usage: Read[!] file|command [0 ...]"
echohl Normal
return
else
let args = [(a:1 - 1), (a:2 - 1)]
endif
endif
if a:bang == '!'
let a=split(system(a:cmd), "\n")
else
let a=readfile(a:cmd)
endif
let _c = getpos('.')
exe append(a:firstline, a[args[0] : args[1]])
call setpos('.', _c)
endfun

Use :[count]Read filename 1 100
to input the first 100 records below line [count] (default current line)

Use :[count]Read! command 20 50
to input the records 20 till 50 below line [count]

If you leave out the last 2 parameters, it will input all records found).

regards,
Christian

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment