aboutsummaryrefslogtreecommitdiff
path: root/powershell/Microsoft.PowerShell_profile.ps1
blob: dc824e13f517b16e2ec02507e13fb5b8f691e7c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -----------------------------------------------------------------------------
#     - MODULES -
# -----------------------------------------------------------------------------

Import-Module -Name PSFzf -ErrorAction 'SilentlyContinue'
Import-Module -Name posh-git -ErrorAction 'SilentlyContinue'

if ($IsWindows) {
    $env:XDG_CONFIG_HOME = "$env:USERPROFILE/.config"
    if (-not(Test-Path -Path $env:XDG_CONFIG_HOME -PathType Container)) {
        New-Item -Path $env:XDG_CONFIG_HOME -ItemType Directory
    }
} else {
    $env:XDG_CONFIG_HOME = "$env:HOME/.config"
}

# -----------------------------------------------------------------------------
#     - PROMPT -
# -----------------------------------------------------------------------------

if (Get-Command 'oh-my-posh' -ErrorAction 'SilentlyContinue') {
    oh-my-posh init pwsh --config "$env:XDG_CONFIG_HOME/oh-my-posh/config.yml" | Invoke-Expression
} else {
    function prompt {
        $git_branch = $(git branch --show-current 2>/dev/null)
        if ($git_branch -ne $null -and $git_branch -ne "") {
            $git_branch = "($($git_branch))"
        }
        "[pwsh] `e[01;32m$($env:USER)@$([System.Net.Dns]::GetHostName())`e[00m:`e[01;34m$(Split-Path -Path $pwd -Leaf) `e[1;35m$git_branch`e[00m`$ "
    }
}

# -----------------------------------------------------------------------------
#     - ENVIRONMENT VARIABLES -
# -----------------------------------------------------------------------------

$env:PATH += ":$env:HOME/go/bin"
$env:EDITOR = "vim"
$env:VISUAL = "code"

# -----------------------------------------------------------------------------
#     - CUSTOM FUNCTIONS -
# -----------------------------------------------------------------------------

function New-Note {
    param(
        [Parameter()]
        [string] $Name = "{0}.md" -f (New-Guid)
    )

    $path = Join-Path $env:HOME "notes" $Name

    "$env:EDITOR $path" | Invoke-Expression
}

function Open-Note {
    param(
        [Parameter()]
        [string] $Name = ""
    )

    $notesPath = Join-Path $env:HOME "notes"
    $path = Join-Path $notesPath $Name

    if (Test-Path $path) {
        "$env:EDITOR $path" | Invoke-Expression
    } else {
        "$env:EDITOR $notesPath" | Invoke-Expression
    }
}

# -----------------------------------------------------------------------------
#     - PSREADLINE CONFIGURATIONS -
# -----------------------------------------------------------------------------

Set-PSReadLineOption -PredictionSource History
Set-PSReadLineKeyHandler -Chord "Ctrl+f" -Function ForwardWord
Set-PSReadLineKeyHandler -Chord "Alt+RightArrow" -Function ForwardWord
Set-PSReadLineKeyHandler -Chord "Alt+LeftArrow" -Function BackwardWord
Set-PSReadLineKeyHandler -Chord "Alt+LeftArrow" -Function BackwardWord
Set-PSReadLineKeyHandler -Chord "Ctrl+Backspace" -Function DeleteWord

if (Get-Command 'Set-PsFzfOption' -ErrorAction 'SilentlyContinue') {
    Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'
}

# --- Hide sensitive information from history
Set-PSReadLineOption -AddToHistoryHandler {
    param(
        [string]$line
    )
    $sensitive = "password|asplaintext|token|secret"
    return ($line -notmatch $sensitive)
}

Set-PSReadLineOption -Colors @{ Member = "`e[95m"; Number = "`e[95m" }
Set-PSReadLineOption -Colors @{
    Member    = "`e[95m"
    Parameter = "`e[97m"
    Number    = "`e[97m"
}

# -----------------------------------------------------------------------------
#     - ALIASES -
# -----------------------------------------------------------------------------
function ListFilesAndFolders { param([string]$path = ".") Get-ChildItem -Path $path }
Set-Alias -Name ll -Value ListFilesAndFolders

function ListAllFilesAndFolders { param([string]$path = ".") Get-ChildItem -Path $path -Force }
Set-Alias -Name la -Value ListAllFilesAndFolders

function ListAllFilesAndFoldersSorted { param([string]$path = ".") Get-ChildItem -Path $path -Force | Sort-Object LastWriteTime }
Set-Alias -Name lss -Value ListAllFilesAndFoldersSorted

function GitCommitAlias { git commit -m $args[0] }
Set-Alias -Name gcmm -Value GitCommitAlias

function GitStatusAll { git status -uall }
Set-Alias -Name gsa -Value GitStatusAll

function GitCommitPatch { git commit -p }
Set-Alias -Name gcp -Value GitCommitPatch

function GitAddPatch { git add -p $args }
Set-Alias -Name gap -Value GitAddPatch

Set-Alias -Name vi -Value vim