Support loading Vim commands from configuration (vimrc-style)

got you. Thanks for the info! You are right, The real vim can always be used for code, in obsidian basic functionality for comfortable writing with vim is more than enough.

There are some great vim plug ins for writing, but you are right it’s better to wait for the API to implement these things.

For now I can’t say how happy I am with:

nmap j gj
nmap k gk

That remap is so essential, to the point where vim can be really annoying without it.

Thanks again @esm90

PS: @tallguyjenks vim-easymotion is a killer plug in, it would be amazing to have it in obsidian!

2 Likes

Your quick hack code just converted a guy from one “note taking OS” to Obsidian.

I also avoided Obsidian because the lack of gj and gk key bindings. thank you!

2 Likes

hey @esm90 I’m not sure if it’s just me or if your amazing .obisidian.vimrc stopped working on version 0.9.7

Would it be possible to move this from volcano to the new plugin API?

Thanks for your hard work!

1 Like

I didn’t yet try 0.9.7, but I did read through the alpha plugin API and I’ll definitely make a proper plugin in the next few days.
BTW did you repatch Obsidian with Volcano after the upgrade? One of the practical downsides of Volcano is that after each upgrade you need to run it so it will patch the new version.

3 Likes

awesome, looking forward to see it as a plug in, without your obsidian.vimrc vim mode is just not the same.
Yeah I run volcano again, but it didn’t seem to work, it might be some issue for my side, but I can see volcano slowly fading away now that the API has been released
I’ll keep track of your porject, thanks for your work!

Done! I made a formal plugin that can already be installed manually and created a PR to list it in the community plugins.

5 Likes

Finally I can unmap <C-c> properly. Thank you so much.

2 Likes

A few additional nice-to-haves for Vim support. I’m looking at @esm90 (thanks so much for the plugin!) but I’m not sure if some of these issues are rooted in Obsidian or CodeMirror, or need their own plugins.

  1. It would be nice if nmap <CR> o<Esc> could be added to the .obsidian.vimrc file. This allows you press enter to create a new blank line below the cursor but stay in normal mode. It seems that any attempt to map or remap <CR> is broken, but I don’t know if this a problem with Obsidian or CodeMirror. My current workaround is nmap J o<Esc> and nmap K O<Esc> but it’s not ideal.
  2. I can’t seem to get search and replace macros to work. For example, if I want to clear all checkboxes on a note I have to type :s/\[x\]/[ ], and if I write this into a macro it doesn’t seem to “stick”–the macro executes, and then stops with a blank command line open. I haven’t tried to write this into the .obsidian.vimrc file yet since I can’t get it to work manually.
  3. It would be nice if there was a way to keep the Vim console open for more than 3 seconds. For example, typing :reg to figure out whether that macro recorded.
  4. It would be nice if the .obsidian.vimrc file actually behaved like one and allowed for comments and blank lines–but this does risk a deep rabbit hole of making it a full on parser.
  5. Is there a way to get the vim register to interat with the system clipboard? Ctrl/Cmd + C is okay for now, but we all prefer yy and p

Let me know what you think, and if my amateur coding skills could be of any assistance.

These are interesting and I’ll answer based on what I know:

  1. I don’t know to explain why it doesn’t work because at first glance it seems like CodeMirror should support it. <CR> is a default CodeMirror key mapping that should be possible to override. Very possibly a bug.
  2. I struggled with a similar issue, it seems like CodeMirror macros can’t execute searches (they open a search box instead of searching what’s in the macro). Maybe we should open them a bug (or even better, try to fix it?)
  3. That duration is hard-coded in CodeMirror, see duration: 5000 in showConfirm.
  4. Comments and blank lines are very easy to add, I’ll do that if it’s a wanted feature.
  5. I started looking into that one a few days ago and am thinking to add support for it by overriding the CodeMirror yank function (together with a set clipboard vimrc setting). I also started looking into another thing that I really want, which are marks that work across files. Hopefully I’ll get to these.
1 Like

I would appreciate point 4.
I already achieved point 5 with this script(https://github.com/ianhi/jupyterlab_vim-system-clipboard-support) in my own private plugin. Just as a tip.

1 Like

thanks @Vinadon for sharing your script, could you provide some instructions for setting it up for Obsidian if possible?
also, does it work for linux manjaro?

It is not my script, I found in in the internet, but because it is codemirror, you can use it in obsidian as well. I didnt make a public plugin, because I am not sure with the rights and so on with this script.
To use it you have to

  1. create you own plugin with this template
  2. put the yank.ts in your plugin folder
  3. register it with
this.registerEvent(this.app.on('codemirror', (cm: CodeMirror.Editor) => {
     CodeMirror.Vim.defineOperator("yank", yankGenerator(CodeMirror.Vim.getRegisterController(), true))
}));

somewhere in your main.ts

Honestly I dont know what the last argument stands for, I noticed no differene between true and false, but it works. EDIT: I found some info, but I guess true is the right choice.

It will throw some types error, because it cant find Vim and so on, but I guess it is just not well documentated.

To make it accessible for everybody, do you mind opening a PR to add this to the Vimrc plugin? I’d add a custom option (CodeMirror.Vim.defineOption) named clipboard to make it configurable similarly to Vim.
If you don’t get to it I’ll do it in the next few days.

1 Like

What do you mean exactly with?

I’d add a custom option (CodeMirror.Vim.defineOption) named clipboard to make it configurable similarly to Vim.

Do you want that it is configurable with the .vimrc file? What options and syntax do you have in mind?

In addition, should I ask the author of the originial script if is ok to use it?

it would be amazing @Vinadon and @esm90 to have this as part of obsidian.vimrc looking forward to it, thanks!

I was thinking to bind the yank register to the system clipboard using a set clipboard=unnamed.
IMHO those two lines you copied are too trivial to ask permission for, but it’s up to you. If you don’t feel comfortable, some time next week I’ll investigate this from scratch without using this snippet.

1 Like

I tried it myself, but I dont understand the error messages and so on. I am just not comfortable with codemirror. Happy to see what you will achieve when you find the time for it.

1 Like

You may go ahead and update, I added the support for copying to system clipboard (add set clipboard=unnamed to your vimrc), and also the promised support for vimrc comments & blank lines.

@Vinadon I eventually chose a different approach than what they did at Jupyterlab. Over there they essentially re-implemented the yank command from scratch because CM doesn’t expose it via the API. But I think that’s not so future-proof (e.g. may not work well on future versions of CM), so instead of that, I added code that monitors the CM Vim yank register and copies it to the system clipboard when a change is detected (if configured to do so).

2 Likes

I can confirm that it works great on linux, thanks a lot @esm90 it’s such a great time saving feature, really appreciate it!

Thank you. Yeah thats a better implementation. I didnt know you can listen to it.