How can I make a function run every time cd successfully changes to another directory within sh on FreeBSD? - sh

I'm using sh as my shell on FreeBSD but I want to be able to have a pretty prompt like the one bash gives me on Ubuntu. There are two things that the FreeBSD implementation of sh seems to lack as far as PS1 escape characters go:
The \w works but does not expand $HOME to ~, so this is something I have already hacked up myself
I can use PS1 to update the prompt on the terminal, but as far as I can tell it is not possible to use the PS1 variable to update the title bar as well. ESC and BEL fail to set the title as one would expect if they were using bash or ksh
Here is my .shrc file
update_prompt() {
case "$PWD" in
"$HOME"*)
pretty_pwd="~${PWD#*"${HOME}"}"
;;
"/usr$HOME"*)
pretty_pwd="~${PWD#*"/usr${HOME}"}"
;;
*)
pretty_pwd="$PWD"
;;
esac
case "$TERM" in
xterm*|rxvt*)
PS1="[$USER#\\h $pretty_pwd]\\$ "
;;
*)
;;
esac
printf "\\033]0;[%s#$(hostname -s): %s]\\007" "$USER" "$pretty_pwd"
}
update_prompt
So when I fire up a terminal or log in via ssh, it gives the pretty prompt that I like. But now I need this function to run every time that cd is executed and returns an exit status of 0.
I was going to use an alias that was something like:
alias cd="cd $1 && update_prompt"
but that was before I realized that aliases do not except arguments. How might I go about doing something like this?

You can use a function instead of an alias:
cd() {
command cd "$#" && update_prompt
}
Just put it into ~/.shrc. You have to use command here to let sh know that you are referring to the actual cd builtin command instead of the function you've just defined.
Refer to the sh(1) manual page for the details on how to make sh(1) source the ~/.shrc file when it starts:
Therefore, a user should place commands that are to be executed only at login
time in the .profile file, and commands that are executed for every shell
inside the ENV file. The user can set the ENV variable to some file by placing
the following line in the file .profile in the home directory, substituting for
.shrc the filename desired:
ENV=$HOME/.shrc; export ENV
I use this trick in my cd alias manager. Here's a link to the source code of the function: https://github.com/0mp/goat/blob/v2.5.0/libgoat.sh#L31-L57

You can do it with alias+arguments if you swap the commands:
$ alias cd="echo change; cd"
$ pwd
/nas
$ cd /
change
$ pwd
/
$ cd /etc
change
$ pwd
/etc
$

Related

Autocomplete directories in a subfolder with the Fish shell

I'm having trouble getting the 'complete' function in the fish shell to behave as I would like and I've been searching for an answer for days now.
Summary
Essentially I need to provide tab directory auto-completion as if I was in a different directory to the one I am currently in. It should behave exactly as 'cd' and 'ls' do, but with the starting point in another directory. It seems like such a trivial thing to be able to do but I can't find a way to make it work.
Explanation
Example folder structure below
- root
- foo
- a
- dir1
- subdir1
- dir2
- subdir2
- b
- dir3
- subdir3
- dir4
- subdir4
I am running these scripts whilst in the 'root' directory, but I need tab auto-complete to behave as if I was in the 'foo' directory.
testfunc -d a/dir2/subdir2
Instead of
testfunc -d foo/a/dir2/subdir2
There are a lot of directories inside 'foo' and a lot of sub-directories within them, and this auto-complete behaviour is necessary to speed our process (this script is used extensively throughout the day).
Attempted Solution
I've tried using the 'complete' builtin to get this working by specifying the directory to use, but all this managed to do was auto-complete the first level of directories with a space after the argument instead of continuing to auto-complete like 'cd' would.
complete -x -c testfunc -a "(__fish_complete_directories ./foo/)"
Working bash version
I have already got this working in Bash and I am trying to port it over to fish. See below for the Bash version.
_testfunc()
{
local cur prev words cword
_init_completion || return
compopt +o default
case $prev in
testfunc)
COMPREPLY=( $( compgen -W '-d' -- "$cur" ) )
compopt +o nospace
return
;;
-d)
curdir=$(pwd)
cd foo/ 2>/dev/null && _filedir -d
COMPREPLY=( $( compgen -d -S / -- "$cur" ) )
cd $curdir
return
;;
esac
} &&
complete -o nospace -F _testfunc testfunc
This is essentially stepping into the folder that I want, doing the autocompletion, then stepping back into the original folder that the script was run in. I was hoping this would be easier in Fish after getting it working in Bash (I need to support these two shells), but I'm just pulling my hair out.
Any help would be really appreciated!
I am not a bash completions expert, but it looks like the bash completions are implemented by changing directories, running completions, and then changing back. You can do the same in fish:
function complete_testfunc
set prevdir $PWD
cd foo
__fish_complete_directories
cd $prevdir
end
complete -x -c testfunc -a "(complete_testfunc)"
does that work for you?

