Best way to just add an item to a zsh prompt - virtualenv

I have been working on a way to add an item to my zsh prompt to allow it to show the active virtualenv. I followed this blog post, and got this so far:
function virtual_env_prompt () {
REPLY=${VIRTUAL_ENV+(${VIRTUAL_ENV:t}) }
}
grml_theme_add_token virtual-env -f virtual_env_prompt '%F{magenta}' '%f'
zstyle ':prompt:grml-large:left:setup' items \
rc virtual-env jobs history shell-level change-root time \
date newline user at host path vcs percent
It works, but the problem is I have to take the items from the prompt and repeat them in my config file. Is there any way I can just add the virtualenv item to the prompt without repeating everything else as well?

Related

zsh: compinit and autocomplete function redefinition

I'm trying to learn how autocompletion works in zsh. I've got a simple script file (example.zsh) and I'm trying to create a simple autocomplete function that describes each of its parameters. In order to do that, I've started by creating a simple _example file which looks like this:
#compdef create_ca
_arguments \
"--caKey[name of the file that will hold the keys used for generating the certificate (default: ca.key)]" \
"--caCrt[name of the file that will hold the certificate with the public key (default: ca.crt)]" \
"--cn[common name for the root certificate (default: root.GRM)]" \
"--days[number of days that certificate is valid for (default: 10500)]" \
"--size[key size (default: 4096)]" \
"--help[show this help screen]"
The file is on the same folder as the script and I've updated my .zshrc file so that it adds that folder to the $fpath:
fpath=(~/code/linux_certificates $fpath)
autoload -Uz compinit
compinit -D
I'm using the D option so that the .zcompdump isn't generated. At first sight, everything worked out, but when I tried to update the helper autocomplete function, I'm unable to see thosee changes (ex.: changing the description). I've tried re-running the compinit command and, when using the cache .zcompdump, deleting that file. However, it simply didn't work. The only way I've managed to get it working was by deleting the autocomplete helper function with:
unfunction _create_ca
Is this the expected behavior? I mean, should't running compinit -D be enough to reload my helper autocomplete function?
btw, any good tutorials on how to create autocomplete functions (besides the official docs)?
thanks.
Once a function has been loaded, it will not be loaded again. That’s why you first have to unfunction your function, causing Zsh to unload it, so it can be loaded again.
Alternatively, you can just use exec zsh to restart your shell.

How do I make zsh autocompletion remove trailing slash it returns?

I have a custom script that I pass an argument into that will create a directory in a specific folder and then cd to it, unless it already exists, then it just cd to it.
I added a zsh autocompletion that way if I want to use the command to just jump to a directory in that folder I can autocomplete the target folder and get there in less keystrokes. The only issue with my zsh autocomplete is that it leaves the tailing directory slash after the autocomplete. Is there any way to tell zsh to only return the name of the directory and leave the / off? The docs are long winded and my searching has not turned up anything useful.
Here is my zsh autocomplete
compdef '_path_files -W /home/me/dir1/dir2/folder -/ && return 0 || return 1' mycommand

ZSH autocomplete option parsing: Accessing the completed value

I have an autocompletion script, with this snippet:
_arguments \
'-project[project file]:project:->projects'
...
case "$state" in
...
projects)
local -a projects
projects=(*.project)
_multi_parts / projects
echo "$SELECTED_PROJECT" > /tmp/project
;;
...
How can I get access to the user-selected option from _multi_parts / projects such that there is a variable $SELECTED_PROJECT that is accessible to other parts of the completion script?
I have other options in the _arguments declaration that are dependent on which project the user has selected, but I can't seem to find a way to get access to it.
Inside a completion function, the words that are already present on the command line are available in the $words array.
You can find examples of using $words with _arguments in the completion functions that ship with Zsh.

Showing time and/or date on powershell command prompt

Is it possible to have powershell show the time and/or date on the command prompt so that, for example, I can see when the last command was executed?
Something like this:
C:\git\MyProj [feature/delivery +0 ~6 -0 !] [14:32:02]>
This is not a duplicate because I would like to maintain poshgit integration while adding the time to the prompt. The related post replaces the entire prompt with a custom prompt. I am asking for an augmentation while leaving any other plugins intact.
poshgit provide example instructions how to do this, though they are out of date with my version of of poshgit.
type $GitPromptSettings at the prompt to view the list of settings. My system lists the DefaultPromptSuffix as being set to $('>' * ($nestedPromptLevel + 1))
To change it, open your PowerShell profile file (type $profile at the prompt to get its location, or just notepad $profile to open it)
Below the Import-Module line for posh-git, add the following:
$GitPromptSettings.DefaultPromptSuffix = ' [$(Get-Date -f "HH:mm:ss")]$(">" * ($nestedPromptLevel + 1))'
or, if you wanted the time as a prefix:
$GitPromptSettings.DefaultPromptPrefix = '[$(Get-Date -f "HH:mm:ss")] '

Cygwin shortcut for command history

How can I search the command history in cygwin?
I don't want to keep pressing the arrow keys to execute a command from console command history.
If you are using the default editing mode, do ctrl+R to search back through your history.
If you have done set -o vi to use vi editing mode, then it is esc-/
The history command is the way to go. I use
h ()
{
history | cut -f 2- | sort -u | grep -P --color=auto -e "$*"
}
so that I can type something like h git.*MyProgram, h ^tar -c, h svn:ignore, etc to pull up a sorted list of past commands matching a regex.
You might also want to add the following lines to ~/.inputrc:
# Ctrl+Up/Down for searching command history
"\e[1;5A": history-search-backward
"\e[1;5B": history-search-forward
With these in place, you can type a partial command prefix (such as gi or sql) then use Ctrl+Up to scroll back through the list of just your command history entries that match that prefix (such as git clone https://code.google.com/p/double-conversion/ and sqlite3 .svn/wc.db .tables). This can be a lot faster than searching and then cutting and pasting if you want to edit or re-execute a command that was fairly recent.
I use the history command in combination with grep, e.g. history | grep vi shows all commands where vi was used.
Checkout the "Gnu Bash Manual" (man bash) for the command "fc". E.g.fc -l -80 would list the last 80 commands, while other options let you search with RegEx...
Do
vi ~/.inputrc
Add
For arrow up/down bindings:
"\e[A": history-search-backward
"\e[B": history-search-forward
Or for page up/down bindings:
"\e[5~": history-search-backward
"\e[6~": history-search-forward
Close and open cygwin.
Voila.
I think one of the easiest way is to pipeline it with less and press search character ("/") and then type the command you wanna find.
history | less
and then
/text to find
to find the desired command
Another way
is to append the stdout form history command to a file: history > /cygdrive/path/file.txt
and then search in the document.