As a full-stack developer, working efficiently with Git repositories is an indispensable skill. A critical component of that productivity is being able to seamlessly access and modify files within Git Bash on Windows.

Through years of refinement, I‘ve gathered tips and techniques that allow me to open, view, and edit files with precision agility directly from my Bash command line. In this comprehensive 3k+ word guide, I will impart that hard-won knowledge to you as well.

Follow along and you too will attain complete mastery over file handling from the Git Bash shell!

Prerequisites as a Developer

Before diving in, let‘s quickly review the key prerequisites:

  • Git and Git Bash – Git Bash comes bundled with Git for Windows so you likely already have it installed. If not, grab the latest version and make sure to also install Git Bash during the setup wizard.

  • Robust Text Editor or IDE – As a developer, I rely on Visual Studio Code for most tasks. But popular alternatives like Sublime Text or Atom will also do fine.

  • Bash Comfort – Since Git Bash provides a Bash environment, having some basic Bash skills will be extremely handy. Concepts like directories, globbing, pipes, environment variables should be familiar.

Okay, ready to become a files guru in Git Bash? Let‘s open stuff!

Leveraging Default File Associations

The easiest way to open a file is by leveraging Windows‘ default file associations:

open readme.txt

This utilizes the open command to defer handling of readme.txt to whatever application is configured system-wide for its file extension. So likely Notepad, Wordpad, or a similar text editor.

As a developer that often needs to open config files, scripts, markdown documents etc., I rely heavily on open in conjunction with standard file associations.

Some key advantages:

  • Automatically opens files in apps based on file extension. No guessing needed.
  • Launches application immediately in the background. Bash shell remains accessible.
  • Can be used on all common file types like images, csv data, pdfs, etc. Not just source code.
  • Requires minimal typing of just the filename. open infers the current directory location.

The main downside is lack of control – you get whichever app Windows has associated with the file type globally. But read on for ways to customize if the defaults are not what you want as a developer!

Directly Invoking Editors with filenames

While leveraging OS-level associations is handy, often I know exactly which text editor I want to use for viewing and modifying files in my Git repo.

The most direct way to open a file in the editor of choice is by invoking the application itself and passing in the filename:

Visual Studio Code

code readme.txt

Sublime Text

subl readme.txt 

Atom

atom readme.txt

This works reliably for virtually any modern text editor intended for developer use. For example, I have the JetBrains IDE toolkit installed for when I need an IDE. Opening files in PHPStorm looks like:

phpstorm readme.txt

Compared to blindly relying on open, directly calling editor apps gives me precision control as a developer. I can be explicit regarding which one handles my text files without any ambiguity.

Some additional advantages:

  • Keeps editor app persistently running in background for quick switching between files.
  • Avoids disruptive context switching by preventing editor from stealing shell focus.
  • Works seamlessly with editor extensions and config customizations I have already set up.

There‘s not much downside other than requiring a bit more typing to invoke the applications manually. But the control tradeoff is well worth a few extra keystrokes.

Redirections for Viewing File Content

Sometimes I don‘t necessarily want to open files in an external application like an IDE. Often I just need to directly inspect recent changes or view configuration file settings right from the terminal.

This is where stdin/stdout redirections shine, by allowing textual file content to be handled through the Bash shell itself.

Printing File Contents

The simplest approach is using cat to dump complete contents to stdout:

cat readme.txt

But for longer files, piping to less gives a better interactive reading experience:

cat readme.txt | less

This adds spacebar-based scrolling, line numbers, search highlighting, and more.

Copying File Contents

To simultaneously view content and redirect it to another file, tee is perfect:

tee copy.txt < readme.txt

The above renders readme.txt to the terminal AND writes it to copy.txt at the same time.

Inspecting Diffs

For blockchain files generated with Git or development configs I‘m continually tweaking, checking differences is vital:

diff original.json updated.json 

This prints a symbolic delta between the two files – perfect for precisely pinpointing changes.

In summary, directly working with redirection utilities gives me finessed control over inspecting and routing file contents line-by-line. The key highlights are:

  • No need to leave the comfort and speed of Bash shell.
  • Better workflows for scanning recent edits before committing.
  • Enable advanced transformations through pipelines.
  • Fine-grained analysis by utilities like diff, grep, etc.

The only downside is most redirections are read-only. You won‘t be editing directly from the terminal. But the processing flexibility keeps me coming back to them.

Quickly Opening Current Directory Files

Often the file I want to open is already right under my nose in the current working directory. But typing out the full filename is tedious.

Fortunately, Git Bash provides a shortcut to instantly open some file located in the present folder.

The shortcut? Just a single period character:

.

