Skip to content

hutchYy/zsh-vpn-network-prompt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

zsh-vpn-network-prompt

Guide to add the VPN's network config in the prompt

Installation

The prompt works for both ZSH "vanilla" and Oh My Zsh, on the other hand the configuration file is not the same.

Oh My Zsh

In Oh My Zsh, it is fairly simple to add the network prompt by editing your current theme file.

First, we need to find what's your current theme:

cat ~/.zshrc | grep -E 'ZSH_THEME=".*?"'

Then, we need to edit it to add our custom prompt code:

# Please replace [THEME_NAME] by your theme's name
code ~/.oh-my-zsh/themes/[THEME_NAME].zsh-theme

Once open, you can jump to the interesting part.

ZSH vanilla

Just open the .zshrc file with your favorite code editor and jump to the interesting part.

Adding the VPN's network config

To add the VPN's network config to your prompt you will need to:

  1. Add the following function to your file:

    function _tun_ips() {
    local ipaddr
    ipaddr=$(ip a | grep -E 'tun[0-9]+' | grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{split($0,a," "); print a[5]"("a[2]")"}')
    if [[ $(echo $ipaddr | wc -l) > 1 ]];then
        ipaddr=$(echo -e $ipaddr | sed ':a;N;$!ba;s/\n/,/g')
    fi
    if [[ -n $ipaddr ]]; then
        echo "%{$fg[cyan]%}$ipaddr%{$reset_color%}:"
    fi
    }
  2. Call the previously added function in the prompt by adding "$(_tun_ips)" in the "PROMPT" variable.

    For e.g., here is my PROMPT variable with the "$(_tun_ips)":

    PROMPT='
    $(_user_host)${_current_dir} $(git_prompt_info) $(ruby_prompt_info) $(_tun_ips)
    %{%(!.${fg[red]}.${fg[white]})%}➔%{$reset_color%} '
    
  3. Reload you zsh by doing:

    source ~/.zshrc
    
  4. Enjoy!

About

Guide to enable the VPN's network config in the prompt

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published