AutoLS for fish shell - fish

I have an auto-ls script in my conf.d directory
function __autols_hook --description "Auto ls" --on-event fish_prompt
if test "$__autols_last" != (pwd)
if test "$HOME" = (pwd)
else
clear; ls;
# Show git information, and if it's not a git repo, throw error
# into /dev/null. Simples
git status 2>/dev/null
end
end
set -g __autols_last (pwd)
end
This works very well. However, I'd also like this to trigger when I hit enter, in the same pwd, but with no command.
I can't find a way to check if the enter key was pressed but no command

Change the binding for the [enter] key:
bind \cm 'set -l cmd (commandline); test -z "$cmd"; and set -g _empty_command yes; or set -g _empty_command no; commandline -f execute'
Now you can test $_empty_command in your fish_prompt event function. Note that [ctrl-j] also invokes the execute command so you should probably also bind \cj to the same code. But that's optional unless you have an unusual terminal config.

Related

Powershell - Run SCP command using CYGWIN but also expand variables

I need to expand variables before running the SCP command as a result I can't use single quote. If I run the script using double quotes in Powershell ISE it works fine.
But doesn't work if I run the script through command prompt.
I'm using zabbix to run the script which calls the script as [cmd /C "powershell -NoProfile -ExecutionPolicy Bypass -File .\myscript.ps1"]
Here is the code that needs to run SCP using Cygwin bash.
if ((test-path "$zipFile"))
{
C:\cygwin\bin\bash.exe -l "set -x; scp /cygdrive/e/logs/$foldername/dir1/$foldername.zip root#10.10.10.10:~/"
}
Output:
/usr/bin/bash: set -x; /cygdrive/e/logs/myfolder/dir1/server.zip root#10.10.10.10:~/: No such file or directory
If I run the same command above in Cygwin manually it works.
I even tried to use bash -l -c but then the SSH session is stuck maybe because the root#10.10.10.10 becomes $1 according to the documentation.
Documentation link
-c If the -c option is present, then commands are read from
the first non-option argument command_string. If there are
arguments after the command_string, the first argument is
assigned to $0 and any remaining arguments are assigned to
the positional parameters. The assignment to $0 sets the
name of the shell, which is used in warning and error
messages.
Figured it out. It was halting when using bash -c was due to StrictHostKeyChecking, the known hosts thing (where you get a prompt to type yes/no). I set the -v switch to SCP and it showed me the Debug logs where it was halting.
Had to set scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null options.
The complete line now looks like the following:
c:\$cygwin_folder\bin\bash.exe -c ("/usr/bin/scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -v -i /cygdrive/c/cygwin/home/myuser/.ssh/id_rsa /cygdrive/e/logs/$foldername/dir1/$foldername.zip root#10.10.10.10:~/")

How do you override/remove default completions in fish shell?

I am adding completions for a command's subcommand, however fish is retaining the built in completions for the base command, but those no longer apply for the subcommand. I want to disable those base command completions when using the subcommand.
So, to give a specific example, I am adding complete completions for the python3 -m venv command. As I stated, all the builtin python3 completions still show even though they no longer apply. So, when I type python3 -m venv -<TAB>, I get the completions I've added (good!), but also all the default completions too (bad).
So I have this code:
function __fish_python_using_command
# make sure that the command (minus the first item) matches argv
set cmd (commandline -opc)
if [ (count $cmd) -le (count $argv) ]
return 1
end
set idx (math (count $argv)+1)
if [ "$argv" = "$cmd[2..$idx]" ]
return 0
end
return 1
end
complete -f -c python3 -n '__fish_python_using_command -m venv' -s h -l help -d 'Display help creating virtual Python environments'
After running this, when I type when I type python3 -m venv -<TAB> I get:
The new auto complete I defined for --help (correct)
The base defined auto complete for -h (wrong)
All the other python3 base auto complete switches like -V from complete --command python3 --short-option 'V' --description 'Display version and exit' (I want to disable these)
I have considered using the -e flag to remove the defaults when you are in python3 -m venv mode, but that seems like the wrong way to go about it. I'm stumped. How would one disable all existing completions once a subcommand mode is entered? Or would this require a fundamental change to the way the python3 fish builtin completions are structured?
Fish loads completions from files in $fish_complete_path. This is a list of directories, like $PATH. Put your completions into a file named after the command with a ".fish" suffix in an earlier directory and it will take precedence.
E.g. ~/.config/fish/completions/python3.fish.

Running sudo on user-defined functions in fish

