Yank or delete backlink with double brackets in vim normal mode

What I’m trying to do

I want to yank or delete the backlink with its double brackets in vim normal mode with one command.

Things I have tried

  • ya[ - it only grab one bracket with the text in backlink
  • y2a[ - it works in vs code but unfortunately not in obsidian
  • yaW - can’t deal with names that have space in them, like [[cursor navigation - vim]]
  • vf]f] - the cursor should locate at the start of open bracket, and it is actually didn’t behave correctly in my test. (It ignore the second closed bracket and jump to second backlink’s first closed bracket)

Try :s/\[\[[^\]]+\]\]// – it doesn’t matter where you cursor is on that line. :wink:

There is another, more efficient, way of selecting the “outer” WikiLink:

  • Put your cursor before the object you want to yank or delete: you can, e.g., use 0 or ^ in normal mode to go to the beginning of the line,
  • and use the following command sequence in normal mode va[ to select it.

That’s the fastest way I can currently think of.

If you have several WikiLinks on one line:

  • go to the beginning of the line as mentioned above,
  • hit va[ then Esc then l to step away from the WikiLink and (rinse and) repeat.

Or you can start from the end of the line and hit va] etc.

Look at https://github.com/esm7/obsidian-vimrc-support/blob/master/JsSnippets.md#vimwiki-like-link-navigation if you want to quickly navigate from one link to the next. There is even a plugin for that: jdluque/next-link (github.com).

1 Like

Thank you very much for the suggestion; it’s really useful. Now I think I have finally figured out how to ensure that the text object a[ works normally. I need to make sure that the cursor position is right at the beginning of the wiki-link, so that it could include two brackets. Therefore, I have to move my cursor first with 0, ^, b or F[. You’ve helped me clarify this vague notion.

I just got into the Vim world, and I am really loving it. Also looking forward to exploring Vimrc customization once I am familiar with the basic motion and operator.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.