Search code examples
fish

Always open a txt file using specified app in fish shell


I use fish shell. I always open *.txt files in atom, so I need to type atom filename.txt. I know, that in zsh, there's an option to always open files with some extension in the specific app using alias -s option. Is there a way to achieve the same behavior in fish shell?


Solution

  • Two solutions come to mind. First, use an abbreviation or function to reduce the number of characters you have to type:

    abbr a atom
    

    Now you can just type "a *.txt". The advantage of doing function a; atom $argv; end is that it allows for more complicated steps than just replacing a short command with a longer command. As another example, I have abbr gcm "git checkout master" in my config because that's something I do frequently.

    Second, use a key binding. For example, arrange for pressing [meta-a] to insert "atom" at the start of the command and execute it:

    function edit_with_atom
        set -l line (commandline -b)
        commandline -r "atom $line"
        commandline -f execute
    end
    
    bind \ea edit_with_atom
    

    The key binding allows for more complicated operations than what I've shown above since you can execute arbitrary code.

    These solutions don't scale but if there's just a couple of commands you run frequently that you want to invoke with fewer keystrokes they might help.