Friday, September 30, 2022

Vim and Windows? YES!Vim and Windows? YES!


# Installing vim-console on Windows 2022 from PowerShell
Working with Windows Containers has provided me tremendous education.  Learning PowerShell has shown me how far the windows realm has come in the form of CLI management.  It still seems to be lacking in a few areas, and I really don't like the extremely verbose syntax nor the single dash arguments.  But I digress.  This post is about solving a single problem.  The one problem with endless obtuse workarounds.

## Problem
Editing text files...from a windows CLI (cmd.exe, PowerShell, etc).   

## Solution
Find the Microsoft equivalent of Vim.   Is that Notepad.exe?  Wordpad.exe?  Get-Contents?   
Fail, Fail, Mostly-Fail

## My Solution
Install vim-console; an extremely powerful text editor that has proven to be the staple of all *nix users around the world for decades.

Note: There are many articles similar to[How to Edit Files with a Real PowerShell Text Editor](https://adamtheautomator.com/powershell-text-editor/) that show how to use Chocolaty to install Vim, Emacs or Nano.  However, most of my use cases don't allow Chocolaty in their datacenters.   So, we have to install vim the hard way.

### Steps.
1) SSH to your server.   
Don't know how to setup ssh on Windows?  I will point you at their documentation 
    - [Getting Started OpenSSH for Windows](https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse?tabs=powershell)
2) Verify you are in PowerShell 
    Type the command powershell or pwsh and hit enter
        image
    Look for the PS in front of your command prompt.
        image
3) Download vim-console artifacts [NLUUG - dunno where I found this](https://ftp.nluug.nl/pub/vim/pc/)
    - wget https://ftp.nluug.nl/pub/vim/pc/vim90rt.zip -o vim90rt.zip
    - wget https://ftp.nluug.nl/pub/vim/pc/vim90w32.zip -o vim90w32.zip
4) Extract archives
    - expand-archive .\vim90w32.zip -DestinationPath .
    - expand-archive  -Force .\vim90rt.zip -DestinationPath .
5) Run installer
    - cd vim\vim90
    - run .\install.exe
    - Change settings as needed and say `d` for do it
        image

6) Run and Profit
    - Simply type `vim` and hit enter
    - To exit the key sequence `Esc-:-q-` will exit.   

## Conclusion
I didn't go into a lot of detail but hopefully this helps with the steps for using VIM.   

-Nate

Monday, September 19, 2022

Wrangling bash_history across multiple sesions

## BASH_HISTORY_FILE or HISTFILE
I find myself constantly using multiple terminals on the same system.  The default bash history is a good start, but not enough for my needs.  After endless searching, testing, ignoring, deleting, and procrastinating I finally found a solution that works for me.   

1. Session Isolation 
    - I don't need new history items available across sessions
    - Typically the work I am preforming is isolated but similar enough that having commands updated between sessions is both obnoxious and unhelpful.
2. Custom Timestamp 
    - I like seeing a particular timestamp format in my history output (helps me remember what I was doing at that time)
3. Dedupe
    - Like I mentioned above, I don't need commands shared across terminal windows
    - Also, I don't like duplicate uses of command in my history
    - Sure I may lose the first, or prior, occurrence(s) of a command.  But I only require the last use.
4. Write on session exit
    - I don't want to lose my history to a `kill -i `
    - I also don't want to lose my history if I close the wrong session tab
    - dedupe only needs to happen at this point
5. Use only linux bash commands
    - I don't want to download some fancy utility.
    - use the fewest number of commands
6. Unlimited history
    - computers are big enough and fast enough to handle this
    - new versions of bash allow an unlimited history


## `~/.bashrc`

```
HISTCONTROL=ignorespace:ignoredups:erasedups
shopt -s histappend
HISTSIZE=-1
HISTFILESIZE=-1
HISTTIMEFORMAT="[%b %d,%Y %T] "
PROMPT_COMMAND="history -a;${PROMPT_COMMAND}"
```


## `/etc/profile.d/00-dedup-bash-history-on-exit.sh`

```
function deduphistory {
  echo "deduphistory"
  history -w ~/.bash_history.dedup_backup
  history -n; history -a
  tac ~/.bash_history | awk '!x[$0]++' | tac > ~/.bash_history_new_dedup
  history -cr ~/.bash_history_new_dedup
  history -w
}
# cleanup bash history on exit
trap deduphistory EXIT
```


## Profit
Watch your history get appended and deduped on exit.  
No more lost history (mostly)
And be happy


### Files available as gists
https://gist.github.com/therevoman/a7532af575479f0a2b5edfbb0bef8212

Originally posted on github.
https://github.com/therevoman/nate-revo-blog/issues/2 

Adding nerdctl to an OpenShift 4 Windows Node

# Getting a Windows Node on OpenShift ... Not what this post is about, follow the Red Hat and Microsoft documentation and then consult with...