triggering macro on navigation to new line

10 views
Skip to first unread message

Ed Peschko

unread,
May 13, 2019, 1:04:05 PM5/13/19
to vim...@googlegroups.com
All,

I just found :set nolazyredraw, which is *very* nice in speeding up
user interaction time, and which helps immensely with my goal of a
quickfix-like editing mode (see the post that I made earlier), so the
only thing left is if there is a way to trigger an action when you go
to a new line, whether you:

1. search for a pattern
2. do :<lineno>
3. hit j or k, up-arrow, down-arrow

I suppose I could re-map each of these actions but IMO it would be a
lot cleaner if something universal existed. Does vim have something
like this?

thanks much again,

Gary Johnson

unread,
May 13, 2019, 2:11:11 PM5/13/19
to vim...@googlegroups.com
The CursorMoved autocommand event may do what you want. See:

:help CursorMoved

Regards,
Gary

Ed Peschko

unread,
May 13, 2019, 3:11:11 PM5/13/19
to vim...@googlegroups.com
Gary,

thanks for this tip, yes it looks like I could use it to do what I
want but my concern is speed. I don't want to trigger a potentially
expensive macro each time the cursor moves anytime, just when the line
number changes.

So perhaps there is a need for CursorLineMoved? I see in vim where I
might be added, but I haven't contributed patches to vim before, so
i'm not sure on how to go about it.

Ed
> --
> --
> 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+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20190513181120.GA12153%40phoenix.
> For more options, visit https://groups.google.com/d/optout.

Gary Johnson

unread,
May 13, 2019, 3:52:19 PM5/13/19
to vim...@googlegroups.com
On 2019-05-13, Ed Peschko wrote:
> Gary,
>
> thanks for this tip, yes it looks like I could use it to do what I
> want but my concern is speed. I don't want to trigger a potentially
> expensive macro each time the cursor moves anytime, just when the line
> number changes.
>
> So perhaps there is a need for CursorLineMoved? I see in vim where I
> might be added, but I haven't contributed patches to vim before, so
> i'm not sure on how to go about it.

I don't think we need Yet Another Event for this special case. The
usual approach to this sort of problem is to make an inexpensive
test early in the process so that you don't spend time considering
cases you don't care about. In this case, you could cache the last
cursor line location and test against that early in the
autocommand command, perhaps something like this:

:let g:prev_line = 0
:au CursorMoved if line('.') != g:prev_line
\ | let g:prev_line = line('.')
\ | " other stuff
\ | endif

Note that you could have just jumped to the same line number in
a different buffer, so you should test the buffer number, too,
perhaps with getcurpos() instead of line() to get both the buffer
number and the line number.

Regards,
Gary

Ed Peschko

unread,
May 13, 2019, 6:09:20 PM5/13/19
to vim...@googlegroups.com
Gary,

Yes I was thinking of that - a special check - but any perceptible
delay on the part of vim for what is such a common operation
(basically any operation) really goes against what I'm trying to get
at here usability wise. I really want this to be as close to a zero
cost operation as possible.

Plus there is always the cost of eval. I'm not sure if vim saves
internal vimscript compiles of a given macro for future evals of the
same macro. How much of a startup time would those checks impose? And
my ultimate language of choice would either be perl or python so
people could configure it without knowing vim regular expressions, and
i'm not sure if vim stores perl/python as bytecode either, or
(shudder) actually executes a subprocess.

In any case I'll play around with it and thanks for all the info.
> --
> --
> 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+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20190513195231.GC12153%40phoenix.
Reply all
Reply to author
Forward
0 new messages