Improve powershell notes in the dev manual.

I now know some things better about powershell.

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
This commit is contained in:
Rafael Kitover 2020-05-11 00:33:30 +00:00
parent 4f0f942e31
commit 710d356084
No known key found for this signature in database
GPG Key ID: 08AB596679D86240
1 changed files with 118 additions and 160 deletions

View File

@ -196,10 +196,6 @@ For calling Windows APIs with strings, use the wide char `W` variants and the
### Windows Native Development Environment Setup ### Windows Native Development Environment Setup
Install Visual Studio 2019:
You just want the core and C++ packages.
Install the chocolatey package manager: Install the chocolatey package manager:
- Press Win+X and open Windows PowerShell (administrator). - Press Win+X and open Windows PowerShell (administrator).
@ -215,101 +211,58 @@ Close the administrator PowerShell window and open it again.
Install some chocolatey packages: Install some chocolatey packages:
```powershell ```powershell
choco install ag dejavufonts git gpg4win hackfont microsoft-windows-terminal poshgit powershell-preview vim-tux zip unzip notepadplusplus openssh diffutils choco install -y visualstudio2019community visualstudio2019-workload-nativedesktop ag dejavufonts git gpg4win hackfont microsoft-windows-terminal powershell-preview vim-tux zip unzip notepadplusplus openssh diffutils neovim
``` ```
Launch the terminal and choose Settings from the tab drop-down, this will open Launch the terminal and choose Settings from the tab drop-down, this will open
the settings json in visual studio. the settings json in visual studio.
Add the Tango Dark Theme to the theme section: Change the powershell (and not the "windows powershell") profile like so:
```json ```json
{ {
"background": "#000000", "name": "PowerShell",
"black": "#000000", "source": "Windows.Terminal.PowershellCore",
"blue": "#3465a4", // If you want a background image, set the path here:
"brightBlack": "#555753", //"backgroundImage": "file://c:/users/rkitover/Pictures/wallpapers/wallhaven-01ge81.jpg",
"brightBlue": "#729fcf", "backgroundImageOpacity": 0.32,
"brightCyan": "#34e2e2", "backgroundImageStretchMode": "uniformToFill",
"brightGreen": "#8ae234", "fontFace": "Hack",
"brightPurple": "#ad7fa8", "fontSize": 10,
"brightRed": "#ef2929", "colorScheme": "Tango Dark",
"brightWhite": "#eeeeec", "cursorShape": "filledBox"
"brightYellow": "#fce94f", },
"cyan": "#06989a",
"foreground": "#d3d7cf",
"green": "#4e9a06",
"name": "Tango Dark",
"purple": "#75507b",
"red": "#cc0000",
"white": "#d3d7cf",
"yellow": "#c4a000"
}
``` ```
Change the powershell profile like so: Make sure it is set as the default profile.
You can use tab shortcuts to get a sort of tmux for powershell, using the same
bindings as kitty, add this to the "keybindings" section:
```json ```json
{ {
"acrylicOpacity": 0.5, "command" : "newTab",
"closeOnExit": true, "keys" :
"colorScheme": "Tango Dark", [
"commandline": "C:\\Program Files\\PowerShell\\7-preview\\pwsh.exe", "ctrl+shift+t"
"cursorColor": "#FFFFFF", ]
"cursorShape": "filledBox", },
"fontFace": "Hack", {
"fontSize": 10, "command" : "nextTab",
"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}", "keys" :
"historySize": 30001, [
"icon": "ms-appx:///ProfileIcons/{574e775e-4f2a-5b96-ac1e-a2962a402336}.png", "ctrl+shift+right"
"backgroundImage": "ms-appdata:///roaming/wallhaven-127481.jpg", ]
"backgroundImageOpacity": 0.32, },
"backgroundImageStretchMode": "uniformToFill", {
"name": "PowerShell Core", "command" : "prevTab",
"padding": "0, 0, 0, 0", "keys" :
"snapOnInput": true, [
"startingDirectory": "%USERPROFILE%", "ctrl+shift+left"
"useAcrylic": false ]
}, },
``` ```
Notice I have a background image, if you want to install one, follow this
guide:
https://www.thomasmaurer.ch/2019/09/how-to-change-the-windows-terminal-background-image/
Change the settings for the tab shortcuts if you want, I use the same config as
kitty:
```json
{
"command" : "newTab",
"keys" :
[
"ctrl+shift+t"
]
},
{
"command" : "nextTab",
"keys" :
[
"ctrl+shift+right"
]
},
{
"command" : "prevTab",
"keys" :
[
"ctrl+shift+left"
]
},
```
This works kind of like a tmux for powershell!
The keys to copy a mouse selection are `Ctrl-Shift-C` like in kitty, to paste
`Ctrl-Shift-V`.
Now add some useful things to your powershell profile: Now add some useful things to your powershell profile:
Run: Run:
@ -318,92 +271,107 @@ Run:
notepad++ $profile notepad++ $profile
``` ```
Here's mine, most importantly the Visual Studio environment setup: (or vim.)
Here's mine, most importantly the Visual Studio environment setup.
If you use my posh-git prompt, you'll need the git version of posh-git:
```powershell
mkdir ~/source/repos
cd ~/source/repos
git clone https://github.com/dahlbyk/posh-git
```
Alternately install "poshgit" from chocolatey.
```powershell ```powershell
chcp 65001 > $null chcp 65001 > $null
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned set-executionpolicy -scope currentuser remotesigned
Set-Culture en-US set-culture en-US
$env:EDITOR = 'C:/Program\ Files/Vim/vim82/vim.exe' ri env:TERM
$env:VBAM_NO_PAUSE = 1 $env:EDITOR = 'c:/tools/neovim/neovim/bin/nvim.exe'
set-alias -name vim -val /tools/neovim/neovim/bin/nvim
function megs { function megs {
ls $args | select Name, @{Name="MegaBytes"; Expression={$_.Length / 1MB}} gci $args | select mode, lastwritetime, @{name="MegaBytes"; expression = { [math]::round($_.length / 1MB, 2) }}, name
} }
function cmconf { function cmconf {
ag 'CMAKE_BUILD_TYPE|VCPKG_TARGET_TRIPLET' CMakeCache.txt ag 'CMAKE_BUILD_TYPE|VCPKG_TARGET_TRIPLET|UPSTREAM_RELEASE' CMakeCache.txt
} }
function sudo { function pgrep {
ssh localhost $args get-ciminstance win32_process -filter "name like '%$($args[0])%' OR commandline like '%$($args[0])%'" | select processid, name, commandline
} }
Set-Alias -name notepad -val "C:\Program Files\Notepad++\notepad++.exe" function pkill {
Set-Alias -name which -val Get-Command pgrep $args | select processid | stop-process
Set-Alias -name grep -val ag }
function taskslog {
get-winevent 'Microsoft-Windows-TaskScheduler/Operational'
}
set-alias -name notepad -val '/program files/notepad++/notepad++'
set-alias -name which -val get-command
set-alias -name grep -val ag
# For vimdiff etc., install diffutils from choco. # For vimdiff etc., install diffutils from choco.
if (Test-Path Alias:diff) { if (test-path alias:diff) { remove-item -force alias:diff }
Remove-Item Alias:diff -force
}
# Load VS env only once. # Load VS env only once.
if (-not $env:VSCMD_VER) { if (-not $env:VSCMD_VER) {
pushd "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build" pushd '/program files (x86)/microsoft visual studio/2019/community/vc/auxiliary/build'
cmd /c "vcvars64.bat & set" | where { $_ -match '=' } | %{ cmd /c 'vcvars64.bat & set' | where { $_ -match '=' } | %{
$v = $_.split('=') $var,$val = $_.split('=')
Set-Item -Force -Path ("ENV:"+$v[0]) -Value $v[1] set-item -force "env:$var" -value $val
} }
popd popd
} }
# Chocolatey profile # Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" $chocolatey_profile = "$env:chocolateyinstall\helpers\chocolateyprofile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile" if (test-path $chocolatey_profile) { import-module $chocolatey_profile }
import-module ~/source/repos/posh-git/src/posh-git.psd1
function global:PromptWriteErrorInfo() {
if ($global:gitpromptvalues.dollarquestion) {
"`e[0;32m✔`e[0m"
}
else {
"`e[0;31m✘`e[0m"
}
} }
Import-Module 'C:\tools\poshgit\dahlbyk-posh-git-9bda399\src\posh-git.psd1' $gitpromptsettings.defaultpromptabbreviatehomedirectory = $true
Import-Module PSReadLine $gitpromptsettings.defaultpromptprefix.text = '$(PromptWriteErrorInfo) '
$HistoryFilePath = Join-Path ([Environment]::GetFolderPath('UserProfile')) .ps_history $gitpromptsettings.defaultpromptwritestatusfirst = $false
$gitpromptsettings.defaultpromptbeforesuffix.text = "`n$env:USERNAME@$env:COMPUTERNAME "
$gitpromptsettings.defaultpromptbeforesuffix.foregroundcolor = 0x87CEFA
$gitpromptsettings.defaultpromptsuffix.foregroundcolor = 0xDC143C
Register-EngineEvent PowerShell.Exiting -Action { Get-History | Export-Clixml $HistoryFilePath } | out-null import-module psreadline
if (Test-path $HistoryFilePath) { Import-Clixml $HistoryFilePath | Add-History } if (-not (test-path ~/.ps_history)) {
new-item -itemtype file ~/.ps_history
}
Set-PSReadLineOption -EditMode Emacs register-engineevent powershell.exiting -action { get-history | export-clixml ~/.ps_history } | out-null
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward import-clixml ~/.ps_history | add-history *> $null
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward set-psreadlineoption -editmode emacs
``` set-psreadlinekeyhandler -key tab -function tabcompletenext
set-psreadlinekeyhandler -key uparrow -function historysearchbackward
You may want to add an alias for your favorite editor. set-psreadlinekeyhandler -key downarrow -function historysearchforward
Relaunch the terminal.
Add the powershell community extensions, to get a port of less and some other goodies:
```powershell
Find-Package pscx | Install-Package -Force -Scope CurrentUser -AllowClobber
```
and restart your shell.
Unrelated, but to get the up/down arrow history search behavior in bash, add
the following to your `~/.inputrc`:
```inputrc
## arrow up
"\e[A":history-search-backward
## arrow down
"\e[B":history-search-forward
``` ```
To set notepad++ as the git commit editor, run this command: To set notepad++ as the git commit editor, run this command:
@ -451,13 +419,6 @@ if (has('win32') || has('gui_win32')) && executable('pwsh')
endif endif
``` ```
To use neovim instead of vim, install the neovim chocolatey package and use this $profile setup:
```powershell
$env:EDITOR = 'C:/tools/neovim/Neovim/bin/nvim.exe'
Set-Alias -name vim -val nvim
```
To use gvim instead of console vim, you could try this `$profile` set up: To use gvim instead of console vim, you could try this `$profile` set up:
```powershell ```powershell
@ -484,11 +445,10 @@ endif
If you don't know how to use vim and want to learn, run `vimtutor`, it takes If you don't know how to use vim and want to learn, run `vimtutor`, it takes
about half an hour. about half an hour.
Now to set up gpg. To set up gpg:
I recommend not using a passphrase so as not to deal with passphrase prompts, I don't use a passphrase on my key, because gpg-agent constantly causes me
which are a huge pain and break both ssh and release automation. If you have a grief, if you want to remove yours see:
passphrase and want to remove it see:
http://www.peterscheie.com/unix/automating_signing_with_GPG.html http://www.peterscheie.com/unix/automating_signing_with_GPG.html
@ -504,8 +464,6 @@ Tell git to always sign commits:
git config --global commit.gpgsign true git config --global commit.gpgsign true
``` ```
Now your development/build environment is ready!
To set up ssh into your powershell environment, which allows doing builds To set up ssh into your powershell environment, which allows doing builds
remotely etc., edit the registry as described here to set powershell-preview as remotely etc., edit the registry as described here to set powershell-preview as
the default shell: the default shell:
@ -533,6 +491,8 @@ For example:
alias | ag sort alias | ag sort
``` ```
The above $profile aliases grep to ag.
Powershell itself provides a nice way to do simple grep/sed operations: Powershell itself provides a nice way to do simple grep/sed operations:
```powershell ```powershell
@ -544,35 +504,35 @@ cmd /c date /T | %{ $_ -replace '.*(\d\d)/(\d\d)/(\d\d\d\d).*','$3-$1-$2' }
For `ls -ltr` use: For `ls -ltr` use:
```powershell ```powershell
ls | sort lastwritetime gci | sort lastwritetime
``` ```
You can use the `-Recurse` flag for `Get-ChildItem` (`ls`, `gci`) as a You can use the `-Recurse` flag for `Get-ChildItem` (`ls`, `gci`) as a
substitute for `find`, e.g.: substitute for `find`, e.g.:
```powershell ```powershell
ls -rec *.xrc gci -rec *.xrc
``` ```
Now combine this with the awesome powershell object pipeline to e.g. delete all Now combine this with the awesome powershell object pipeline to e.g. delete all
vim undo files: vim undo files:
```powershell ```powershell
ls -rec .*un~ | rm gci -rec .*un~ | ri
``` ```
You will notice that `Remove-Item` (`rm`) does not take multiple space You will notice that `Remove-Item` (`rm, ri`) does not take multiple space
separated items, you must separate them by commas, eg.: separated items, you must separate them by commas, eg.:
```powershell ```powershell
1..4 | %{ni foo$_; ni bar$_} 1..4 | %{ni foo$_; ni bar$_}
rm foo*, bar* ri foo*, bar*
``` ```
The equivalent of `rm -rf` to delete a directory is: The equivalent of `rm -rf` to delete a directory is:
```powershell ```powershell
rm -rec -for dir ri -rec -for dir
``` ```
The best replacement for `sudo` is to set up the openssh server with the shell The best replacement for `sudo` is to set up the openssh server with the shell
@ -580,11 +540,9 @@ in the registry pointing to powershell-preview, commands run over ssh are
elevated. For example: elevated. For example:
```powershell ```powershell
ssh localhost choco upgrade all ssh localhost choco upgrade -y all
``` ```
the above `$profile` example has an alias for this.
It should not take you very long to learn enough basic usage for your dev It should not take you very long to learn enough basic usage for your dev
workflow. There is a lot of good info on powershell out there on blogs and workflow. There is a lot of good info on powershell out there on blogs and
stackoverflow/superuser etc.. stackoverflow/superuser etc..