Keyboard shortcuts: Create block {before, after} current block at same indentation level

There should be a keyboard shortcut to create a block before or after the current block, at the same indentation level as the current block, because the current methods to do this are too tedious and error-prone.

This is how these tasks in Logseq would change:

Create block above current block at same indentation level

  • Current method:
    1. Select block
    2. Edit block
    3. Move cursor to beginning of block text
    4. Press enter to create new block above
  • Proposed method:
    1. Keyboard shortcut to create new block above (at same indentation level)

Create block below current block at same indentation level

  • Current method:
    1. Select block
    2. Close if it has children
    3. Press enter to create new block below
    4. Select initial block and open, if closed (to restore previous state)
  • Proposed method:
    1. Keyboard shortcut to create block below (at same indentation level)

If this is too important for you, it is relatively easy to prepare some custom.js for achieving it. Just let me know, as well as the preferable keyboard combination.

1 Like

I think it makes the most sense to assign these keys to the proposed keyboard shortcuts:

  • Proposed keyboard shortcuts:
    • Create block above current block at same indentation level: βŒ₯⏎
    • Create block below current block at same indentation level: βŒ₯β‡§βŽ

(βŒ₯ chosen instead of ⌘, since ⌘⏎ is already claimed)

Given the existing relevant keyboard shortcuts:

  • Existing keyboard shortcuts:

    • Block-related commands:
      • Create new block: ⏎
      • Create new line in current block: β‡§βŽ
      • Move block up: βŒ˜β‡§β†‘
      • Move block down: βŒ˜β‡§β†“
    • All other context-less commands that use the ⏎ key:
      • Rotate the TODO state of the current item: ⌘⏎
  • Keyboard key symbols:

    • ⇧: Shift
    • βŒ₯: Option / Alt
    • ⌘: Command / Control
    • ⏎: Return
1 Like

Add the following code inside file custom.js (restart Logseq and permit to run when asked):

document.addEventListener("keydown", (e)=>{    
    if (e.altKey) {
        if (e.key === "Enter") {
            const blockId = logseq.api.get_current_block().uuid
            const options = {focus: true, sibling: true, before: e.shiftKey}
            logseq.api.insert_block(blockId, "", options)
        }
    }
});
  • For consistency, I have opted for the following shortcuts:
    • βŒ₯⏎ to insert block below
    • βŒ₯β‡§βŽ to insert block above
  • If you still prefer the inverse, just replace e.shiftKey with !e.shiftKey
3 Likes

Thanks so much!

This JavaScript is great for solving this for me, but is there any way this could make it in to the official release? I think a lot of people would find it useful if they found it in the keyboard shortcuts.

(I had actually meant to assign the keyboard shortcuts as you did, I just got them backwards.)

1 Like
  • The team is busy with more fundamental issues.
  • For third-party changes to make it in to the official release, someone has to prepare a so-called Pull Request, along with some tests etc.
    • In other words, unofficial has less bureaucracy, with all its pros and cons.
2 Likes

I would still prefer an UI option for it :slight_smile: … like the (+) already existing option which is only available for when the mouse hovers below the last block of a page…

my 2c :slight_smile:

2 Likes

Thanks for the code! Where is the custom.js file located or how do you create it?

Create it inside subfolder logseq of your graph’s folder.

1 Like

For those like me who don’t know about javascript, you create a regular text file in the Logseq folder inside your vault, paste the code in, and change name and extension to custom.js. Accept the warning prompt when you relaunch Logseq and you’re done.