Show current branch on terminal

Is there any way to show in Terminal of VS Code to show in brackets current branch? I saw it somewhere but not sure how it can be done. By some extension or whatever..
C:/myUser/project> git status
I would like to see it something like:
C:/myUser/project>(master) git status
Open zshrc file
open ~/.zshrc
Add this text in the end of zshrc file
autoload -Uz vcs_info
precmd() { vcs_info }
zstyle ':vcs_info:git:*' formats 'on branch %b'
setopt PROMPT_SUBST
PROMPT='%n in ${PWD/#$HOME/~} ${vcs_info_msg_0_} > '
Source zshrc file
source ~/.zshrc
For Linux Terminal
You can modify the PS1 variable. PS1 is a Bash Environment Variable that represents the primary prompt string which is displayed when the shell is ready.
You can achieve your result by modifying this variable with a script.
First, get the output of your current value of the variable by running
$ echo $PS1
Sample output:[\u#\h \W]$
Now you save the following code in a bash file(Remember to replace the initial string of export PS1 with the output of the above command).
#!/bin/bash
source ~/.bashrc
get_cur_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="[\u#\h \W]\$(get_cur_branch)\$ "
Let's say path of the file is "/home/samar/Documents/my_vs_script.sh"
Now change your VS code settings by adding the following lines in 'settings.json'
"terminal.integrated.shellArgs.linux": [
"--init-file",
"/home/samar/Documents/my_vs_script.sh"
]
Now each time you open a new terminal in VS code, script file "my_vs_script.sh" will execute and you get the desired output.
For Windows-Powershell
The solution above works well for the Linux terminal. But if you want to do it for another command-line shell-like Powershell, you can change the 'setting.json' to
{
"terminal.integrated.shellArgs.windows": [
"-NoExit",
"-Command", "c:/scripts/myscript.ps1"
]
}
where 'myscript.ps1' must have a function 'prompt' definition to add git branch to your prompt.
You can refer this question for your 'myscript.ps1' code.
You don't need to change 'Microsoft.PowerShell_profile.ps1'. Defining it in another file works too.
I hope it helps.

why doesn't make -C change $PWD as seen through a scripting language such as Perl?

Here is temp/Makefile:
all:
echo $$PWD
echo $(CURDIR)
perl -e 'print $$ENV{"PWD"}'
and now
$make -C temp
make: Entering directory `/home/mgaleck/temp'
/home/mgaleck/temp
/home/mgaleck/temp
/home/mgaleck
make: Leaving directory `/home/mgaleck/temp'
Why is the third value without temp?
According to Make manual, -C option causes to "change the directory" (working directory?) first.
Same thing happens with Python.
Because the PWD environment variable doesn't hold the current working directory; it holds whatever the current working directory was the last time sh set it. Anything other than a shell starting up, or a shell executing the cd builtin (or a similar builtin like pushd, in shells that have it), has no effect on PWD, and relying on PWD anywhere except in the shell is probably a silly idea. Use getcwd (C), Cwd::getcwd (Perl), os.getcwd (Python), etc. instead.

Can a Fish script tell what directory it's stored in?

So, I really like Fish - but I need some help with scripting.
and in particular finding the path of the script being run.
Here is the solution for BASH
Getting the source directory of a Bash script from within
Can anyone help me find the equivalent with fish?
status --current-filename will output the path to the currently executing script.
For more information on the status command, you can run man status or see the documentation at http://fishshell.com/docs/current/commands.html#status
In fish shell,
set DIR (cd (dirname (status -f)); and pwd)
is an equivalent to the BASH one liner
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
mentioned in Can a Bash script tell what directory it's stored in?.
NOTE: fish shell will cd into that dir and stay there. Bash will cd but it stays contained in subcommand.
File path
To get the full path of the script, use status --current-filename, or
set FILE (status --current-filename) to declare a variable.
Directory path
To get the full path to the directory the script is stored in, use dirname (status --current-filename), or set DIR (dirname (status --current-filename)) to declare a variable.
The equivalent to this question is the following:
set DIR (dirname (status --current-filename)) is the equivalent
Since readlink -m does not exist on macOS,
DIR=dirname (realpath (status -f))
Works on macOS and Linux at the same time.
This solution also has the benefit to work from an .sh file or even the command line.

MacVim: create new file from command line by using `alias mvim="open -a macvim"`

When I use vim newfilename to open a file and this file does not exit, vim will create a new file with the name newfilename.
However, MacVim does not work in this way --- i.e. mvim newfilename (alias mvim="open -a macvim") will lead to an error: newfilename does not exist
Is there a way to configure MacVim such that mvim newfilename (alias mvim="open -a macvim") will create a new file and open it?
I'm guessing the error message comes from open, not from vim. You can replace your alias with a function;
mvim () {
local f
for f; do
test -e "$f" || touch "$f"
done
open -a macvim "$#"
}
This will create empty files if necessary before opening them.
edit Didn't see #Peter Lyons' comment about this; credit should go to him for first suggesting this solution. I'll be happy to remove this answer if Peter wants to submit his.
You don't need the mvim alias to the open command, you can instead use the mvim launcher script that comes bundled with most MacVim Snaphots. After adding that mvim to your path, then runing mvim newfile, will now open a newfile buffer in an new MacVim window just like gvim would.
The MacVim mvim script as linked to above:
#!/bin/sh
#
# This shell script passes all its arguments to the binary inside the
# MacVim.app application bundle. If you make links to this script as view,
# gvim, etc., then it will peek at the name used to call it and set options
# appropriately.
#
# Based on a script by Wout Mertens and suggestions from Laurent Bihanic. This
# version is the fault of Benji Fisher, 16 May 2005 (with modifications by Nico
# Weber and Bjorn Winckler, Aug 13 2007).
# First, check "All the Usual Suspects" for the location of the Vim.app bundle.
# You can short-circuit this by setting the VIM_APP_DIR environment variable
# or by un-commenting and editing the following line:
# VIM_APP_DIR=/Applications
if [ -z "$VIM_APP_DIR" ]
then
myDir="`dirname "$0"`"
myAppDir="$myDir/../Applications"
for i in ~/Applications ~/Applications/vim $myDir $myDir/vim $myAppDir $myAppDir/vim /Applications /Applications/vim /Applications/Utilities /Applications/Utilities/vim; do
if [ -x "$i/MacVim.app" ]; then
VIM_APP_DIR="$i"
break
fi
done
fi
if [ -z "$VIM_APP_DIR" ]
then
echo "Sorry, cannot find MacVim.app. Try setting the VIM_APP_DIR environment variable to the directory containing MacVim.app."
exit 1
fi
binary="$VIM_APP_DIR/MacVim.app/Contents/MacOS/Vim"
# Next, peek at the name used to invoke this script, and set options
# accordingly.
name="`basename "$0"`"
gui=
opts=
# GUI mode, implies forking
case "$name" in m*|g*|rm*|rg*) gui=true ;; esac
# Restricted mode
case "$name" in r*) opts="$opts -Z";; esac
# vimdiff, view, and ex mode
case "$name" in
*vimdiff)
opts="$opts -dO"
;;
*view)
opts="$opts -R"
;;
*ex)
opts="$opts -e"
;;
esac
# Last step: fire up vim.
# The program should fork by default when started in GUI mode, but it does
# not; we work around this when this script is invoked as "gvim" or "rgview"
# etc., but not when it is invoked as "vim -g".
if [ "$gui" ]; then
# Note: this isn't perfect, because any error output goes to the
# terminal instead of the console log.
# But if you use open instead, you will need to fully qualify the
# path names for any filenames you specify, which is hard.
exec "$binary" -g $opts ${1:+"$#"}
else
exec "$binary" $opts ${1:+"$#"}
fi