Select whole block with Ctrl+a

image
Should be support for both inline code block and code block
Thanks!

1 Like

Came here for this feature request. To elaborate a bit:

Suppose I’m editing an JSON embedded in a Markdown document:

Blah blah, something something.

Here goes my JSON:
```json
{ <- Obsidian cursor is here; pressing a hotkey would select all text inside of this code block
    "a": 1, 
    "b": 2
}
```

<%*
let editor = app.workspace.activeLeaf.view.editor;
let scroll=editor.getScrollInfo();
//检索当前页面中所有的行 check all line find begin with
let linenums=[]
for(let i=0;i<=editor.lastLine();i++){
if(editor.getLine(i).substring(0,3)===‘```’)
linenums.push(i);
}
// 检查出所有代码块的起始和结束位置 find all code block begin and end line
let blocks=[]
for (let n in linenums){
if(n%2==0){
blocks.push({start:linenums[n]})
}
else{
let block=blocks[blocks.length-1]
block.end=linenums[n]
}
}
let position = editor.getCursor();
let thisLineNumber=position.line;
let inCodeBlock=false;
let inBlock=null;
//判断当前光标是否在一个代码块中 check cursor is in a code block
for(let block of blocks){
if(block.start<=thisLineNumber && block.end>=thisLineNumber){
inCodeBlock=true;
inBlock=block;
break;
}
}
if(inCodeBlock){
//在代码块中 in a code block
editor.setSelection({line:inBlock.start+1,ch:0},{line:inBlock.end-1,ch:editor.getLine(inBlock.end-1).length});
}
else{
//不在代码块中 not in a code block
// editor.setSelection({line:0,ch:0},{line:editor.lastLine(),ch:editor.getLine(editor.lastLine()).length});
}
return
%>
put this in a md,and use templater hotkey
if you bind ctrl+a it will replace default ctrl+a, you can uncomment code in else,but after run editor.setSelection,the page scroll will change,and i can’t find any way to fix it.

+1.

Otherwise user have to select all codes manually, which is really time consuming for longer code blocks.

+1. It’s so useful especially selecting a long code block in a note.

+1. Really missing this feature, which was/is available in Typora.