Bash highlight variables

Bash highlight variables

1
NewbieNewbie
1

    Jul 15, 2014#1

    Hi,

    I'd like to highlight complete variables in bash.
    The variables either look like:
    $var
    or like
    ${var}

    I tried playing around with with Marker Characters but did not find the answer.

    My wordfile:

    Code: Select all

    L33"Bash_2" Line Comment = #  Escape Char = \ String Chars = "' File Extensions = SH CSH
    /Delimiters = ~!@$%^*()+=/\[]{}:;"<>'´` ,       .?
    /Indent Strings = "{("
    /Unindent Strings = "})"
    /Function String = "%[a-zA-Z_]*("
    /Function String 1 = "%[a-zA-Z_]*{"
    /Marker Characters = "${}"
    /C1
    '
    {
    }
    &&
    ||
    $
    alias
    break
    case continue
    do done
    elif else esac exit export
    fi for
    if in
    return
    set
    then
    unalias unset
    while
    /C2
    halt
    ifconfig init initlog insmod
    linuxconf lsmod
    modprobe
    reboot rmmod route
    shutdown
    traceroute
    /C3
    ]
    [
    awk
    basename
    cat cp
    echo egrep
    fgrep
    gawk grep gzip
    kill killall
    less
    md mkdir mv
    nice
    pidof ps
    rd read rm rmdir
    sed sleep
    test touch
    ulimit uname usleep
    zcat zless
    /C4
    `
    -a -b -c -d -e -f -g -h -i -j -k -l -m -n -o -p -q -r -s -t -u -v -w -x -z
    /C5
    -eq -ge -gt -le -lt
    =
    !=
    /C6"Variables" Colors = 1508853
    $}
    
    Any help is appreciated.

    115
    Power UserPower User
    115

      Jul 15, 2014#2

      Marker characters are defined as single character pairs with up to 4 pairs. So your example "${}" says the first pair opens with $ and ends with { while the second pair is incomplete but it would open with } if there were another character after it. If you changed your marker characters to "{}" then you could highlight those variables when the braces are used.

      Also you have to define the color to be used or the default text color is used. To define the color, put the pair in the color group you want. For example, put {} in C4 on a line by itself.

      As for highlighting varibles that have the $ but don't have the braces, there isn't much that can be done outside of a kludge. Like making sure that all of your variables end in z and then adding a marker characters pair of "$z"

      6,603548
      Grand MasterGrand Master
      6,603548

        Jul 16, 2014#3

        I suggest following:

        Code: Select all

        /L20"Bash" Line Comment = # String Chars = "' Escape Char = \ DisableMLS File Extensions = SH CSH
        /Delimiters = ! "	$%'()*+,./:;<=>?@[\]^`~´
        /Function String = "%^([0-9a-z_]+^)[^t ]++[({]"
        /Indent Strings = "(" "{"
        /Unindent Strings = ")" "}"
        /Open Brace Strings = "(" "{" "["
        /Close Brace Strings = ")" "}" "]"
        /Open Fold Strings = "if" "elif" "else" "for" "while" "until"
        /Close Fold Strings = "elif" "else" "fi" "done"
        /C1"Keywords"
        $
        alias
        break
        case continue
        do done
        elif else esac exit export
        fi for
        if in
        return
        set
        then
        unalias unset until
        while
        {
        }
        /C2"Commands 1"
        halt
        ifconfig init initlog insmod
        linuxconf lsmod
        modprobe
        reboot rmmod route
        shutdown
        traceroute
        /C3"Commands 2"
        [
        ]
        awk
        basename
        cat cp
        echo egrep
        fgrep
        gawk grep gzip
        kill killall
        less
        md mkdir mv
        nice
        pidof ps
        rd read rm rmdir
        sed sleep
        test touch
        ulimit uname usleep
        zcat zless
        /C4"Switches"
        ** -a -b -c -d -e -f -g -h -i -j -k -l -m -n -o -p -q -r -s -t -u -v -w -x -z
        /C5"Operators"
        !
        &&
        -eq -ge -gt -le -lt
        <
        =
        >
        | ||
        /C6"Single Quotes"
        '
        `
        ´
        /C7"Variables in Braces"
        ** ${
        /C8"Variables no Braces"
        ** $
        
        Note: There should be a tab character between between " and $ in the second line.

        We would need good examples of bash scripts to improve this syntax highlighting definition.
        Best regards from an UC/UE/UES for Windows user from Austria

        115
        Power UserPower User
        115

          Jul 16, 2014#4

          Since the same variable rules apply for the Bourne and Korn shell scripting syntax file, I made similar changes there. I did have to remove the $ from the delimiter character list to allow highlighting for the special shell variables $0-$9, $-, $*, $#, $$ and $?

          kornsample.jpg (138.87KiB)


          Sorry about the colors. I tweaked them for contrast for this example.

            Jul 16, 2014#5

            The braces are used to separate the variable from regular characters which can immediately follow the variable. However the syntax color continues until a delimiter is encountered. I fixed that by adding the closing brace to the delimiter characters. I gave the closing brace the same color as the variable by adding it to the color group defining ${. Since the braces are used with function definitions, I also added the open brace so the colors would be the same when used that way.