Boost Your Productivity with Shell Aliases: A Simple Hack for Flutter Developers
Sinnoor C
Posted on October 4, 2024
As Flutter developers, we often perform repetitive tasks — like cleaning builds, getting dependencies, and running code generators. While these tasks are necessary, they can quickly become time-consuming and disrupt our focus. Luckily, there's a simple productivity hack that can help: shell aliases.
What Are Shell Aliases?
Shell aliases are shortcuts you can create for commonly used commands in your terminal. Instead of typing out a long command every time, you set up a short keyword that performs the same action. It's a small change that can save you a lot of time and keystrokes throughout the day.
Common Repetitive Tasks in Flutter Development
If you've been developing with Flutter, you're probably familiar with the following commands:
-
Cleaning your project (
flutter clean
) -
Getting dependencies (
flutter pub get
) -
Upgrading dependencies (
flutter pub upgrade
) -
Running build_runner (
dart run build_runner build
) -
Watching build_runner (
dart run build_runner watch
)
These are vital for any Flutter project but can get tedious when you have to run them frequently. Shell aliases help you automate these repetitive tasks.
My Experience with Shell Aliases
A while back, I noticed that I was spending too much time typing the same commands repeatedly. It was distracting me from focusing on actual coding. That's when I decided to create a few aliases to speed things up, and they've been a game-changer for me.
Here are some of the aliases I use:
Flutter Aliases
alias fclean="flutter clean"
alias fpg="flutter pub get"
alias fpu="flutter pub upgrade"
Instead of typing flutter pub get
every time, I now just type fpg
, saving me a few keystrokes and keeping my flow intact.
Build Runner Aliases
alias brb="dart run build_runner build -d"
alias brw="dart run build_runner watch -d"
If you work with generated code in Flutter, you're likely familiar with build_runner
. These aliases help me quickly run builds or start watching for changes.
Combined Aliases
I found myself frequently needing to run pub get
before using build_runner
, so I combined them into one command:
alias fpgbrb="fpg && brb"
alias fpgbrw="fpg && brw"
Now, when I want to update dependencies and rebuild my code, I just use fpgbrb
. If I want to watch for changes, I use fpgbrw
. It's a small change, but it saves a lot of time over the course of a project.
Shell Aliases on Windows
If you're working on Windows, you can still benefit from the power of aliases. Although Windows doesn't support shell aliases in the same way as macOS or Linux, there are alternatives:
PowerShell Aliases
PowerShell offers similar functionality to Unix-like shells. You can create aliases using the Set-Alias
command:
Set-Alias fclean "flutter clean"
Set-Alias fpg "flutter pub get"
Set-Alias fpu "flutter pub upgrade"
To make these aliases permanent, add them to your PowerShell profile by running notepad $profile
and entering your alias commands. Save the file, and they’ll be available every time you open PowerShell. For more details, you can refer to the Set-Alias in PowerShell documentation.
Batch Files in CMD
In the traditional Windows Command Prompt (CMD), you can use batch files to create shortcuts for common commands:
- Create a
.bat
file, such asfclean.bat
, with the following content:
@echo off
flutter clean
- Place this file in a folder that's included in your system's PATH environment variable.
Now, you can run fclean
from any Command Prompt to clean your Flutter project.
Using Windows Subsystem for Linux (WSL)
Another option is to use the Windows Subsystem for Linux (WSL), which gives you a Linux-like terminal on Windows. With WSL, you can follow the same steps for creating aliases as you would on macOS or Linux by editing your .bashrc
or .zshrc
file. To set up WSL, check out the official Windows Subsystem for Linux documentation.
iOS Aliases
For iOS development, I've also created a few handy shortcuts:
alias pinst="pod install"
alias pru="pod repo update"
Instead of typing out pod install
or pod repo update
, these aliases do the same thing with much less effort. These are especially helpful if you're frequently switching between Flutter and native iOS development.
Creating Your Own Aliases
Creating shell aliases is easy! Here's how to get started:
For macOS/Linux:
- Open your terminal and type
nano ~/.bashrc
(or~/.zshrc
if using Zsh). - Add your alias commands, like
alias fclean="flutter clean"
. - Save the file and run
source ~/.bashrc
(orsource ~/.zshrc
) to apply the changes.
For Windows:
- Use PowerShell's
Set-Alias
command or create batch files for CMD. - Alternatively, set up WSL and use aliases just like you would on Linux.
A Few Tips:
- Think about the commands you run most often.
- Start small and expand your list of aliases as you identify more repetitive tasks.
- Combine related commands for even greater efficiency.
Why Use Shell Aliases?
In Flutter projects, particularly those involving code generation or frequent updates, shell aliases are a huge time saver. Tasks like running pub get
and build_runner
are done so often that even saving a few seconds each time can add up to hours over the course of a project. With aliases, you can focus more on your code and less on repetitive commands.
For example:
-
fpgbrb: Automatically runs
flutter pub get
andbuild_runner build
. -
fpgbrw: Runs
flutter pub get
and thenbuild_runner watch
to keep an eye on changes.
These shortcuts keep my workflow efficient and uninterrupted.
Final Thoughts
If you haven't tried using shell aliases yet, I encourage you to give it a go. Tailor them to your workflow, and you'll quickly see how much time and effort they can save you. With just a little automation, you can spend less time typing commands and more time building awesome Flutter apps!
If you have any questions or suggestions, please leave a comment below. 😍 Thanks for reading!
Posted on October 4, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024