Simple way to copy inline code

The new version of Obsidian allows to copy code in fenced code blocks to the clipboard, which is very useful. Sometimes, I also want to copy inline code (not blocks). It would be nice if that would be possible in a similarly easy way. Currently, it is cumbersome, you have to select the text carefully with the mouse, or when you click on “Copy” in the context menu, you need to remove the backticks. A simple solution would be to add a “Copy plain” menu entry to the context menu that only copies the inner code without the backticks. Or maybe make that the normal behavior if you click “Copy”.

23 Likes

I need this feature too.

I usually backup some command line code as inline code, it would be convenient if the copy of inline code can be done by simpler operation.

3 Likes

+1. Logseq has this.

Duplicate request in the bug graveyard

Are we holding it wrong?

I think it doesn’t make sense to include the backticks:
It makes it completetly unusable for comfortable code copying.
In case it’s a shell command the effect is that the command is executed and it’s output then tried to be executed as a command, too! That’s rarely what you want.
And as part of source code it’s also very hindering.

But it has been implemented to include backticks by default.

I wonder why this was chosen?
What is the usecase for the backticks?
How does it compare to the one of code copying?

1 Like

Registered to support this future request. Very needed indeed.

2 Likes

+1 I would also like this function.
It would be great if the inline code could be copied automatically when you click over it at least in Reading mode, like in Telegram.

2 Likes

+1 to this feature.
Single-click copy, like Telegram has with monospace formatting, would be awesome for storing little code snippets.

2 Likes

Logseq doesn’t have this. When selecting code formatted within backticks, it enters edit mode and starts the selection exactly under the cursor. Assuming a right-to-left movement, this is often just before the closing backtick, as it appears to the right of the cursor.

LS2

While this is better than Obsidian, it is still not good because you still have to carefully drag the cursor instead of just double-clicking.

One way to implement this would be a “Copy without Formatting” akin to “Paste without Formatting” (funnily named “Paste and Match Style”):

Use Ctrl + Shift + C to copy the selection without enclosing backticks.

This keeps Copying via Ctrl + C as is – copies exactly what is selected – and gives clear implementation criteria for when to strip backticks.

I also think this issue should be solved not for Editor mode, but for Preview mode. I would like to compare with Telegram, which also supports a subset of Markdown. In Telegram, when you send message with inline code, it becomes copy-able by a single click/tap on the block. I think this behavior would fit Obsidian too. And if you want to copy with backticks (and other formatting) – select text with mouse and hit Ctrl + C – exactly as it is right now.

1 Like

It has been a while but I want to notify you that a simple plugin has been developed. It has some limitations but it works for now.

Excellent solution, @JamesLemony This plugin finally makes inline code useful to me.

1 Like

Just figured out a new way to fast copy inline code.

Preview:
f1a72194-2c9b-4c7e-8312-e8175f6190ee

Save this code into a md file and use it as Templater Plugin’s template:

<%*
// 2024.06.05 增加了对行内代码 inline code 的兼容

function getInlineCode(str, cursor){
    let start = str.lastIndexOf('`', cursor - 1);
    let end = str.indexOf('`', cursor);

    if (start === -1 || end === -1) {
        return null;
    }

    return str.substring(start + 1, end);
}

async function linkHeading() {
  const curView = app.workspace.activeLeaf.view;
  const curFile = curView.file;
  const curEditor = curView.editor;
  const curLine = curEditor.getLine(curEditor.getCursor().line);

  if (!curLine.startsWith('#') || !curLine.includes('# ')) {
    if (!curLine.includes("`")) {
      new Notice("当前光标处没有标题");
      return;
    } else {
      const curCh = curEditor.getCursor().ch;
      let inlineCode = getInlineCode(curLine, curCh)
      if (inlineCode) {
        navigator.clipboard.writeText(inlineCode)
        new Notice("Inline Code Copied!");
        return;
      }
    
    }
    
  }

  let selectedHeading = curLine.replace(/#+ /, "#")
  const filename = curFile.name;
  let linkAlias = selectedHeading.replace(/# ?/, "")

  let headingReferenceLink = `[[${filename}${selectedHeading}|${linkAlias}]]`

  navigator.clipboard.writeText(headingReferenceLink)
  new Notice("Heading Copied!");
}

await linkHeading();
%>

Then just call this TP with cursor inside Inline code.

I use Commander Plugin to add this TP command into Right Mouse Menu, but you can choose whatever way you like ; )

(Initially it’s used to easily copy Heading, just add the new function to be capable to copy inline code, too)

2 Likes