0

How to exclude selecting headers in a file when ctrl a select all or ctrl home go to beginning of a file in vscode?

eg:

Given a markdown file

<link href='G:/Using/mdCss/style.css' rel='stylesheet' />
<link href='./style.css' rel='stylesheet' />
<!-- some random header info -->

This is the content.
Some random sentences.

Current behavior:

press ctrl a

you select all of the lines.

Expected behavior:

Do some configuration.

eg: put a comment as separator.

<link href='G:/Using/mdCss/style.css' rel='stylesheet' />
<link href='./style.css' rel='stylesheet' />
<!-- some random header info -->
<!-- ctrl a dont select above -->

This is the content.
Some random sentences.

press ctrl a

you will only select:

This is the content.
Some random sentences.

In other words, I want to have a keybinding to select everything in a file except some starting lines that terminate with a well-known string.

Is it possible?

  • Same logic for ctrl home.
  • Not just for md, also for html, or other programming languages.
4
  • 1
    Sorry, your request is not sensible. Those are not "headers" in any definition of the word. Markdown can and often does include HTML tags. Ctrl-A means "select ALL". Commented May 28, 2024 at 23:47
  • @TimRoberts Yes, I understand that, but is there a way to assign a new command or hotkey for my special behavior? \ For the terminology "header" I cant find a better word to describe it (I can only think of tampermonkey / UserCss use that terminology for "special text at the beginning of the file"). Commented May 28, 2024 at 23:48
  • Select All does exactly that - it selects ALL. There is no key mapping available for Select All except this part. Likewise, Ctrl+Home takes you to the TOP of the file. There is no key mapping for Take me to the top, but by top I mean below this section. Commented May 29, 2024 at 0:12
  • @KenWhite Yes, I understand what those key does & what they mean. What I am trying to here is to find a solution for my use case. \ Maybe an extension or something that does these kind of jobs. \ Or some config, eg: tweak the "ctrl home" to something like "goto line". \ Or, eg: tweak the "ctrl a" to behave like "alt shift right to smart expand select" \ -- those things are totally doable, Im just trying to find a way. \\ Or, like ipynb files, they knows how to render a cell as a block, where you "ctrl a" only select the cell. Commented May 29, 2024 at 0:41

2 Answers 2

1

You can use a regex to select only what you want. See regex101 demo.

Find: ^(?!(<link|<!--)).+$

Explanation: Match lines that do not start with <link or <!--. You can see that it is just an alternation (or) of line starters, like <link that could easily modify yourself to add other line starters to avoid or remove the <!-- if you do not need that.

Now assuming you want to reuse this you don't want to have to paste that regex into the Find Widget every time. We can set up a keybinding to automate this. Put this into your keybindings.json:

{
  "key": "alt+f", // whatever keybinding you want
  "command": "editor.actions.findWithArgs",
  "args": {
    "searchString": "^(?!(<link|<!--)).+$",
    "isRegex": true
  }
}

See my answer at Perform pre-defined Find-Replace-All in VSCode using a keybinding for the other options to this command.

Triggering that keybinding will open the Find Widget, populate it with your regex and options. Then you can Alt+Enter (the command editor.action.selectAllMatches) to select the matches - whether your focus is in the Find Widget or the file.

If you want you can automate this further by combining the two commands into one keybinding using the runCommands command:

{
  "key": "alt+f", // whatever keybinding you want
  "command": "runCommands",
  "args": {
    "commands": [
      {
        "command": "editor.actions.findWithArgs",
        "args": {
          "searchString": "^(?!(<link|<!--)).+$",
          "isRegex": true
        }
      },
      "editor.action.selectAllMatches",
      "editor.action.clipboardCopyAction"    // to trigger copy to clipboard
    ]
  },
  "when": "editorTextFocus && !editorReadonly && editorLangId == markdown"
}
Sign up to request clarification or add additional context in comments.

1 Comment

- This is a more completed version. - Doesnt seem like its possible to use other flags inside the regex like m / s . - To close the find widget after the selection closeFindWidget stackoverflow.com/questions/46535964/… (No docs for this in code.visualstudio.com/api/references/commands ) - To reset the find flags, re-execute the findWithArgs with flags you prefer as default.
0

Put this in keybindings.json:

{
    "key": "ctrl+b", // TODO
    "command": "editor.actions.findWithArgs",
    "args": {
        "isRegex": true,
        "isCaseSensitive": true,
        "findInSelection": false,
        "searchString": "(?<=<!-- some random header info -->\\n)([\\n\\s]|.)*",
    },
},

See also:

3 Comments

(Thats a smart and simple way)
Why is this using runCommands? It is only running one command. And how does this help select the 'non-header' text the OP wants?
@Mark it (was) using runCommands because when I started writing it I assumed I might need it, and then I forgot to remove it after testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.