1

I am still quite new to Powershell, but I would like to add my favourite editor into an Alias in Powershell.

I edited the profile.ps1 in C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 which will run automatically when PowerShells starts.

I tried enter New-Alias np notepad.exe which works perfectly everytime I launch PowerShell.


However, I would like to use Sublime Text 3 as my editor. I followed the instructions in this SO page: How can I write a PowerShell alias with arguments in the middle?

The command line I need for Sublime Text is "C:\Program Files\Sublime Text 3\sublime_text.exe" -n [FirstArg]

Which I come out something like this: function sublime { 'C:\Program Files\Sublime Text 3\sublime_text.exe' -n $args }

It does not work and I got the error like this:

At C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1:5 char:72
+ ... lime {  'C:\Program Files\Sublime Text 3\sublime_text.exe' -n $args }
+                                                                ~~
Unexpected token '-n' in expression or statement.
At C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1:5 char:75
+ ... lime {  'C:\Program Files\Sublime Text 3\sublime_text.exe' -n $args }
+                                                                   ~~~~~
Unexpected token '$args' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

Any helps would be appreciated. Thanks!

Jamie Phan
  • 796
  • 1
  • 7
  • 26
  • 2
    You miss invocation operator `&`. – user4003407 Jul 22 '17 at 03:58
  • @PetSerAl As I'm still quite new to PS, would you please give me an example please? Many thanks. – Jamie Phan Jul 22 '17 at 04:03
  • Do you ever launch any program with space in their name? If yes, then do the same. If no, then find how to do that. It should be plenty information about this in the Internet. – user4003407 Jul 22 '17 at 04:15
  • @PetSerAl Yes. In Linux, when I want to set an alias with space in path, I could use quotes in it, for example: `export sublime="/path/to/Sublime Text 3"`. Thats why I am confused by `&`. But thanks anyways. – Jamie Phan Jul 22 '17 at 04:20
  • 3
    `'...'` is just a string of characters it doesn't run anything. You need the & to run the thing in the string e.g. `& 'C:\Program Files\Sublime Text 3\sublime_text.exe' -n $args` – TessellatingHeckler Jul 22 '17 at 04:24
  • Try this: https://stackoverflow.com/a/61374953/1896134 – JayRizzo Apr 22 '20 at 22:23

1 Answers1

0

Without being a Sublime user, I suspect this should work:

function Start-Sublime {
    param([string]$args)

    $limeArgs = [string]::Empty
    if ($args -ne $null) {
        $limeArgs = $args
    }

    Start-Process "$env:ProgramFiles\Sublime Text 3\sublime_text.exe" -n $limeArgs
}

Set-Alias lime Start-Sublime

Not the prettiest PowerShell code, but I imagine it will do what you're after. It's a little easier to understand than the cryptic & operator is.

PSGuy
  • 653
  • 6
  • 17