vim: how to yank only displayed texts in folding

105 views
Skip to first unread message

ping

unread,
May 20, 2012, 5:28:10 AM5/20/12
to vim...@googlegroups.com
[retry]
sorry if I posted answered questions since I haven't found a way out yet
for my issues...

I'm using fold a lot and wondering if there is a way to yank ONLY the
displayed texts?
that is , say I have following folding:

30 hostname "WASHDC core"
31 +-- 34 lines: aaa new-model
65 +-- 2 lines: !
67 service password-encryption
68 baseline show-delta-counts
69 !
70 +-- 3 lines: controller sonet 12/0
73 !
74 +-- 3 lines: controller sonet 13/0
77 !
78 +-- 23 lines: controller sonet 4/0
101 +-- 31 lines: !

normally when you yank a folded line, it actually yank the whole texts
that line folded, not only the displayed title.
but I want to also have the option to do the other way -- just yank the
texts that is getting displayed.

this is kind of another good usage of folding -- generate an overview
that can be passed to other places.


regards
ping

Chris Jones

unread,
May 20, 2012, 7:32:18 AM5/20/12
to vim...@googlegroups.com
On Sun, May 20, 2012 at 05:28:10AM EDT, ping wrote:

> I'm using fold a lot and wondering if there is a way to yank ONLY the
> displayed texts?
> that is , say I have following folding:
>
> 30 hostname "WASHDC core"
> 31 +-- 34 lines: aaa new-model
> 65 +-- 2 lines: !
> 67 service password-encryption
> 68 baseline show-delta-counts
> 69 !
> 70 +-- 3 lines: controller sonet 12/0
> 73 !
> 74 +-- 3 lines: controller sonet 13/0
> 77 !
> 78 +-- 23 lines: controller sonet 4/0
> 101 +-- 31 lines: !
>
> normally when you yank a folded line, it actually yank the whole
> texts that line folded, not only the displayed title.
> but I want to also have the option to do the other way -- just yank
> the texts that is getting displayed.
>
> this is kind of another good usage of folding -- generate an overview
> that can be passed to other places.

I guess you write a function that uses the following Vim functions to
extract your text and append it to a register:

:h getline()
:h line()
:h foldclose()
:h foldtextexpr()
:h setreg()

And then have mapping such as:

:nmap ,y :g/\n/call MyYank()<CR>

.. to scan the entire buffer so that when you return from MyYank() your
outline would be available in one of the A-Z registers.

I believe you'd need to keep track of whether the current line is the
top line of the fold, or you'd end up with the entire fold appended to
the register.. not what you want obviously.

There are probably less kludgy ways to do that, but that's what comes to
mind..

CJ

--
Alex Perez is aliveeeeeeee!!!

Christian Brabandt

unread,
May 20, 2012, 7:34:36 AM5/20/12
to vim...@googlegroups.com
Hi ping!

On So, 20 Mai 2012, ping wrote:

> [retry]
> sorry if I posted answered questions since I haven't found a way out
> yet for my issues...

What was wrong with the answer you received last time?
http://groups.google.com/group/vim_use/msg/dd1baac3622107c0

regards,
Christian
--

Chris Jones

unread,
May 20, 2012, 7:54:22 AM5/20/12
to vim...@googlegroups.com
Maybe that I hadn't joined in.. ;-)

Funny that I don't remember seeing that thread and yet came up with the
premises of pretty much the same solution..

CJ

--
Oh My God!!! Larry is back!

Christian Brabandt

unread,
May 22, 2012, 1:54:57 PM5/22/12
to ping, vim-use Mailingliste
Hi ping!

On Di, 22 Mai 2012, ping wrote:

> can you let me know how to use this function in practice?
> I tried visual mark then :call func..
> it doesn't work.

Please be exact. What did you do? What happened instead? Did you get an
error message?

>
> http://groups.google.com/group/vim_use/msg/dd1baac3622107c0
>
Take the function CopyNonFoldedI() from here:
http://groups.google.com/group/vim_use/msg/571f070987ee35ce
and copy it into a file which you save below ~/.vim/plugin/CopyFolds.vim

Restart Vim and now you can simply call :call CopyNonFolded() and it
will create a new split buffer which contains only the fold data.

regards,
Christian

ping

unread,
May 30, 2012, 2:17:46 PM5/30/12
to vim-use Mailingliste
hi Christan:
yes that works!
thanks!

previously I got it wrong that I visually selected my text and apply
the function with that range.

b.t.w can we actually achieve that goal based on your function here?

"yank only non-folded texts
function CopyNonFolded()
let lnum=1
let buffer=[]
while lnum <= line("$")
    if (foldclosed(lnum) == -1)
        let buffer += getline(lnum, lnum)
        let lnum += 1
    else
        let buffer += [ foldtextresult(lnum) ]
        let lnum = foldclosedend(lnum) + 1
    endif
endwhile
top new
set bt=nofile
call append(".",buffer)
0d_
endfu
"

thanks again for your help!

regards
ping

Christian Brabandt

unread,
May 31, 2012, 1:21:40 AM5/31/12
to vim...@googlegroups.com
On Wed, May 30, 2012 20:17, ping wrote:
> hi Christan:
> yes that works!
> thanks!
>
> previously I got it wrong that I visually selected my text and apply
> the function with that range.
>
> b.t.w can we actually achieve that goal based on your function here?

I think, this should do:

function! CopyNonFolded() range
let lnum= a:firstline
let buffer=[]
while lnum <= a:lastline
if (foldclosed(lnum) == -1)
let buffer += getline(lnum, lnum)
let lnum += 1
else
let buffer += [ foldtextresult(lnum) ]
let lnum = foldclosedend(lnum) + 1
endif
endwhile
top new
set bt=nofile
call append(".",buffer)
0d_
endfu

com! -range=% CopyFolds :<line1>,<line2>call CopyNonFolded()

You can then use the command Copyfolds with or without a selected range
(in which case it selects the complete file).

regards,
Christian

ping

unread,
May 31, 2012, 1:41:06 PM5/31/12
to vim_use
hi chris:
this works!

thanks!

regards
ping
Reply all
Reply to author
Forward
0 new messages