Last active
July 27, 2024 01:46
-
-
Save bradwilson/5b6d1ad8f797eee304f7028bf16253b2 to your computer and use it in GitHub Desktop.
Replace cat with bat in PowerShell (and my personal bat config file)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is to replace PowerShell's 'cat' with 'bat'. Install from here: https://github.com/sharkdp/bat | |
# | |
# This code goes into my $profile. If you put this into a separate file, you will need to dot source | |
# that file so that the function shows up in the global namespace. I only replace the cat alias, | |
# so Get-Content still performs its standard behavior. | |
# | |
# I use a pipeline to capture all the file content (so you can do things like `dir *.ps1 | cat` as well | |
# as `cat *.ps1`). If there's a single file, then the contents are displayed raw; if there are multiple files, | |
# then it uses bat's header to discriminate the content of multiple files from each other. I also replicated | |
# a few of the more common (for me) command line switches that I'd want to use to influence bat's behavior, | |
# including paging support. | |
# | |
# I've added the config file I use below to add a few common file types for me with syntax maps. | |
# See `bat --config-file` for the location of your config file. | |
$batSource = (get-command bat).Source | |
if ($null -ne $batSource) { | |
if (test-path alias:/cat) { remove-item -force alias:/cat } | |
function cat { | |
param( | |
[Parameter(Mandatory,ValueFromPipeline)][System.IO.FileInfo[]]$Path, | |
[switch]$Diff, | |
[string]$Language, | |
[string][ValidateSet("unicode","caret")]$NonPrintableNotation, | |
[string][ValidateSet("auto","always","never")]$Paging, | |
[switch]$ShowAll, | |
[string[]][ValidateSet("default","full","auto","plain","changes","header","header-filename","header-filesize","grid","rule","numbers","snip")]$Style | |
) | |
begin { | |
$batArgs = [System.Collections.ArrayList]@() | |
} | |
process { | |
$Path | ForEach-Object { | |
$fullPath = Resolve-Path $_ -ErrorAction Continue | |
if ($null -ne $fullPath) { | |
$batArgs.AddRange([Array]$fullPath) | |
} | |
} | |
} | |
end { | |
if ($batArgs.Count -eq 0) { return } | |
if ($batArgs.Count -gt 1) { $batArgs.AddRange(@("--style", "header-filename,grid")) } | |
else { $batArgs.AddRange(@("--style", "plain")) } | |
if ($Diff) { $batArgs.Add("--diff") } | |
if ("" -ne $Language) { $batArgs.AddRange(@("--language", $Language)) } | |
if ("" -ne $NonPrintableNotation) { $batArgs.AddRange(@("--nonprintable-notation", $NonPrintableNotation)) } | |
if ("" -ne $Paging) { $batArgs.AddRange(@("--paging", $Paging)) } | |
if ($ShowAll) { $batArgs.Add("--show-all") } | |
if ($Style.Count -gt 0) { $batArgs.AddRange(@("--style", [string]::Join(",", $Style))) } | |
& $batSource $batArgs | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--map-syntax "*.csproj:XML" | |
--map-syntax "*.fsproj:XML" | |
--map-syntax "*.nuspec:XML" | |
--map-syntax "*.props:XML" | |
--map-syntax "*.targets:XML" | |
--map-syntax "*.vbproj:XML" | |
--paging never | |
--tabs 4 | |
--theme="GitHub" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment