Monday, February 21, 2011

Re: Including into the current buffer from an external source

On Feb 21, 2:29 am, Christian Brabandt <cbli...@256bit.org> wrote:
> 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
>

I noted a little while ago that readfile() accepts a maximum number of
lines to read in. If you use this argument, you could make this
function more efficient for retrieving lines near the top of big
inputs.

--
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: