How to improve your console experience on windows
Tags: git, powershell, vscode, heroku
Published: 2019-11-12 16:37:52

---
title: How to improve your console experience on windows
published: true
description: Powershell isn't given much love, but I tried to improve my experience with it a little and wanted to share with you guys some useful things I implemented. Also, there are some tips and tricks that you can use for git and others :)
tags: git, PowerShell, VScode, heroku
---

On windows you have many options, you can use git-bash, cmd, wls (to use Linux on the cli) and Powershell. While wsl is nice, I personally wanted to use a native option so I wouldn't have to wait too much for the console start-up and didn't want to deal with the file paths being different.

The usual `cmd.exe` isn't very powerful so I chose the PowerShell instead. If you want to live on the edge you can also install its beta version

https://github.com/PowerShell/PowerShell/releases

And hey, if you ever need to use bash or wsl, as long you have it installed, here is a nice trick:

- Open up Powershell and type:
  - `bash` to switch to git-bash
  - `ubuntu` to switch to the ubuntu installed on wsl
  - `cmd` to switch to the usual `cmd.exe`
- Type `exit` to go back to PowerShell (or the previous console, since you can do it on cmd too)

This not only lets you use the commands for a different shell fast but also preserves the path you are in, so you can easily open your project using `open with vscode` in a folder for example, then switch to wsl if needed

## The PowerShell `$profile`

Powershell has a profile file that is loaded when you open the terminal, it can be accessed with the variable `$profile`, you can type `code $profile` if you have VScode. This will open the profile file for you.

With that, you can now set up a lot of things to be loaded every time you open PowerShell.

### Powershell Modules

Here are some interesting things to get started:

#### Git information:
https://github.com/dahlbyk/posh-git

#### Colors:
https://github.com/PoshCode/Pansies
https://github.com/Davlind/PSColor

#### Themes:
https://github.com/JanDeDobbeleer/oh-my-posh

#### Completions and a lot more:
https://github.com/PowerShell/PSReadLine

#### Fun (fix typos):
https://github.com/mattparkes/PoShFuck

#### Others:
https://github.com/janikvonrotz/awesome-powershell

Those modules should make your life easier and also add some things windows is missing regarding autocompletion, themes and etc.


### Creating aliases

The next thing to do would be creating your own aliases for things you do a lot, for that we have 3 simple options: functions, variables and Set-Alias.

There are some modules that may implement some for you already, but I personally like to make my own, so I remember them better.

let's start with a basic one:

`Set-Alias g git`

this will alias the `git` command to a `g` command. It isn't very helpful in this case, but you get the concept. It has the limitation of not working with commands with parameters, so something like this:

`Set-Alias gp git pull`

will not work :/, but for that, we can use functions instead:

```powershell
function pull {
  git pull
}
```

pretty simple, no? If you add this after the imports of your `$profile` file, next time you load powershell you will be able to just type `pull` (or whatever you want) instead of `git pull`.

if you still want to be able to add the branch you can use parameters like so:
```powershell
function pull($a,$b) {
  git pull $a $b
}
```
But that is not really good since you might want to add even more parameters. For that, we can use the `$args` variable instead.

```powershell
function pull {
  git pull $args
}
```

Now everything we pass to the `pull` command will be also passed to the `git pull` command.

If you want to run multiple commands it's also pretty simple:

```powershell
function sync {
  git pull
  git push
}
```

This is pretty trivial for now, but I'll share more useful ones after.

let's see what variables can do. Variables in PowerShell are prefixed with `$` and to declare them you just need to do: `$foo = "bar"` and you get the string `bar` every time you use `$foo`.

A useful thing to do with it is to store the current project path in a variable so in the future you can do `code $project`, `cd $project` or `explorer $project`

To get the current project path we need to type `Get-Location` in PowerShell.

you can store it on a temporary variable by using `$myVar = Get-Location` directly on the cli or you can copy-paste it into the profile file so you can use it later. A temporary variable can be very useful if you want to navigate to a lot of places with the cli during an operation.

now that you know about those you can start doing some useful functions and variables:

#### Getting the current path
```powershell
# Everytime you open the console, the initial path will be stored here.
$cwd = Get-Location

# Alias for Get-Location
function path {
  Get-Location
}
```

#### Improving git experience:

On git there is a very useful variable called `HEAD`, it represents the current branch and we can use it to make life easier.

```powershell

# A even simpler version of the git pull alias
function gp {
  git pull $args
}

# Sync git changes
function sync {
  git pull
  git push
}

# This will push and set-upstream to the remote branch
function pup {
  git push --set-upstream origin (git symbolic-ref --short HEAD)
}

# If you have an issue tracker and need to follow a naming pattern a function 
# like this may help you out. It will create the branch and checkout to it
function issue($jiraId, $title) {
  git checkout -b your_name/$jiraId-$title
}

# This one gets more advanced with some parameter validation. 
# I'm not an expert on PowerShell so I can't give much information on it, but 
# it should be pretty simple to understand.

# It will force push the current branch to a Heroku QA branch 
# based on the number of the QA branch.
function qa {
  param(
    [Parameter(Mandatory=$true)]
    [Int]
    $qaBranch
  )

  echo('deploying current branch to qa' + $qaBranch)
  git push origin HEAD:qa$qaBranch --force
}
```

#### Some other ideas to explore
- A function for configuring Heroku env variables (so you can easily set-up qa, staging and production common variables).
- NPM related functions
- modules install packs
- commit patterns