Search code examples
visual-studio-codevscode-keybinding

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


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.

Solution

  • 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"
    }