Open a file in Notepad++ directly from the Git command line.
Explanation
Open the first file whose filename (not path) contains the specified search term.
Intended usage is for a complete or partial file name - does not match against directory names.
File is opened in Notepad++ and the alias assumes this is installed in C:/Program Files/Notepad++
Tested in powershell, command prompt, and git bash.
Explanation:
root is the file path to the git repository.
file is the file path within the repository for the first file found matching the search term.
Uses grep/findstr to match only against the basename (filename) portion of the path, not directories.
Checks if grep is available and uses it, otherwise falls back to a Windows-compatible approach.
Validates that a file was found before attempting to open it.
Calculates root and file, then starts notepad++.exe and passes the absolute file path in.
Note that it uses start so that the command finishes executing without waiting for notepad++ to close.
CLI Command
git config --global alias.open "!f() { root=$(git rev-parse --show-toplevel); if command -v grep >/dev/null 2>&1; then file=$(git ls-files | grep -m1 "/[^/]*$1[^/]*$"); else file=$(git ls-files | findstr /R "[^/]*$1[^/]*$" | head -1); fi; if [ -n "$file" ]; then start "" "C:\Program Files\Notepad++\notepad++.exe" "$root/$file"; else echo "No file found matching: $1"; fi; }; f"
.gitconfig Snippet
[alias]
open = !f() { root=$(git rev-parse --show-toplevel); if command -v grep >/dev/null 2>&1; then file=$(git ls-files | grep -m1 "/[^/]*$1[^/]*$"); else file=$(git ls-files | findstr /R "[^/]*$1[^/]*$" | head -1); fi; if [ -n "$file" ]; then start "" "C:\Program Files\Notepad++\notepad++.exe" "$root/$file"; else echo "No file found matching: $1"; fi; }; f