none
Powershell ISE – change indent/tab size + keep tabs

    Question

  • Anyway to change indent/tab size in Powershell ISE?
    Something simple like in Visual studio IDE:
    Tab size 2
    Indent size 2
    Keep tabs

    Wednesday, March 07, 2012 5:18 PM

Answers

All replies

  • No.  No way. (Not in V3 Beta either)

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)


    • Edited by Bigteddy Wednesday, March 07, 2012 6:01 PM V3 comment
    Wednesday, March 07, 2012 5:32 PM
  • The PowerShell ISE doesn't let you configure the tab size or to insert tabs instead of spaces.

    Bill

    Wednesday, March 07, 2012 5:37 PM
    Moderator
  • Sorry - not in this version.  Tabs are fixed a four spaces and not saved but are converted to spaces.


    ¯\_(ツ)_/¯

    Wednesday, March 07, 2012 5:45 PM
  • Make a suggestion on Connect:

    https://connect.microsoft.com/PowerShell

    I'd vote for it :)

    I have a few suggestions out there for the ISE as well....

    Karl


    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})

    Wednesday, March 07, 2012 5:58 PM
  • I have created a suggestion on Connect for this: https://connect.microsoft.com/PowerShell/feedback/details/761424/ise-configurable-tab-size-convert-tabs-to-spaces

    Vote for it to get this implemented!


    Author of SetACL | web: http://helgeklein.com | Twitter: @HelgeKlein


    Thursday, September 06, 2012 12:40 PM
  • Thank you, and also thanks for writing SetACL.

    Bill

    Thursday, September 06, 2012 2:21 PM
    Moderator
  • FWIW

    In $psise.CurrentFile.Editor there is a private property called EditorOperations. EditorOperations.SupportedOptions has two options "Tabs/TabSize" and "Tabs/IndentSize". 

    void EditorOperations.options.SetOptionValue(string optionId, object value) is a method that apparently sets editor options. Unfortunately invoking SetOptionValue throws an exception "The calling thread cannot access this object because a different thread owns it.".


    • Edited by NoSimDash Sunday, May 26, 2013 8:40 PM correct mistake
    Sunday, May 26, 2013 8:38 PM
  • I was able to set IndentSize to 2. I used a psIse Dispatcher to get around the exception mentioned in the previous post. The dispatcher is passed a delegate which gets invoked on the psIse thread that owns the editor options. Here is C# class that does part of the work:

    $src=@"
    // Borrowed this from MSDN sample under PushFrame
    public MethodInfo methInfo;
    public Object opt;
    public Dispatcher disp;
    
    public void DoEvents(){
        DispatcherFrame frame = new DispatcherFrame();
        disp.BeginInvoke(DispatcherPriority.Normal,
            new DispatcherOperationCallback(ExitFrames),frame);
        Dispatcher.PushFrame(frame);
    }
    
    public object ExitFrames(object f){
        DispatcherFrame df = ((DispatcherFrame)f);
        Object[] args = {"Tabs/TabSize",2};
        methInfo.Invoke( opt, args);
        args[0] = "Tabs/IndentSize";
        methInfo.Invoke( opt, args);
        df.Continue = false;
        return null;
    }
    "@
    
    $ns="a"
    add-type $src -name eventually -NameSpace $ns -UsingNamespace System.Windows.Forms,System.Windows.Threading,System.Reflection, Microsoft.VisualStudio.Text.EditorOptions.Implementation -ReferencedAssemblies windowsbase,system.windows.forms,Microsoft.PowerShell.Editor

    And then the powershell code to run it:

    $ed = $psise.CurrentFile.Editor
    #selp is like select-object but it selects private properties
    $opt  = $ed|selp EditorOperations|select -ExpandProperty Options
    $disp = $ed|selp EditorViewHost|select -ExpandProperty Dispatcher
    $invokeMethInfo = $opt.GetType().getmethod("SetOptionValue",    [type[]]@([string],[object]))
    
    $ev=new "$ns.eventually"
    $ev.disp = $disp
    $ev.methInfo = $invokeMethInfo 
    $ev.opt = $opt
    $ev.doevents()
    As you can probably guess this only affects the CurrentFile. However $opt has a Parent property. I think if you substitute $opt.Parent or $opt.Parent.Parent or similar you might affect all open files and any file that gets opened afterwards
    • Proposed as answer by NoSimDash Tuesday, July 02, 2013 12:09 AM
    Monday, July 01, 2013 4:23 PM