obsidian-vimrc-support icon indicating copy to clipboard operation
obsidian-vimrc-support copied to clipboard

vim with [[double brackets]]

Open santiyounger opened this issue 4 years ago • 22 comments
trafficstars

I was just about to ask if this is possible, and I found this in the ReadMe

Implement some standard vim-markdown motions for Obsidian, e.g. [[

This would be an amazing addition, since it's one of the only thing that makes using vim in Obsidian slower than without it.

Is this a very challenging feature to add? I'd love to know more about it.

In the mid-time, do you have any workarounds, I'm happy to try out different stuff to get this to work somehow.

Thanks!

santiyounger avatar Apr 01 '21 21:04 santiyounger

Can you elaborate a little more on what is the feature that you mean? What I meant in that Readme item is to add motions to jump to the next Markdown header etc. That's not hard to do at all and if I get the impression people besides me would find it useful, I'll spend the time on it. But what I implicitly understand from your title is that there are other issues that relate specifically to the [[double brackets]] link notion?

esm7 avatar Apr 02 '21 14:04 esm7

Hey, thanks for clearing it up. I was wrong to assume what you meant in the ReadMe. As you just described it "add motions to jump to the next Markdown header etc." is definitely something I've been interested as well.

However what I means here was something different, and by accident I assumed that's what you meant in the README, in that case let me explain.

When vim mode is off

we can go to settings > editor > Auto pair brackets (on) This allows us to press [[ to surround some text with [[brackets]]

By default we can't do this when vim is on (I always have vim on) so I'd love to be able to achieve the same.

Here's a video illustrate what I mean

https://www.loom.com/share/8cc9b9e012fb4ba4bb1d9b30e58c0ac2

perhaps there's something that could be added to the vimrc to achieve this.

Thanks a lot!

santiyounger avatar Apr 02 '21 20:04 santiyounger

Seems like when in Insert mode, [[ works as you describe for Vim being off, i.e. if you mark text in Insert mode and type [[ it surrounds it. In Normal mode however, AFAIK there is currently no way to map [[ (or any other combination in that regard) to surround the text. In theory you should be able to use vmap to set a combination that would surround the selection with [[...]] in Visual mode, but I couldn't get it to work... I never tried using vmap in CodeMirror before, maybe it's broken? Or maybe some more persistence in this direction can work. Beyond that, I see two options:

  1. Implement optional functionality similar to vim-surround, e.g. great shortcuts like ysw] to surround a word, or S] to surround in Visual mode. I'd love to have that but it's quite a task!
  2. Find a way to overcome the CodeMirror limitations in saving macros as keymaps. If we could save a macro in the Vimrc and then map it to a key, it would solve this problem and many others. I think I'll spend some time in this direction when I get a few hours to work on the plugin.

esm7 avatar Apr 05 '21 10:04 esm7

thanks a lot for looking further into it! I really appreciate it since it's the only reason I ever have to turn vim mode for. I know what you means with vim surround, that'd be pretty good, but a bit less accessible for some starting out in vim. However if that's the best way to go, I'd fully support that way to go.

If the CodeMirror mirror limitations can be solved that sounds like the best way forward.

Thank you so much for taking a look at this, I'll keep an eye for this issue, feel free to update me here, this would be extremly amazing!!

santiyounger avatar Apr 05 '21 17:04 santiyounger

I just released a version that adds an obcommand Ex command, which in theory should enable what you asked for. However to my disappointment, launching the editor:insert-link or editor:insert-wikilink Obsidian commands on a selected text doesn't work as expected and outputs an error coming from Obsidian ("Cannot read property 'path' of null") :disappointed:

esm7 avatar May 19 '21 19:05 esm7

thanks @esm7 for the awesome update, I'll be testing it out and get back to you if needed.

launching the editor:insert-link or editor:insert-wikilink Obsidian commands on a selected text doesn't work as expected and outputs an error coming from Obsidian ("Cannot read property 'path' of null") 😞

What was it that wasn't possible sorry, I'm not sure I understand.

Does this mean that it's not possible to achieve what I showed in this video?

https://www.loom.com/share/8cc9b9e012fb4ba4bb1d9b30e58c0ac2

santiyounger avatar May 19 '21 20:05 santiyounger

The capability I added to the plugin is to launch an arbitrary Obsidian command (anything from the command palette basically) through an Ex command, which in turn can be mapped to Vim shortcuts. I was hoping this will solve the use case that you wanted, because turning text into a [[...]] link is an Obsidian command. But for some reason launching this command on text selected via the Vim mode results in some error coming from Obsidian :(

esm7 avatar May 20 '21 10:05 esm7

I was hoping this will solve the use case that you wanted, because turning text into a [[...]] link is an Obsidian command. But for some reason launching this command on text selected via the Vim mode results in some error coming from Obsidian :(

Got you, that's a shame, do you think this is something we can make a feature request for in the Obsidian Forum. Or is it more of a code-mirror limitation? Let me know if there's anything I can do to help

Thanks a lot!

santiyounger avatar May 21 '21 01:05 santiyounger

I am wanting this as well. The quickest way I do this now, is go to insert mode and use the mouse (lol) to double click the word and while it is highlighted, I can press the bracket [ twice and then escape to normal mode. This is unfortunate because I do not want to use the mouse, but maybe there is a easy (keyboard only) way to highlight the word from the cursor in insert mode? Perhaps we can get one step closer?

CodeAmend avatar Jun 12 '21 17:06 CodeAmend

I tried a few variations of Visual mode and some mappings with no results yet, but I will continue to investigate this next time I get a chance.

esm7 avatar Jun 13 '21 06:06 esm7

I am wanting this as well. The quickest way I do this now, is go to insert mode and use the mouse (lol) to double click the word and while it is highlighted.

Thanks for the workaround, I can't believe I never tried this, I was always just adding the [[ manually with vim shortcuts.

Like you say it'd be great to have a shortcut to achieve this, but in the mid-time thanks for the simple workaround

santiyounger avatar Jun 13 '21 14:06 santiyounger

I'm working on a small command that allows surrounding your selected text in visual mode with characters. See https://github.com/Andr3wD/obsidian-vimrc-support/commit/0636e2772411c4826694d7f900f282659a1f121a if interested. I'll make a PR sometime soon.

Andr3wD avatar Jul 15 '21 05:07 Andr3wD

That's awesome, and if you can make it similar to vim-surround, i.e. in visual mode type S{ to wrap the selected text with {...}, S( for (...) etc., it would be a hit :sunglasses:

esm7 avatar Jul 15 '21 07:07 esm7

thanks @Andr3wD and @esm7 . If you guys merge that PR please let me know. This is a feature I'd truly appreciate. Thanks

santiyounger avatar Jul 19 '21 02:07 santiyounger

I've spent some more time trying to get it to act like vim-surround, but it seems like CodeMirror.Vim doesn't allow unmapping or overwriting of main actions, such as y, s, etc... If anyone knows anything about getting around this, then please help me out. For now I'll clean up my original ex command and update my PR.

Edit: Code has been updated and PR is ready for review. Just waiting for @esm7 to review.

Andr3wD avatar Jul 20 '21 17:07 Andr3wD

@Andr3wD thank you so much for the contributions! I'm a little swamped this week but really hoping to merge and release it next week :)

esm7 avatar Jul 22 '21 12:07 esm7

I've spent more time looking into custom actions and I was able to recreate one vim-surround keybind for adding surrounding text by using the CodeMirror dialog addon. See https://github.com/Andr3wD/obsidian-vimrc-support/commit/bbdde52e75c40dbe75fe6c530e7239c658e03243 for changes. I'll look into implementing deleting and changing later. Additionally, for now it seems that a ys, cs, etc... keybind won't work because y, c, etc... are already bound to actions, and I don't want to overwrite those. So for now I'm using Alt-y instead of y. Also, no worries @esm7, take your time.

Andr3wD avatar Jul 22 '21 23:07 Andr3wD

Thank you folks for working on this. Being able to surround a selection with double brackets from visual mode would be fantastic.

pooriajr avatar Jul 23 '21 00:07 pooriajr

Released the version with this feature and it works beautifully :)

esm7 avatar Jul 29 '21 18:07 esm7

Actually I was happy too soon, something got broken :-/ Will look into it. EDIT: seems to now be working for real.

esm7 avatar Jul 29 '21 18:07 esm7

@santiyounger can you confirm the feature works for you and we can close the issue?

esm7 avatar Aug 01 '21 17:08 esm7

@esm7 I am using this feature and it is fantastic. I came to your plugin hoping I could implement this by implementing a form of surround and was delighted when I saw you had already added support for this.

Thank you so much!

brandonkboswell avatar Aug 19 '21 04:08 brandonkboswell

@santiyounger is MIA. Can probably close it as it seems to work

huyz avatar Nov 07 '22 18:11 huyz

hey, apologies I haven't checked github in ages. But yes I can confirm now that this works great! Thanks a lot guys!

santiyounger avatar Nov 09 '22 18:11 santiyounger