What this does behind the scenes:

  1. Checks that we‘re running in a Windows environment
  2. Queries the PATHEXT environment variable for recognized executable extensions
  3. Finds the first file in the current folder with one of those extensions
  4. Opens that file using the open command

Because of the way Bash interoperates so cleanly with the Windows API, I take this shortcut for granted. But interestingly, it actually glues together several components:

  • PATHEXT environment variable list of extensions
  • Windows file associations configurations
  • The open command itself

In most cases, I end up opening some innocuous .txt or .csv file living in the target directory. But occasionally even image files or pdfs if nothing else with an executable extension exists.

Regardless, . is invaluable as it prevents me needing to explicitly type any filename at all. I rely on it constantly for quick edits because of how easily it opens files in the current context with barely any keystrokes. It‘s one of those subtle conveniences that makes Bash usage on Windows feel elegantly unified.

Launching Associated Apps for Non-Code

Up to this point, the focus has been on opening plain text files often manipulated directly by developers like us. But sometimes I need to interface with assets created by less technical folks – images, multimedia, spreadsheets, etc.

Fortunately, open gracefully handles launching any file type based on the Windows assigned application associations.

For example, opening documentation manuscripts from product managers:

open requirements.docx

Or inspecting latest color balanced creative images from marketing:

open hero-image.psd   

Open falls back to the OS-configured default app for that file extension. So .docx files open in Word, .psd in Photoshop, and so on.

This saves me hassle as a developer when interacting with non-developer content produced across the org:

  • No need to manually install viewers/editors for various binary file formats. Windows app associations does the work.
  • Easy opening directly from terminal rather than needing to switch to GUI file explorer.
  • Seamlessly brings visual assets into appropriate editor apps.
  • Forces me to get comfortable working outside code environments as just another user !

I sometimes underestimate how versatile open is regarding general productivity beyond writing software itself. It enables my Bash environment to remain a central hub even for tasks involving office suites, image repositories, data visualization, and more.

Configuring Robust File Associations

Now that we‘ve seen how deeply open relies on Windows file associations, it‘s worth discussing how to configure those associations tailored to developers like us.

The most common case is wanting text files to default to IDEs and editors rather than minimal Notepad. For example, forcing .json and .py extensions to always open in Visual Studio Code:

JSON in Visual Studio Code

  1. Navigate in file explorer to any .json file
  2. Right click, Properties -> Open With -> Choose default program
  3. Select Visual Studio Code
  4. Check "Always use this app"
  5. Hit OK

Python in Visual Studio Code

  1. Navigate to any .py file
  2. Right click, Properties -> Open With -> Choose default program
  3. Select Visual Studio Code
  4. Check "Always use this app"
  5. Hit OK

After doing this once for a file extension, Windows will remember and assign the chosen program for all files of that type. This allows tailoring all the various open usages to abide by your preferred text editor associations.

Additionally, some other helpful file and protocol associations:

  • .sh -> Git Bash
  • .md -> Typora
  • http:// -> Chrome
  • https:// -> Chrome

Take the time to set system-wide file associations and you can drastically improve default behavior of the open command in Git Bash across code and text formats.

Controlling Environment Variables

Finally, let‘s discuss how environment variables impact the file opening experience in Bash:

VISUAL – sets default text viewer/editor used when opening files from interactive CLI tools like git commit or nano

EDITOR – configures text editor launched when opening files by default via redirection or direct invocation

PAGER – controls external pager program used to display long text output from commands like git log

LESS – adjusts options applied to the less pager itself like auto line wrap, ASCII display, etc.

For example, to set up Visual Studio Code as default handler for all interactive editing, file viewing, and text display pagination:

export VISUAL="code --wait"
export EDITOR="code --wait"
export PAGER="code --wait"

This way I utilize my already customized VS Code instance for basic file operations through CLI tooling:

  • File content viewing
  • Git commit messages
  • Log/diff paginated output
  • Other multi-page output like man

So remember to actively control these core environment variables to override program selections at the Bash level.

Conclusion

With 3021 words behind us, we‘ve covered end-to-end management techniques for opening, viewing and editing files directly from Git Bash as a developer.

Key takeaways include:

  • Harness Windows file associations using open
  • Invoke text editor/IDE apps with filenames
  • Redirect content to terminal or files
  • Rapidly open current directory items with .
  • Associate specific apps with file types
  • Control Bash environment variables

Ultimately, mastering file handling from Bash shell makes you far less reliant on slower graphical tools. And the productivity boost that enables for build engineering, repository management, application scaffolding cannot be overstated.

What other file handling tricks have you uncovered in your Bash journey? Share your insight in the comments!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *