banner
Barry

Barry

偶尔摆烂,经常偶尔.
twitter
github
tg_channel
medium
email
steam_profiles

Make Windows Terminal more user-friendly (v2)

Preface#

A few months ago, I wrote an article Making Windows Terminal Easier to Use, which mainly introduced how to improve the appearance of Windows Terminal by installing oh-my-posh, installing Nerd fonts to fix the issue of missing oh-my-posh font icons, solving the problem of automatic disconnection after a long time of inactivity in SSH connections, enabling focus mode, and removing annoying startup copyright information and default administrator startup. In fact, it only made it look better without much improvement in usability. But today, we will upgrade our WT again.

Installing Necessary Tools#

First, Windows Terminal is definitely necessary, and then you need to install PowerShell. Both can be downloaded and installed from the Microsoft Store, as shown in the following figure. If the Microsoft Store cannot be opened, check your network, as it is usually a network problem.
gh

I won't go into the installation of oh-my-posh here, you can refer to my previous article. Finally, let's install ni, a command-line tool developed by antfu to simplify our command operations and automatically select package managers. It's a lazy person's tool. If you haven't used it yet, start using it now. Just follow the documentation and run a command to install it. However, there may be alias conflicts when using it on Windows. I will mention the solution later in this article.

Exploration Process and Overview of Principles#

I want to try using Windows Terminal for development. Windows Terminal can use PowerShell, cmd, and wsl bash as scripting tools by default. Since we are in a Windows environment, we should respect PowerShell. So let's change the default shell of wt to the PowerShell we just installed, as shown in the following figure.
gh

Actually, the initial inspiration came from watching antfu livestreams. He always types some extremely short commands in the command line to perform certain operations, which left me puzzled. Finally, I realized that he had set some aliases for his shell and even open-sourced his zsh configuration file configuration file (Mac users are happy because they can just copy it 😂). But since I don't have money to buy a Mac and I only have Windows, how can I achieve similar functionality on Windows Terminal? So I started my experiment.
If you search for the keywords windows powershell set user alias, Google usually gives you the official Microsoft documentation. However, this document only tells us how to set temporary aliases in scripts. What if we want to set permanent aliases? In fact, this kind of thing, alias, or alias, almost all scripting languages do not have so-called "permanent aliases" (Permanent alias). When we use script tools like Linux bash or Cmder to open the terminal, the system will execute a script file by default (bash is .bashrc in the user directory, Cmder is config/user_aliases.cmd), and this script file contains alias definitions. That's why when we modify .bashrc in Linux-like systems, we must log out and log in again, or use source .bashrc.
Therefore, we just need to modify the file executed when PowerShell starts. Many forums say that the default script executed is $Home\Documents\profile.ps1, which is C:\Users\YourUsername\Documents\profile.ps1, but this is not correct. The best way is to start PowerShell first, and then execute echo $profile to get the file path, which is the default execution file path of PowerShell. And here we need to pay attention that if you have configured it according to my previous article, and then reinstall PowerShell today and set it as the default shell of wt, then you need to reconfigure it because the PowerShell from the Microsoft Store and the Windows built-in Windows PowerShell are not the same version, so they do not share the same configuration file. The approximate path is as follows:

gh

Then, just create this file.

In the file, write the alias setting statements. Once again, if the command referred to by your alias contains spaces, you cannot use the New-Alias command because it does not support spaces, even if you enclose the command in quotes. So what should we do? After asking GPT, I found out that the correct way is to use function. In other words, we define the command we want to define as a function.
Save the file and restart PowerShell. Unexpectedly, you should see an error message saying File xxxxxxx\Microsoft.PowerShell_profile.ps1 cannot be loaded because running scripts is disabled on this system. According to this link, this happens because the Windows system does not allow scripts to run automatically by default in order to prevent malicious scripts from executing automatically. So, if you are confident that you can control it, run Set-ExecutionPolicy RemoteSigned in PowerShell as an administrator.
Restart PowerShell again, and you should find that the custom aliases are now effective.

Resolving Alias Conflicts and Parameter Optimization#

After performing the above operations, you will find that the aliases you set are indeed available, but some of them may conflict with the aliases already existing in PowerShell, such as ni, gp, gcm, and there may be many more in the future. Our solution is to simply remove the conflicting aliases. Add the following code to your configuration file:

Remove-Alias -Name ni -Force
Remove-Alias -Name gp -Force
Remove-Alias -Name gcm -Force

Okay, the conflict problem is solved, but there is still one more problem. You will find that when you use some aliases that require passing parameters, the parameters cannot be passed. We need to modify our alias functions to be able to receive parameters. Of course, you don't need to worry about aliases that don't require parameters. We just need to add $args after our command. It is an automatic variable that contains an array of undeclared parameters for the current script, function, or script block. In short, it captures all parameters that are not captured by pre-defined parameters. For example, the following:

function gcl { git clone $args }
function gst { git stash $args }
function grm { git rm $args }
function gmv { git mv $args }

If you want to be more powerful and have higher customization, you can define your own parameters and prompts as follows:

function gcm {
    param (
        [Parameter(Mandatory=$true)]
        [string]$message
    )
    
    if (-not $message) {
        Write-Host "Error: You must provide a commit message." -ForegroundColor Red
        return
    }
    
    git commit -m $message
}

In short, there are many ways to play with it. You can study it slowly by yourself. Here is a configuration file that I currently use. If you want to change the theme, you can follow the documentation to find the name of the JSON theme file you like and replace the first line of the configuration file. If you have any other questions, you can refer to my previous article.

oh-my-posh init pwsh --config $env:POSH_THEMES_PATH\montys.omp.json | Invoke-Expression
cls
function s { nr start }
function d { nr dev }
function b { nr build }
function t { nr test }
function lint { nr lint }
function release { nr release }


function gs { git status }
function gp { git push }
function gpf { git push --force }
function gpl { git pull --rebase }
function gcl { git clone $args }
function gst { git stash $args }
function grm { git rm $args }
function gmv { git mv $args }

function main { git checkout main }
function gco { git checkout $args }

function gcob { git checkout -b $args }

function gb { git branch }
function gbd { git branch -d $args }
function gbD { git branch -D $args }

function grb { git rebase $args }
function grbc { git rebase --continue }
function gme { git merge $args}

function gl { git log }
function glo { git log --oneline --graph }

function ga { git add }
function gA { git add -A }

function gc { git commit }
function gcm {
    param (
        [Parameter(Mandatory=$true)]
        [string]$message
    )
    
    if (-not $message) {
        Write-Host "Error: You must provide a commit message." -ForegroundColor Red
        return
    }
    
    git commit -m $message
}


function gca { git commit -a }
function gcam { git commit -am $args }


function l { cls }
function q { exit }

function fuck { shutdown /s /f /t 0 }

Remove-Alias -Name ni -Force
Remove-Alias -Name gp -Force
Remove-Alias -Name gcm -Force

Just enjoy it.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.