Enable Vim Bindings to Pass Over Folded Sections

Use case or problem

I want to take advantage of folding more often to help navigate my notes and focus on certain parts. I exclusively use Vim emulation and Live Preview in Obsidian, and while this works well for the most part with just a couple quirks (see Go To Link Command Inconsistencies with Cursor Position, Live Preview, and Vim Mode), one quirk prevents folds from being very useful to me.

Most commonly when I am navigating my note, I use { and }. These keys jump from one blank line to the next or previous, jumping over entire blocks at a time. My hope is that I could use this to jump over the heading of a folded section. However, the Vim navigation has no “awareness” of the fold, and will jump to the next blank line inside the folded section, automatically expanding the entire thing.

Proposed solution

Moving the cursor with Vim should not automatically expand the section. If I want to expand, I will explicitly click the fold arrow, or use the unfold hotkey.

Additionally, Vim bindings should have some “awareness” of the sections that are folded, treating them like a single line. Moving the cursor up and down should go onto the heading line and past it without unfolding. Using the { and } should jump over the heading line with no regard for the content that is inside the folded section.

Another handy use case is when I put my cursor on the folded heading, and I want to add one below. So I press o to open a line below, but this unfolds the section and inserts the line directly below the heading.

Current workaround (optional)

Current workaround is to avoid using folds entirely, to use the mouse to click on the “other side” of a fold (which is antithetical to using Vim bindings in the first place), or to use some Vim motion that is guaranteed to not enter the folded section (eg. going directly to a line number on the other side, which is not practical).

I simply don’t use folds because they can’t be effective to me with this behavior.

5 Likes
  • At this point, it’s an issue.
  • You should go to github and Reddit with this issue.

You can address this via the vimrc support plugin and a custom js function to handle up/down nav.

The solution is described here:

You’ll need to (also described in that plugin’s readme)

  • install vimrc plugin
  • enable js function support (plugin settings page)
  • add the JS functions file to your vault and include the functions I added in the pull req
  • add the corresponding vim mappings to your .obsidian.vimrc
1 Like

what file? where to find it? if it is not there, how to create it?

what functions? Non-programmer here. what exact text to include?

I read it is dangerous to do this. what are the dangers? how do be safe?

It works pretty well. The next issue is now the inability to use numbers in combination with j and k, for jumping multiple lines, and probably a bunch of other combos that use j and k.


@man_x, for your questions

you need to create a mdHelpers.js file and place it at the root obsidian vault folder with the following code

function moveUpSkipFold() {
  view.editor.exec('goUp');
}
function moveDownSkipFold() {
  view.editor.exec('goDown');
}

Add the following to your .obsidian.vimrc

" Fix for unfolding of folded lines when curor is hovered over
exmap upSkipFold jsfile mdHelpers.js {moveUpSkipFold()}
exmap downSkipFold jsfile mdHelpers.js {moveDownSkipFold()}
nmap k :upSkipFold
nmap j :downSkipFold