Sunday, May 11, 2014

Batch and Powershell Nano Syntax Highlighting

NOTE: Please see my better, shorter, and updated versions here.

Another quick update for the few that may venture into this... I've recently had a few quirks when trying to read batch and powershell files in nano on Linux.... I love my Nano editor but it doesn't have nanorc files for syntax highlighting regarding Batch or Powershell. Heck, who can blame them! Windblows? No thanks! Yuck!

But sadly... That's not how the real world works so... I had to write my own to get it to work since apparently no on one on the internet has ever made them for nano.

## Here is an example for Batch scripts.
## CREATED BY MITCH BUMGARNER ON MAY 10TH 2014

syntax "bat" "\.(bat|cmd)$"
icolor brightred "%%."
icolor red "(do|else|exist|exit|for|goto|if|not|return|say|select|then|when)"
icolor magenta "^call :?[a-zA-Z0-9_].*$"
icolor brightmagenta "([[:space:]]equ[[:space:]]|[[:space:]]neq[[:space:]]|[[:space:]]gtr[[:space:]]|[[:space:]]lss[[:space:]]|[[:space:]]geq[[:space:]]|[[:space:]]leq[[:space:]])"
color brightyellow "=="
icolor brightcyan "(ansi|append|assign|assoc|attrib|at|autofail|backup|basedev|boot|break|buffers|cache|cacls|cd|chcp|chdir|chkdsk|chkntfs|cls|cmd|codepage|color|command|compact|comp|convert|copy|date|del|dir|diskcomp|diskcopy|doskey|echo|endlocal|erase|fc|find|findstr|format|ftype|graftabl|help|keyb|label|md|mkdir|mode|more|move|path|pause|popd|print|prompt|pushd|rd|recover|rename|ren|replace|restore|rmdir|run|set|setboot|setlocal|shift|sort|start|subst|time|title|trapdump|tree|type|undelete|unpack|use|verify|ver|vol|xcopy)"
icolor brightblue "^rem.*$"
color cyan "[[:space:]]?::.*$"
icolor brightwhite ""[^"]*"" 
icolor yellow "[[:space:]]\%.*%[[:space:]]"





## Here is an example for Powershell scripts.

##
syntax "bat" "\.(ps1|psm1)$"
icolor brightred "([[:space:]]Add-Computer[[:space:]]|[[:space:]]Add-Content[[:space:]]|[[:space:]]Add-History[[:space:]]|[[:space:]]Add-Member[[:space:]]|[[:space:]]Add-PSSnapin[[:space:]]|[[:space:]]Add-Type[[:space:]]|[[:space:]]asnp[[:space:]]|[[:space:]]cat[[:space:]]|[[:space:]]cd[[:space:]]|[[:space:]]cd..[[:space:]]|[[:space:]]chdir[[:space:]]|[[:space:]]Checkpoint-Co[[:space:]]|[[:space:]]clc[[:space:]]|[[:space:]]clear[[:space:]]|[[:space:]]Clear-Content[[:space:]]|[[:space:]]Clear-EventLo[[:space:]]|[[:space:]]Clear-History[[:space:]]|[[:space:]]Clear-Host[[:space:]]|[[:space:]]Clear-Item[[:space:]]|[[:space:]]Clear-ItemPro[[:space:]]|[[:space:]]Clear-Variabl[[:space:]]|[[:space:]]clhy[[:space:]]|[[:space:]]cli[[:space:]]|[[:space:]]clp[[:space:]]|[[:space:]]cls[[:space:]]|[[:space:]]clv[[:space:]]|[[:space:]]compare[[:space:]]|[[:space:]]Compare-Objec[[:space:]]|[[:space:]]Complete-Tran[[:space:]]|[[:space:]]Connect-WSMan[[:space:]]|[[:space:]]ConvertFrom-C[[:space:]]|[[:space:]]ConvertFrom-S[[:space:]]|[[:space:]]ConvertFrom-S[[:space:]]|[[:space:]]Convert-Path[[:space:]]|[[:space:]]ConvertTo-Csv[[:space:]]|[[:space:]]ConvertTo-Htm[[:space:]]|[[:space:]]ConvertTo-Sec[[:space:]]|[[:space:]]ConvertTo-Xml[[:space:]]|[[:space:]]copy[[:space:]]|[[:space:]]Copy-Item[[:space:]]|[[:space:]]Copy-ItemProp[[:space:]]|[[:space:]]cp[[:space:]]|[[:space:]]cpi[[:space:]]|[[:space:]]cpp[[:space:]]|[[:space:]]cvpa[[:space:]]|[[:space:]]dbp[[:space:]]|[[:space:]]Debug-Process[[:space:]]|[[:space:]]del[[:space:]]|[[:space:]]diff[[:space:]]|[[:space:]]dir[[:space:]]|[[:space:]]Disable-Compu[[:space:]]|[[:space:]]Disable-PSBre[[:space:]]|[[:space:]]Disable-PSRem[[:space:]]|[[:space:]]Disable-PSSes[[:space:]]|[[:space:]]Disable-WSMan[[:space:]]|[[:space:]]Disconnect-WS[[:space:]]|[[:space:]]ebp[[:space:]]|[[:space:]]echo[[:space:]]|[[:space:]]Enable-Comput[[:space:]]|[[:space:]]Enable-PSBrea[[:space:]]|[[:space:]]Enable-PSRemo[[:space:]]|[[:space:]]Enable-PSSess[[:space:]]|[[:space:]]Enable-WSManC[[:space:]]|[[:space:]]Enter-PSSessi[[:space:]]|[[:space:]]epal[[:space:]]|[[:space:]]epcsv[[:space:]]|[[:space:]]epsn[[:space:]]|[[:space:]]erase[[:space:]]|[[:space:]]etsn[[:space:]]|[[:space:]]Exit-PSSessio[[:space:]]|[[:space:]]Export-Alias[[:space:]]|[[:space:]]Export-Clixml[[:space:]]|[[:space:]]Export-Consol[[:space:]]|[[:space:]]Export-Counte[[:space:]]|[[:space:]]Export-Csv[[:space:]]|[[:space:]]Export-Format[[:space:]]|[[:space:]]Export-Module[[:space:]]|[[:space:]]Export-PSSess[[:space:]]|[[:space:]]exsn[[:space:]]|[[:space:]]foreach[[:space:]]|[[:space:]]ForEach-Objec[[:space:]]|[[:space:]]Format-Custom[[:space:]]|[[:space:]]Format-List[[:space:]]|[[:space:]]Format-Table[[:space:]]|[[:space:]]Format-Wide[[:space:]]|[[:space:]]gal[[:space:]]|[[:space:]]gbp[[:space:]]|[[:space:]]gc[[:space:]]|[[:space:]]gci[[:space:]]|[[:space:]]gcm[[:space:]]|[[:space:]]gcs[[:space:]]|[[:space:]]gdr[[:space:]]|[[:space:]]Get-Acl[[:space:]]|[[:space:]]Get-Alias[[:space:]]|[[:space:]]Get-Authentic[[:space:]]|[[:space:]]Get-ChildItem[[:space:]]|[[:space:]]Get-Command[[:space:]]|[[:space:]]Get-ComputerR[[:space:]]|[[:space:]]Get-Content[[:space:]]|[[:space:]]Get-Counter[[:space:]]|[[:space:]]Get-Credentia[[:space:]]|[[:space:]]Get-Culture[[:space:]]|[[:space:]]Get-Date[[:space:]]|[[:space:]]Get-Event[[:space:]]|[[:space:]]Get-EventLog[[:space:]]|[[:space:]]Get-EventSubs[[:space:]]|[[:space:]]Get-Execution[[:space:]]|[[:space:]]Get-FormatDat[[:space:]]|[[:space:]]Get-Help[[:space:]]|[[:space:]]Get-History[[:space:]]|[[:space:]]Get-Host[[:space:]]|[[:space:]]Get-HotFix[[:space:]]|[[:space:]]Get-Item[[:space:]]|[[:space:]]Get-ItemPrope[[:space:]]|[[:space:]]Get-Job[[:space:]]|[[:space:]]Get-Location[[:space:]]|[[:space:]]Get-Member[[:space:]]|[[:space:]]Get-Module[[:space:]]|[[:space:]]Get-PfxCertif[[:space:]]|[[:space:]]Get-Process[[:space:]]|[[:space:]]Get-PSBreakpo[[:space:]]|[[:space:]]Get-PSCallSta[[:space:]]|[[:space:]]Get-PSDrive[[:space:]]|[[:space:]]Get-PSProvide[[:space:]]|[[:space:]]Get-PSSession[[:space:]]|[[:space:]]Get-PSSession[[:space:]]|[[:space:]]Get-PSSnapin[[:space:]]|[[:space:]]Get-Random[[:space:]]|[[:space:]]Get-Service[[:space:]]|[[:space:]]Get-TraceSour[[:space:]]|[[:space:]]Get-Transacti[[:space:]]|[[:space:]]Get-UICulture[[:space:]]|[[:space:]]Get-Unique[[:space:]]|[[:space:]]Get-Variable[[:space:]]|[[:space:]]Get-Verb[[:space:]]|[[:space:]]Get-WinEvent[[:space:]]|[[:space:]]Get-WmiObject[[:space:]]|[[:space:]]Get-WSManCred[[:space:]]|[[:space:]]Get-WSManInst[[:space:]]|[[:space:]]ghy[[:space:]]|[[:space:]]gjb[[:space:]]|[[:space:]]gmo[[:space:]]|[[:space:]]gps[[:space:]]|[[:space:]]group[[:space:]]|[[:space:]]Group-Object[[:space:]]|[[:space:]]gsn[[:space:]]|[[:space:]]gsnp[[:space:]]|[[:space:]]gsv[[:space:]]|[[:space:]]gwmi[[:space:]]|[[:space:]]help[[:space:]]|[[:space:]]history[[:space:]]|[[:space:]]icm[[:space:]]|[[:space:]]iex[[:space:]]|[[:space:]]ihy[[:space:]]|[[:space:]]Import-Alias[[:space:]]|[[:space:]]Import-Clixml[[:space:]]|[[:space:]]Import-Counte[[:space:]]|[[:space:]]Import-Csv[[:space:]]|[[:space:]]Import-Locali[[:space:]]|[[:space:]]Import-Module[[:space:]]|[[:space:]]Import-PSSess[[:space:]]|[[:space:]]ImportSystemM[[:space:]]|[[:space:]]Invoke-Comman[[:space:]]|[[:space:]]Invoke-Expres[[:space:]]|[[:space:]]Invoke-Histor[[:space:]]|[[:space:]]Invoke-Item[[:space:]]|[[:space:]]Invoke-WmiMet[[:space:]]|[[:space:]]Invoke-WSManA[[:space:]]|[[:space:]]ipal[[:space:]]|[[:space:]]ipcsv[[:space:]]|[[:space:]]ipmo[[:space:]]|[[:space:]]ipsn[[:space:]]|[[:space:]]ise[[:space:]]|[[:space:]]iwmi[[:space:]]|[[:space:]]Join-Path[[:space:]]|[[:space:]]kill[[:space:]]|[[:space:]]Limit-EventLo[[:space:]]|[[:space:]]man[[:space:]]|[[:space:]]measure[[:space:]]|[[:space:]]Measure-Comma[[:space:]]|[[:space:]]Measure-Objec[[:space:]]|[[:space:]]mkdir[[:space:]]|[[:space:]]more[[:space:]]|[[:space:]]mount[[:space:]]|[[:space:]]move[[:space:]]|[[:space:]]Move-Item[[:space:]]|[[:space:]]Move-ItemProp[[:space:]]|[[:space:]]mv[[:space:]]|[[:space:]]nal[[:space:]]|[[:space:]]ndr[[:space:]]|[[:space:]]New-Alias[[:space:]]|[[:space:]]New-Event[[:space:]]|[[:space:]]New-EventLog[[:space:]]|[[:space:]]New-Item[[:space:]]|[[:space:]]New-ItemPrope[[:space:]]|[[:space:]]New-Module[[:space:]]|[[:space:]]New-ModuleMan[[:space:]]|[[:space:]]New-Object[[:space:]]|[[:space:]]New-PSDrive[[:space:]]|[[:space:]]New-PSSession[[:space:]]|[[:space:]]New-PSSession[[:space:]]|[[:space:]]New-Service[[:space:]]|[[:space:]]New-TimeSpan[[:space:]]|[[:space:]]New-Variable[[:space:]]|[[:space:]]New-WebServic[[:space:]]|[[:space:]]New-WSManInst[[:space:]]|[[:space:]]New-WSManSess[[:space:]]|[[:space:]]nmo[[:space:]]|[[:space:]]nsn[[:space:]]|[[:space:]]ogv[[:space:]]|[[:space:]]Out-Default[[:space:]]|[[:space:]]Out-File[[:space:]]|[[:space:]]Out-GridView[[:space:]]|[[:space:]]Out-Host[[:space:]]|[[:space:]]Out-Null[[:space:]]|[[:space:]]Out-Printer[[:space:]]|[[:space:]]Out-String[[:space:]]|[[:space:]]popd[[:space:]]|[[:space:]]Pop-Location[[:space:]]|[[:space:]]prompt[[:space:]]|[[:space:]]pushd[[:space:]]|[[:space:]]Push-Location[[:space:]]|[[:space:]]pwd[[:space:]]|[[:space:]]rbp[[:space:]]|[[:space:]]rcjb[[:space:]]|[[:space:]]rdr[[:space:]]|[[:space:]]Read-Host[[:space:]]|[[:space:]]Receive-Job[[:space:]]|[[:space:]]Register-Engi[[:space:]]|[[:space:]]Register-Obje[[:space:]]|[[:space:]]Register-PSSe[[:space:]]|[[:space:]]Register-WmiE[[:space:]]|[[:space:]]Remove-Comput[[:space:]]|[[:space:]]Remove-Event[[:space:]]|[[:space:]]Remove-EventL[[:space:]]|[[:space:]]Remove-Item[[:space:]]|[[:space:]]Remove-ItemPr[[:space:]]|[[:space:]]Remove-Job[[:space:]]|[[:space:]]Remove-Module[[:space:]]|[[:space:]]Remove-PSBrea[[:space:]]|[[:space:]]Remove-PSDriv[[:space:]]|[[:space:]]Remove-PSSess[[:space:]]|[[:space:]]Remove-PSSnap[[:space:]]|[[:space:]]Remove-Variab[[:space:]]|[[:space:]]Remove-WmiObj[[:space:]]|[[:space:]]Remove-WSManI[[:space:]]|[[:space:]]ren[[:space:]]|[[:space:]]Rename-Item[[:space:]]|[[:space:]]Rename-ItemPr[[:space:]]|[[:space:]]Reset-Compute[[:space:]]|[[:space:]]Resolve-Path[[:space:]]|[[:space:]]Restart-Compu[[:space:]]|[[:space:]]Restart-Servi[[:space:]]|[[:space:]]Restore-Compu[[:space:]]|[[:space:]]Resume-Servic[[:space:]]|[[:space:]]rjb[[:space:]]|[[:space:]]rmdir[[:space:]]|[[:space:]]rmo[[:space:]]|[[:space:]]rni[[:space:]]|[[:space:]]rnp[[:space:]]|[[:space:]]rsn[[:space:]]|[[:space:]]rsnp[[:space:]]|[[:space:]]rvpa[[:space:]]|[[:space:]]rwmi[[:space:]]|[[:space:]]sajb[[:space:]]|[[:space:]]sal[[:space:]]|[[:space:]]saps[[:space:]]|[[:space:]]sasv[[:space:]]|[[:space:]]sbp[[:space:]]|[[:space:]]select[[:space:]]|[[:space:]]Select-Object[[:space:]]|[[:space:]]Select-String[[:space:]]|[[:space:]]Select-Xml[[:space:]]|[[:space:]]Send-MailMess[[:space:]]|[[:space:]]set[[:space:]]|[[:space:]]Set-Acl[[:space:]]|[[:space:]]Set-Alias[[:space:]]|[[:space:]]Set-Authentic[[:space:]]|[[:space:]]Set-Content[[:space:]]|[[:space:]]Set-Date[[:space:]]|[[:space:]]Set-Execution[[:space:]]|[[:space:]]Set-Item[[:space:]]|[[:space:]]Set-ItemPrope[[:space:]]|[[:space:]]Set-Location[[:space:]]|[[:space:]]Set-PSBreakpo[[:space:]]|[[:space:]]Set-PSDebug[[:space:]]|[[:space:]]Set-PSSession[[:space:]]|[[:space:]]Set-Service[[:space:]]|[[:space:]]Set-StrictMod[[:space:]]|[[:space:]]Set-TraceSour[[:space:]]|[[:space:]]Set-Variable[[:space:]]|[[:space:]]Set-WmiInstan[[:space:]]|[[:space:]]Set-WSManInst[[:space:]]|[[:space:]]Set-WSManQuic[[:space:]]|[[:space:]]Show-EventLog[[:space:]]|[[:space:]]sleep[[:space:]]|[[:space:]]sort[[:space:]]|[[:space:]]Sort-Object[[:space:]]|[[:space:]]spjb[[:space:]]|[[:space:]]Split-Path[[:space:]]|[[:space:]]spps[[:space:]]|[[:space:]]spsv[[:space:]]|[[:space:]]start[[:space:]]|[[:space:]]Start-Job[[:space:]]|[[:space:]]Start-Process[[:space:]]|[[:space:]]Start-Service[[:space:]]|[[:space:]]Start-Sleep[[:space:]]|[[:space:]]Start-Transac[[:space:]]|[[:space:]]Start-Transcr[[:space:]]|[[:space:]]Stop-Computer[[:space:]]|[[:space:]]Stop-Job[[:space:]]|[[:space:]]Stop-Process[[:space:]]|[[:space:]]Stop-Service[[:space:]]|[[:space:]]Stop-Transcri[[:space:]]|[[:space:]]Suspend-Servi[[:space:]]|[[:space:]]swmi[[:space:]]|[[:space:]]TabExpansion[[:space:]]|[[:space:]]tee[[:space:]]|[[:space:]]Tee-Object[[:space:]]|[[:space:]]Test-Computer[[:space:]]|[[:space:]]Test-Connecti[[:space:]]|[[:space:]]Test-ModuleMa[[:space:]]|[[:space:]]Test-Path[[:space:]]|[[:space:]]Test-WSMan[[:space:]]|[[:space:]]Trace-Command[[:space:]]|[[:space:]]type[[:space:]]|[[:space:]]Undo-Transact[[:space:]]|[[:space:]]Unregister-Ev[[:space:]]|[[:space:]]Unregister-PS[[:space:]]|[[:space:]]Update-Format[[:space:]]|[[:space:]]Update-List[[:space:]]|[[:space:]]Update-TypeDa[[:space:]]|[[:space:]]Use-Transacti[[:space:]]|[[:space:]]Wait-Event[[:space:]]|[[:space:]]Wait-Job[[:space:]]|[[:space:]]Wait-Process[[:space:]]|[[:space:]]where[[:space:]]|[[:space:]]Where-Object[[:space:]]|[[:space:]]wjb[[:space:]]|[[:space:]]write[[:space:]]|[[:space:]]Write-Debug[[:space:]]|[[:space:]]Write-Error[[:space:]]|[[:space:]]Write-EventLo[[:space:]]|[[:space:]]Write-Host[[:space:]]|[[:space:]]Write-Output[[:space:]]|[[:space:]]Write-Progres[[:space:]]|[[:space:]]Write-Verbose[[:space:]]|[[:space:]]Write-Warning[[:space:]])"
icolor brightyellow "function[[:space:]]"
icolor brightyellow "-(eq[[:space:]]|ne[[:space:]]|gt[[:space:]]|lt[[:space:]]|ge[[:space:]]|le[[:space:]])"
icolor brightred "\$\{?[0-9A-Z_!@#$*?-]+\}?"
color brightblue "^[[:space:]]?#.*$"
icolor brightwhite ""[^"]*""
icolor yellow "[[:space:]]\%.*%[[:space:]]"
icolor brightcyan "([[:space:]]?if[[:space:]]|[[:space:]]?do[[:space:]]|[[:space:]]?else[[:space:]]|[[:space:]]?elseif[[:space:]]|[[:space:]]?for[[:space:]]|[[:space:]]?foreach[[:space:]]|[[:space:]]?switch[[:space:]]|[[:space:]]?until[[:space:]]|[[:space:]]?while[[:space:]])"



Clearly the first one is for batch files and the second for Powershell. The hardest part of this was getting better at regex! Once I got around that hurdle, it was a bit easier. Also, sorry for including so many commandlets in the powershell one but.... I wanted to have decent highlighting for keywords so... I made that work. The key to making them work was having them highlighted only if they are stand-alone keywords... I know they won't currently highlight if they are the start of a line, but it was better to do it this way. They are super long and I think it should be noted that in between each quote and parenthasis should be a single line when you paste this into a text editor for saving. 

To save, you simply copy the text I have here into a file, then save that file in /usr/share/nano/batch.nanorc and also /usr/share/nano/powershell.nanorc.

Then to get them included into your nano, you need to edit /etc/nanorc and navigate to the bottom where it says "## Nanorc files".... then include /usr/share/nano/nanorc.nanorc.... it's saying Read this .nanorc file when you run for syntax highlighting... Well we want to add our two files into that list. Once that's done. Close it up. Fire up nano with a batch file or a powershell file and test away! You can tweak these colors and settings to make them match what you want but, hopefully I've done most of the hard work for you.

Overall, I hope this was helpful to anyone that may need them and if anyone has any questions regarding them, please feel free to contact me! You can either comment on here or email me and I'll try my best to answer!


-Mitch

EDIT: There is a much cleaner version at https://github.com/mitchell486/nanorc

No comments:

Post a Comment