» cat ~/.config/fish/config.fish
function take
command mkdir $argv;and cd $argv
end
function check
sudo dmesg -c>/dev/null;
make clean; make;
/usr/local/bin/kedr start $argv;
sudo insmod "$argv.ko"; sudo rmmod $argv;
/usr/local/bin/kedr stop
dmesg;
end
function sudo
if functions -q $argv[1]
set argv fish -c "$argv"
end
command sudo $argv
end
While running I get this error:
» sudo check "simple-no-macro"
fish: Unknown command 'check simple-no-macro'
fish:
check simple-no-macro
^
You've asked this on GitHub as well, so here's my answer from there:
The problem here is that the function you've defined isn't present in the new instance of fish you start.
You'd be better off defining the check function in a file saved in ~/.config/fish/functions/check.fish, which will then let the function work across instances.
Side note: bash does let you export functions across instances using environment variables, but both zsh and ksh use a similar method to fish - see Propagating shell functions from Unix Power Tools.

Running last command with sudo in fish only works if it has no arguments?

I'm trying to write a function that does the equivalent of sudo !! in Bash. It works, but only when the last command has no arguments.
So far the function is:
function s --description "Run last command (or specified command) using sudo"
if test $argv
switch $argv[1]
case '!!'
command sudo (echo $history[1])
case '*'
command sudo $argv
end
else
command sudo fish
end
end
Testing the relevant line:
$ command sudo whoami
root
$ whoami
nick
$ command sudo (echo $history[1])
root
So far so good, now lets try a command with a few args:
$ echo hi >> /etc/motd
An error occurred while redirecting file '/etc/motd'
open: Permission denied
$ command sudo (echo $history[1])
sudo: echo hi >> /etc/motd: command not found
Hmm, strange.
Got it working using eval.
function sudo --description 'Run command using sudo (use !! for last command)'
if test (count $argv) -gt 0
switch $argv[1]
case '!!'
if test (count $argv) -gt 1
set cmd "command sudo $history[1] $argv[2..-1]"
else
set cmd "command sudo $history[1]"
end
case '*'
set cmd "command sudo $argv"
end
else
set cmd "command sudo fish"
end
eval $cmd
end
I had the same problem as you, and I fixed it by using oh-my-fish
(it's a plugin manager for fish shell) https://github.com/oh-my-fish/oh-my-fish. You can install it with this command :
curl -L https://get.oh-my.fish | fish
Then install the plugin bang-bang (to allow !! and !$) with this command :
omf install bang-bang

Cant access Coda application from command-line

I am having trouble accessing Coda from command-line. I installed the "command-line coda" plug-in, verified that my installation is in the correct location, yet I still can seem to access Coda. Coda sits in my "Applications" folder which is the default location for the plug-in.
Anyone have have this problem? Any tips? On the their site it is recommended that you change the path.
export CODEPATH=/Applications/Coda.app
So I included the above line in my .bash_profile which did not help.
$ Coda -v
-bash: Coda: command not found
Thanks for any direction you can provide.
The default way to open an application on a Mac is to use open -a AppName so you should be able to change your bash profile to use that:
$ open -a Coda
I've created a bash script (as opposed to using the plugin) that Gregory Tomlinson originally posted about (it looks like he's since taken it down but it looks like the following).
Create a new file in /bin called coda:
$ cd /bin
$ sudo touch coda
$ vim coda
Hit an i to enter insert mode. Then include the following code:
#! /bin/bash
if [ "$1" = "" ]; then
echo "Please specify a file to open or create"
exit 0
else
for ARG in $*
do
touch -a $ARG && open -a Coda $ARG
done
exit 0
fi
Save and quit (hit the esc to exit insert mode then type :w !sudo tee % >/dev/null followed by the return key, press L for load when prompted, then type :q to quit). Then give that file execute permissions:
$ chmod u+x coda
Start a new Terminal window and you should be able to use:
$ coda filename.foo
Or just:
$ coda
For some strange reason, my paid registered Coda 2 app just wouldn't open for me this morning. I found this terminal command that worked for me:
open -a Coda\ 2
You can also put the following in your ~/.bash_profile file:
alias coda='open -a "Coda 2"'
I had a similar problem. After installing the plug-in, I still couldn't launch coda from the command line. I took a closer look at /user/local/bin and somehow the permissions had gotten reset so I didn't have execute permissions for /user/local/bin.
I updated my permissions with:
sudo chmod o=rx,g=rx /usr/local/bin
This solved my problem. However, Coda won't launch if the specified file does not exist, which makes it hard to create a file from the command line.