The marks in vim knows the deletion and insertion of lines before it and their positions will keep updated. Is there a way to achieve these pragmatically?

I am running an async task which is supposed to update the current line when the task finishes. Here the current line means the cursor line at the time the task is launched. After launching it, I may need to move the cursor around; make modifications to the buffer. How can I get the correct position when the task finishes without using a mark? If marks are used, they may be accidentally reset by user or by other third party plugins.


example

Given the text:

line1
line2
fix me please
line4
line5

Assume my cursor is now at line 3, I am going to run :Fixme which is to replace fix me please with line3 but at a much later time.

If during this waiting period, I delete line1 or inserte more lines before line 3, I hope :Fixme is aware of this change and still updates the line fix me please. How to achieve this?

  • when you launch your task, store the current cursor position in an extra variable. – Christian Brabandt Nov 8 at 6:46
  • Thank you @ChristianBrabandt. I added an example there. Is there any way to achieve that? – Liu Sha Nov 8 at 6:52
  • It depends on your async task. When calling the async job, you should store the current cursor line number. However, that won't work when the line above has been deleted, than those will be off. – Christian Brabandt Nov 8 at 6:59
  • 1
    That is not possible. adjusting the marks is done internally by Vim. As far as I know, there is no other possibility to have the line numbers adjust automatically. (BTW: You can always restore the previous mark if it was set). – Christian Brabandt Nov 8 at 7:10
  • 1
    Sure, you can request it. By coincidence, Bram just asked what specifically plugin authors need. You can mention your request there (or create a new wishlist ticket). I cannot promise that this will be implemented soon however. – Christian Brabandt Nov 8 at 7:27
up vote 2 down vote accepted

Currently, only marks are automatically adapted to changes in the buffer by Vim. You can record arbitrary positions via getpos(), but these would have to be adapted to changes on your own (and that is not generally possible if you have no control over the changes that are done).

Plugins usually "reserve" some marks for their use, either by documenting it ("this mapping uses mark z"), or by taking unused marks (cp. ingo#plugin#marks#Reserve() in my ingo-library plugin).

It indeed would be great if there were a low-level function to use "unnamed marks" for plugins, similar to the distinction between :match (user) and matchadd() (plugins). You can raise this idea in this poll.

  • thank you very much for explaining this to me and also suggesting the way to go. I will draft a proposal later. – Liu Sha Nov 8 at 8:30

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.