Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suffix aliases in bash

Tags:

linux

bash

unix

zsh

Suffix aliases are the only reason I'm considering to switch to ZSH but I want to stick with bash. So is it possible to have something like suffix aliases in bash?

For those that doesn't know what a suffix alias is, the following in ZSH

$ alias -s cpp=vi
$ filename.cpp

will run vi with filename.cpp as the first argument.

Note that something like xdg-open or gnome-open is not sufficient. I want bash to execute a command when a file name is entered.

Completion is quite important to me. Therefore if the beginning of a filename is typed then it would be nice if the rest of the filename would be completed when the TAB key is pressed.

like image 553
brillout Avatar asked Jan 30 '12 00:01

brillout


People also ask

What are aliases in bash?

A BASH Alias is a map of commands with the sets of commands or functions that can be used as a shortcut in the command line for a BASH environment. Bash Alias allows to aggregate multiple functions into a single command and also it avoids repetitive or large commands into a simple shortcut command.

Does bash have aliases?

The Bash alias command allows a string to be substituted when it is used as the first word of a command line. The shell maintains a list of aliases that may be set and unset with the Bash alias and unalias builtin commands.

How do I check bash aliases?

All you need to do is type alias at the prompt and any active aliases will be listed. Aliases are usually loaded at initialization of your shell so look in . bash_profile or . bashrc in your home directory.


1 Answers

You can build one using the new command_not_found_handle() function. Getting the full ability of the zsh suffix alias would take more work than my simple example here; but my simple example might be sufficient for your needs:

$ command_not_found_handle() { if [[ $1 =~ .*.cpp ]]; then vi $1 ; elif [[ $1 =~ .*.java ]]; then cat $1 ; fi ; }
$ splice.cpp  # started vi on splice.cpp
$ Year.java
import java.util.Scanner;

class Year {
    public static void main(String[] args) {
        Scanner yearenter = new Scanner(System.in); 
        System.out.println("Enter year ");
        int year = yearenter.nextInt();     
        System.out.print("Year " + year + " is ..");
        if (year % 400!=0 || year % 4 != 0 && year % 100==0)
            System.out.println(" not a leapyear"); 
        else
            System.out.println(" a leapyear"); 

    }    
} 
$ 

Here's the function expanded enough to be legible:

command_not_found_handle()
{
    if [[ $1 =~ .*.cpp ]]
    then
        vi "$1"
    elif [[ $1 =~ .*.java ]]
    then
        cat "$1"
    fi
}

Extend it as you see fit -- each =~ is a regular expression match, so feel free to use whatever regular expressions you want.

Note that this conflicts with the command-not-found Debian and Ubuntu packages, so you may need to uninstall or otherwise unconfined this package for reliable results. (Just make sure this function is defined in your own ~/.bashrc or ~/.bash_profile file after the system-wide /etc/bash* files are included, and it should just work.)

like image 185
sarnold Avatar answered Nov 13 '22 11:11

sarnold