Fixing one of the most annoying things in file naming

Many may know that using spaces in your file name can cause certain issues, especially when working with code. So I am pushed into using “The_UNDERSCORE_“! This is kinda annoying because I can’t type out the file names in a natural fashion.

Then one day it occurred to me that there is key-modifier software out there that can maybe help me. The idea was to transform the Caps Lock key (which i never use) into a toggle for the spacebar. With it off, nothing changes, but with it on, it makes it so that every time i hit space it makes an underscore instead.

To do this I used an app called: Karabiner Elements. You go into the complex modifications tab and click the button that says “Add your own rule using JavaScript“. It will bring up a window with some example code and you just have to delete it and add in this code I made with the help of GPT:

function main() {
  var manipulators = [];

  // Caps Lock ON filename mode
  manipulators.push({
    type: "basic",
    from: {
      key_code: "caps_lock",
      modifiers: { optional: ["any"] }
    },
    conditions: [
      {
        type: "variable_if",
        name: "filename_mode",
        value: 0
      }
    ],
    to: [
      {
        set_variable: {
          name: "filename_mode",
          value: 1
        }
      }
    ]
  });

  // Caps Lock OFF filename mode
  manipulators.push({
    type: "basic",
    from: {
      key_code: "caps_lock",
      modifiers: { optional: ["any"] }
    },
    conditions: [
      {
        type: "variable_if",
        name: "filename_mode",
        value: 1
      }
    ],
    to: [
      {
        set_variable: {
          name: "filename_mode",
          value: 0
        }
      }
    ]
  });

  // Space → underscore (bulletproof macOS input method)
  manipulators.push({
    type: "basic",
    from: {
      key_code: "spacebar"
    },
    conditions: [
      {
        type: "variable_if",
        name: "filename_mode",
        value: 1
      }
    ],
    to: [
      {
        shell_command: "osascript -e 'tell application \"System Events\" to keystroke \"_\"'"
      }
    ]
  });

  return {
    description: "Caps Lock filename mode (space becomes underscore)",
    manipulators: manipulators
  };
}

main();

and TADA, bobs your uncle.

Karabiner Elements also makes it very easy to toggle on or off if you dislike it. I should also say that this is a solution for mac, but i would imagine that there are options for windows as well.