Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Running the current function definition

    Hi!

    Is there a way to run a function spread over multiple lines with a keyboard shortcut/combination?

    In other words I would like the do-file editor to do what Rstudio does with Ctrl+Alt+F ("Run the current function definition", https://support.rstudio.com/hc/en-us...oard-Shortcuts)


    Code:
    * An example. It would be great to have a keyboard shortcut/combination that would work regardless where the text cursor is (within the function)
    
    graph bar s7 s8 s10, over(var1, sort(ascend)) ///
     title("Title") ///
     note("Note", pos(b)) ///
     blabel(bar, pos(center) format(%9.0f))  ///
     stack ///
     bar(3, color(219 219 141)) ///
     legend(off)

    Thank you in advance!

    P.S. I have already created a simple autohotkey script to run one line of code, but haven't found a way to run a function that is spread over multiple lines:
    https://stackoverflow.com/questions/...75501#43675501

  • #2
    That's not a function Stata sense; it is a command. Not trying to be more pedantic (than usual) but the right terminology is important.

    Comment


    • #3
      I found a workaround that does the trick for me. There is a keyboard shortcut (Ctrl+B) for balancing braces in the Stata do-file editor. I figured that one can use braces even if they weren't necessary needed. All one has to do is to add extra braces above and below a command and then run an Auto-hotkey script assigned to a desired keyboard combination.

      When I press Ctrl+0 the following code is executed as long as the text cursor is between the braces.

      Code:
      {
      graph bar s7 s8 s10, over(var1, sort(ascend)) ///
       title("Title") ///
       note("Note", pos(b)) ///
       blabel(bar, pos(center) format(%9.0f))  ///
       stack ///
       bar(3, color(219 219 141)) ///
       legend(off)
      }
      In order to accomplish this, go to https://autohotkey.com/ and download the software.

      Write the following script and run it.(Add a shortcut of the script in your startup folder so that it will be running the next time you start your computer)

      Code:
      ^0::Send {Control down}bb{Control up}{Up}{Control down}bb{Control up}{Up}{Control down}bb{Control up}{Up}{Control down}bb{Control up}{Up}{Control down}bb{Control up}{Up}{Control down}b{Control up}{pause}^d{down}{Home}
      
      
      Return
      This script balances the braces 6 times so it will work even if you have up to 5 braces in your command. You can always add or remove the "{Control down}bb{Control up}{Up}"-combinations in order to allow for more braces or make the code faster. If the code does not work, you can always add more {pause}s in between, but this syntax works robustly at least on my computer.

      If you combine this with my other autohotkey script you will be able to execute commands that are on one line with a one keyboard shortcut and commands on multiple lines with another.

      In case someone can improve the solution or has a better way to solve this, I am all ears! Having said that, it would be great if running a command with a simple keyboard shortcut would be an inbuild do-file functionality in the future.

      Comment

      Working...
      X