This was one of the projects made to learn/get a good feel for rust. fd (even good old regular gnu find) lists down directories faster. Its better to just pipe the unbuffered results of that to sed and fzf and go from there. Also, i use windows less and less for developing so I don't really use this anymore.
With that being said, I'm planning to just write scripts for various terminals
that do said fuzzy finding of git repos and cd'ing to it. Name that repo girep
or something.
sgr (sugar) is a wrapper around fzf whose search list only includes local git repos in selected directories.
- selected directories are defined in
dirs.txt
in the same directory as sgr (auto created) - selected directories can either contain git repos OR can be git repos themselves
You can install fzf with chocolatey
choco install fzf
In order to change directories, sgr needs a script native to your shell to be written for it.
- cmd
- pwsh or powershell
You can install pwsh with chocolatey
choco install pwsh
- Download the latest release of sgr
- Extract the archive
- You should have these files (windows .7z archive in this example)
sgr.bat <-- the batch script to make sgr work with cmd sgr.ps1 <-- the powershell script to make sgr work with pwsh sugar.exe <-- the executable to find git repos
- Put these files into one directory you have access to
- Add that directory to your PATH
- this is important because the scripts (
.bat
and.ps1
in this example) assume it can execute thesugar
executable just by calling it.
- this is important because the scripts (
Optional:
-
create
dirs.txt
in the same directory assgr
and put directories containing git repos.- separate the directories with newlines. example:
C:\Users\<username>\spaces are\okay C:\Users\<username>\path\to\single\git\repo\is\okay %env vars%\are\NOT\okay!! <-- INVALID
- if there's no
dirs.txt
in the same directory,sgr
will create one containing only one line:
if called in
cmd
:%userprofile%\projects
if called in
pwsh
orpowershell
:$env:userprofile\projects
These environment variables will be expanded to be absolute paths. Below is the general format:
<whatever is the env var for user>/projects
This means sgr will only search for git repos in that directory
If you prefer to build it from source:
- Clone the repo and cd into it
git clone https://github.com/saltkid/sgr.git
cd sgr
- Build it
cargo build --release
- Move the
sugar
executable into any directory you have access to- You will add this directory to your PATH later
- or you can put it in a directory already in your PATH
- the
sugar
executable is in the./target/release
directory
- You will add this directory to your PATH later
- In the
./scripts
directory, pick the scripts that your shell/s use and move it to the same directory as thesugar
executable in the previous step - If not already, add the directory that contains the
sugar
executable and the scripts to your PATH
- Open a terminal and run
path/to/sgr
- your shell will infer that the
sgr
you are referring to is the script native to it- if you ran
sgr
incmd
, it will executesgr.bat
- if you ran
sgr
inpwsh
, it will executesgr.ps1
- etc.
- if you ran
- your shell will infer that the
- This will open up the usual fzf
interface. Select a repo and it'll
cd
your current terminal to that path
sgr has optional commands that handles modifying dirs.txt
add
-
args:
path/to/dir
-
adds
path/to/dir
todirs.txt
which will be searched for git repos when sgr is run -
validations:
- you cannot add a path that is already in
dirs.txt
- you cannot add a path that is a subdir of another path already in
dirs.txt
- this is because sgr will search for git repos under
directories in
dirs.txt
so having a subdirectory of another directory will cause double the work for the same result
- this is because sgr will search for git repos under
directories in
- similar to 2, you cannot add a path that is a parent of another path
already in
dirs.txt
- you cannot add a path that is already in
-
remove
-
args:
path/to/dir
,<int>
,<int_x>-<int_y>
-
removes
path/to/dir
fromdirs.txt
if it's indirs.txt
-
if
<int>
is provided, it will remove the dir at line<int>
indirs.txt
-
if
<int_x>-<int_y>
is provided, it will remove the dirs between lines<x>
and<y>
(inclusive) -
validations:
- you cannot remove a path that is not in
dirs.txt
- you cannot remove by index where the index is out of range
- you cannot remove by range where the range is out of range
- you cannot remove by range where the starting range is greater than
or equal to the ending range (
1-1
,3-3
,4-2
)
- you cannot remove a path that is not in
-
list
- args: none,
<int>
,<int_x>-<int_y>
,path/pattern
- lists
dirs.txt
if no args are given - if
<int>
is provided, it will list the dir at line<int>
indirs.txt
- if
<int_x>-<int_y>
is provided, it will list the dirs between lines<x>
and<y>
(inclusive) - if
path/pattern
is provided, it will list the dirs that matchpath/pattern
- case insensitive but must be exact match
- validations:
- you cannot list by index where the index is out of range
- you cannot list by range where the range is out of range
- you cannot list by range where the starting range is greater than or
equal to the ending range (
1-1
,3-3
,4-2
)
- args: none,
help
- args: none or another command
- prints help message along with the list of commands
- if a command is specified, it will print the verbose help message of that command
version
- args: none
- prints version
- Open Windows Terminal and go to settings (
Ctrl+,
) - Open settings.json
- Under
"actions"
, there are a list of commands. Add either of these two snippets (or both):
Below will just insert the string sgr
to the terminal and press enter when
you press ctrl+f
. This will open up the fzf
prompt waiting for input.
{
"name": "Find and Goto Git Repository",
"keys": "ctrl+f"
"command": { "action": "sendInput", "input": "sgr\u000D" },
},
Below will open a new tab with the fzf prompt
waiting for input when you press ctrl+shift+f
. Difference with above is that
above snippet executes sgr.bat
in the same tab while below snippet executes
on a new tab
{
"name": "Find and Goto Git Repository (new tab)",
"keys": "ctrl+shift+f",
"command": {
"action": "newTab",
"commandline": "cmd.exe /k \"sgr\""
}
}
Notes:
-
Just be wary that the first shortcut (sending input) will literally send "sgr" and press enter, even when you're in another TUI. This is a limitation of Windows Terminal where you can't execute scripts with a shortcut in the same tab. The next tab variant works as expected though and does not paste text
-
If you want to use
pwsh
in the new tab, just replacecmd.exe /k
withC:\\Program Files\\PowerShell\\7\\pwsh.exe -noexit -c
- yes, it has to be full path, even if
pwsh
is in your PATH
- yes, it has to be full path, even if
-
if you wish to use
powershell.exe
instead, just replacecmd.exe /k
withpowershell.exe -noexit -c
-
ctrl+shift+f
is by default bound to the action of opening the search dialog box in Windows Terminal. If you wantctrl+shift+f
for running sgr in new tab, remap the"find"
command to something else likectrl+alt+f
-
Windows uses backslashes so those need to be escaped here. Example:
"commandline" : "powershell.exe -noexit -c \"sgr\""
Instead of just changing directories, in this example, the below snippets will immediately open up nvim afterwards. Just a small change
{
"name": "Find and Goto Git Repository (nvim)",
"keys": "ctrl+f",
"command": { "action": "sendInput", "input": "sgr && nvim .\u000D" },
},
and
{
"name": "Find and Goto Git Repository (nvim)",
"keys": "ctrl+shift+f",
"command": {
"action": "newTab",
"commandline": "cmd.exe /k \"sgr && nvim .\""
}
}
Notice that we just added && nvim .
after sgr
on both shortcuts. That's it.
You can replace && nvim .
with any command you want, to execute anything
after sgr is done changing directories.
- ThePrimeagen for the idea (specifically, his tmux-sessionizer, the quick switching part only)
- fzf
- chocolatey
- cmd
- pwsh