Ben,
Hopefully this doesn't get sent twice, but it seems that my last response was lost in the ether.
I don't think that those functions help. Here is a simplified version of my problem.
Say we want to convert these lines:
line1
line2
line3
Into this:
line1
some
appended
lines
line2
some
appended
lines
line3
some
appended
lines
My first attempt at the function looks like this:
function! Example()
call append(line('.'), ["some", "appended", "lines"])
endfunction
The problem is that this won't work for ranges.
If you try this command:
1,3call Example()
You get this output:
line1
some
some
some
appended
lines
appended
lines
appended
lines
line2
line3
The problem is that range iterates over line numbers, but I'm changing the content of those lines.
The solution that I came up with that works with ranges is this:
function! Example() range
let num_lines = a:lastline - a:firstline + 1
for linenum in range(a:firstline, a:firstline + (num_lines - 1) * 4, 4)
call append(linenum, ["some", "appended", "lines"])
endfor
endfunction
It works, but is by no means elegant. I have to specify the number my start and end lines and how much to stride by.
This is why using substitute, as John suggests, is probably the best option, assuming that things don't get too complicated.
Hope this explains things,
Eric
On Thu, Aug 8, 2013 at 8:21 AM, Ben Fritz <fritzophrenic@gmail.com> wrote:
On Wednesday, August 7, 2013 8:58:55 PM UTC-5, Eric Siegel wrote:I'm not sure whether you still need the answer to this question, but:
> My coworker said that in emacs he's able to get the current cursor position. In vim we can get the current line '.', but if we could get the cursor position as the range iterates, we wouldn't have to worry about the number of lines appended.
>
:help getpos()
Also:
:help line()
:help col()
:help virtcol()
Any of these functions can return information about the cursor position.
Maybe you already know about these and I misunderstand your question? You mentioned using '.' to get the current line, by which you might mean line('.'), but if that's the case I'm not sure what your problem is because that will give you the cursor line.
--
--
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
---
You received this message because you are subscribed to a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/BSej2dwukdA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
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
---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment