Git Product home page Git Product logo

macros's Introduction

Macros

Brings simple, powerful custom macros support to VS Code. Made with <3 by geddski

See also Level up your Coding with Macros

Create Custom Macros

Create your own custom macros by adding them to your settings.json (Code|File > Preferences > User Settings)

For example:

"macros": {
    "commentDown": [
        "editor.action.copyLinesDownAction",
        "cursorUp",
        "editor.action.addCommentLine",
        "cursorDown"
    ]
}

This macro creates a copy of the current line, comments out the original line, and moves the cursor down to the copy.

Your macros can run any built-in VS Code action, and even actions from other extensions. To see all the names of possible actions VS Code can run, see Default Keyboard Shortcuts (Code|File > Preferences > Keyboard Shortcuts)

Give your macros names that briefly describe what they do.

Add Keybindings to Run your Macros

in keybindings.json (Code|File > Preferences > Keyboard Shortcuts) add bindings to your macros:

{
  "key": "ctrl+cmd+/",
  "command": "macros.commentDown"
}

Notice that macros.my_macro_name has to match what you named your macro.

Passing Arguments to Commands

Many commands accept arguments, like the "type" command which lets you insert text into the editor. For these cases use an object instead of a string when specifying the command to call in your settings.json:

"macros": {
  "addSemicolon": [
    "cursorEnd",
      {"command": "type", "args": {"text": ";"}}
  ]
}

Executing Snippets as part of a Macro

Macros can also execute any of your snippets which is super neat. Just insert the same text that you would normally type for the snippet, followed by the insertSnippet command:

"macros": {
  "doMySnippet": [
    {"command": "type", "args": {"text": "mySnippetPrefixHere"}},
    "insertSnippet"
  ]
}

License

MIT

Known Issues

Doesn't currently add macros to command pallete (have to use keybindings).

Release Notes

1.0.0

Initial release of Macros

macros's People

Contributors

al-th avatar geddski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

macros's Issues

Type text where vscode has focus not just in the editor window

I am trying to create a macro that will execute a find and replace.

I can use editor.action.startFindReplaceAction but that just opens the input. I tried putting a command text after that to input the text I wanted to find but that typed the text in the main editor and not in the find dialog box. Is there anyway to pass arguments to find and replace or to have the type command add text to the input box instead of the editor?

Warn: command 'macros.commentDown' not found

Giving this extension a whirl, but I am unable to get it working even with the basic example supplied. I have added to the User settings (and you can see it also has the same error reported )

image

image

Possible to auto-run a snippet?

Thanks for this great extension.
I have a 2 part macro:

  1. Copy selected text
  2. Run a snippet to wrap JS block quotes around the selection.
    Useful, no?

When I run the macro, it asks to confirm the snippet, Is there a way to just run the whole macro with no confirm?
Thanks.

How to format and save at the same time?

"macros": { "formatAndSave": [ "editor.action.formatDocument", "workbench.action.files.save" ] }

{ "key": "ctrl+s", "command": "macros.formatAndSave" }

These commands (Format and Save) do not work at the same time. I need that.

When I press the first time,then the first command (Format)is triggered Only.
Secontd time - second command (Save) is triggered.

List of editor.actions

I am not sure whether the editor.actions that you list in your examples is part of your extension or part of visual studio code. I assume it is part of visual studio code.

I have searched and cannot find a list of all editor.actions for VSCode. Can someone point me to this documentation specifically? I have looked in the VSCode documentation and can't find it there either.

After last VS Code update, something doesn't works fine

I'm using this extension from 8 months, but I think from last VS code version 1.37.1 my macro doesn't work anymore.
${CLIPBOARD} is not replaced anymore from selected text. In some case all snippet is pasted replacing all document text instead insert it after the newline.

Anyone can help me?

settings.json

`

"macros": { 
    "printr": [
        "editor.action.smartSelect.grow",
        "editor.action.clipboardCopyAction",
        "editor.action.insertLineAfter",
        {
         "command": "editor.action.insertSnippet",
            "args": {
                "name": "customprintr",
                "langId": "php",
            }
       },
       "editor.action.insertLineAfter",
       "editor.action.clipboardCopyAction",
    ]
}, 

`

snippets/php.json

`

 "customprintr": { 
	"prefix": "customprintr",
 	"body": [
		"\t",
		"\t    echo '<pre style=\"text-align:left\">';",
		"\t    \\$printMe = ${CLIPBOARD}; print_r(\\$printMe);",
		"\t    echo '</pre>';",
		"\t    die;",
		"\t",
		"\t"
 	],
 	"description": "Insert custom print_r"
},
"br": {
	"prefix": "br",
	"body": ["<br />"]
}

`

Not all commands are executed.

Not sure what's going on. Trying this:

        "save": [
            "editor.action.formatDocument",
            "workbench.action.files.saveAll"
        ],
        "acceptAndFormat": [
            "^acceptSelectedSuggestion",
            "editor.action.formatDocument"
        ]

In the "save" macro the currently edited document doesn't seem to be saved but other documents are saved.

 {
    {
        "key": "ctrl+s",
        "command": "macros.save",
        "when": "editorHasDocumentFormattingProvider && editorTextFocus && !editorReadonly"
    },

In the next example, only one line of macro works in each case and it's a different line for each:

 {
        "key": "+",
        "command": "macros.acceptAndFormat",
        "when": "editorTextFocus && suggestWidgetVisible && suggestionSupportsAcceptOnKey && editorHasDocumentFormattingProvider && editorLangId == 'csharp'"
    },
    {
        "key": "-",
        "command": "macros.acceptAndFormat",
        "when": "editorTextFocus && suggestWidgetVisible && suggestionSupportsAcceptOnKey && editorHasDocumentFormattingProvider && editorLangId == 'csharp'"
    },

how to modify comment down to comment blocks of code

so comment down works to copy a block of code but it only comments the last line, I was wondering if there is a good way to comment the block of code after.

also are there any docs showing what commands are supported? I don't have a lot of macro experience and I'm having trouble finding super useful resources for this type of problem.

Cannot change language mode

As mentioned in the comments of issue #14 by @martin-tarjanyi:

I would like to have a macro to change the language mode to Json and format the document according to that. However, I'm not able to provide an input for this command automatically.

"macros": {
    "formatJson": [
        {"command": "workbench.action.editor.changeLanguageMode", "args": {"id": "json"}},
        "editor.action.formatDocument"
    ]
}

Second command of macro no longer executed after recent VS Code update

Env

Mac OS 10.13.6 (17G3025)
VS Code Version 1.30.1 (1.30.1)
Macros v1.2.1

Diagnosis and steps to reproduce

I use the macros plugin to replicate the cmd+e behavior of IntelliJ for Mac. This command opens the "Recent files" dialogue and shifts the cursor to the previously open file (i.e. the second item in the list of recently opened files, where the currently open file is first on the list).

To do this in VS Code, I created this macro:

    // User Setings
    ...
    "macros": {
        "intellijRecent": [
            "workbench.action.quickOpen",
            "workbench.action.quickOpenNavigateNext",
        ]
    },
    ...

And bound it to cmd+e in my keybindings:

    // keybindings.json
    ...
    {
        "key": "cmd+e",
        "command": "macros.intellijRecent",
        "when": "!inQuickOpen"
    },
    ...

Up until today, this was working as expected: pressing cmd+e would open the recently open files dialogue box and move the cursor to the second item in the list. Today, it is opening the recently open files dialogue box, but not moving the cursor to the second item in the list.

I tried these changes to see if they would return the expected behavior, but they did not:

    "macros": {
        "intellijRecent": [
            "workbench.action.quickOpen",
            "workbench.action.quickOpenNavigateNext",
            "workbench.action.quickOpenNavigateNext"
        ]
    },
    "macros": {
        "intellijRecent": [
            "workbench.action.quickOpen",
            "workbench.action.quickOpenNavigateNextInFilePicker"
        ]
    },
    "macros": {
        "intellijRecent": [
            "workbench.action.quickOpen",
            "workbench.action.quickOpenNavigateNextInRecentFilesPicker"
        ]
    },

It seems to me that the second command is not being executed in the new update for some reason.

Any advice about trying to determine the cause of the issue would be greatly appreciated. Thank you!

Feature Request: Control Structures

Is it possible to implement simple control structures to repeat a macro or a block inside a macro (or even a macro calling another macro) n-times or until end-of-line or end-of-file, for example?

workbench.action.focusActiveEditorGroup has no effect in certain macros

This issue is also described in https://stackoverflow.com/questions/49457051

Basically, while "workbench.action.focusActiveEditorGroup" can be issued in a key binding, it seems to have no effect when used in a macro immediately after "editor.debug.action.selectionToRepl". What does work is to use the same key binding twice: in the second run, "editor.debug.action.selectionToRepl" does not do anything (there is no active editor and thus no selection) and the second command just works. However, this is only possible for key bindings which are not captured by the debug console itself, so not really a good workaround.

    "macros": {
        "selectionToReplAndReturnToEditor": [
            "editor.debug.action.selectionToRepl",
            "workbench.action.focusActiveEditorGroup",
        ]
    }

Simulate Enter keypress

General usability question.
I am attempting the following, but I can't seem to get the macro to execute the "enter" key. There's no key bindings that describe the "enter" action in keybindings.json (such as CR + LF) nor can I find it in vscode docs. Any ideas on how I might achieve it?

"macros": { "temp": [ "enter", "cursorEnd", "deleteWordRight", "cursorEnd", "cursorLeft" ]
The macro is to be used to move commas from the end of a line to the start of the next. Cursor starts before the comma to be moved and (generally) will end before the next one.

User Settings in windows 10

in windows path to user settings looks as: (File > Preferences > User Settings), maybe it makes sense to add it to readme.

Weird behaviour when using expandLineSelection and clipboardCutAction

I wanted to create a macro for cutting the line containg the cursor or all lines that are atleast partially contained in the current selection.
To do this, I created this macro:
"cutLines": [ "expandLineSelection", "editor.action.clipboardCutAction" ]

When executing the macro, the current line(s) get removed as expected.
However if there was a selection before running the macro, the clipboard will only contain the selected parts instead of containing the expanded selection.
The same behaviour occurs when using clipboardCopyAction instead of the cut action.

How to trigger keyboard 'enter'

A wonderful artifact, but how can I trigger enter to turn to next line by using macros? I cant find the command to turn to next line.

execute external command

Hi,
i would like to execute WinMerge with the path of the currently opened path, i do not know the command to execute external commands.

commands order not always working well

I have mixed results with a macro I am using.
I am using both the current find widget, and the search files panel.
activating the next match, and then the find widget.
But sometimes the find widget executes before the search match.
At least, that's what I deem is happening...
Are the commands happening one after another? Can it be an async problem?
Is there a way that I can add a little delay (sleep?), or wait for focus?
If there is a better way to contact and debug this together, please let me know.

Allow Undo of complete macro (not just last action)

The extension is GREAT. But when using Undo (Ctrl+Z), I'd like to be able to undo all the actions of the macro; not just the last one.

For example, in this macro, only "editor.action.removeCommentLine" is undone.

 "commentDown": [
            "editor.action.addCommentLine",
            "editor.action.copyLinesDownAction",
            "editor.action.removeCommentLine"
        ]

To Undo a macro, I have to type "Ctrl+Z" as many times as editor actions it has.

Thanks

command 'macros.commentDown' not found

I'm using vscode 1.19.0 on Mac OS X. I've added the "macros" key and "commentDown" example macro to my user settings.json file:

image

I've also added example shortcut to my keybindings.json

image

But when I press cmd+ctrl+/ vscode complains with command 'macros.commentDown' not found:

screenshot 2017-12-20 at 11 14 01 pm

Have I configured something incorrectly? I'm expecting it to comment the line, not show the above error.

Passing a string to actions.find

I'm trying to create a macro that will find a certain string (a regex string that I use a lot), that will then replace it with another regex string. I have set it up, but instead of the find string being placed in the find box, it's being placed in the text editor. Is there another argument I should be using instead of text?

	"macros": {
		"addSpacesAfterCommas" : [
			"actions.find",
				{"command": "type", "args": {"text": "search string here"}}
		]
	}

Overview example of inserting a snippet

I tried to get your example of inserting a snippet within a macro to work without success. From VSCode extensions marketplace : macros. Your example:

And I have a macro that will select the word at my cursor, copy it, drop to a new line, and run that snippet to log the variable:

editor.action.addSelectionToNextFindMatch

`"macros": {
    "logCurrentVariable": [
    "editor.action.addSelectionToNextFindMatch",
    "problems.action.copy",

      {"command": "type", "args": {"text": "con"}}
  ]
}`

At the very least that must be missing an "insertSnippet" command? And there is no copy or paste? And the weird mention of "editor.action.addSelectionToNextFindMatch" seems out of place.

Might I suggest:

 "logCurrentVariable": [
      "editor.action.smartSelect.grow",
      "editor.action.clipboardCopyAction",
      "editor.action.insertLineAfter",
      {
        "command": "type",
        "args": {
          "text": "log"
        }
     },
    "insertSnippet",
    "editor.action.clipboardPasteAction"
  ]

which uses the following snippet (in VSCode) ๐Ÿ‘

"Print to console": {
    "prefix": "log",
     "body": [
         "console.log('${TM_FILENAME}, line ${TM_LINE_NUMBER}   :  $1 = ' + $1);"
    ],
   "description": "Log output to console"
}

keybinding fail

keybinding problem with "Macros.commentDown"
The error is "Value is not accepted. Valid values: ..."
Currently no other problems showing in all of vsCode.

My custom macros were failing this way so I removed all that and put only examples from the info page.

keybindings.json
	{ "key": "alt+d", "command": "Macros.commentDown"	},
	
settings.json
"macros": {
	"commentDown": [
		"editor.action.copyLinesDownAction",
		"cursorUp",
		"editor.action.addCommentLine",
		"cursorDown"
	]
},

Macro to find next/previous string

Am trying to define macros that will that move the cursor to the next/previous space. My first go at this:

settings.json

    "macros": {
        "goNextSpace": [
            "actions.find",
            {"command": "type", "args": {"text": " "}}
        ]
    }

and keybindings.json

    {
        "key": "alt+shift+]",
        "command": "macros.goNextSpace"
    }

Naturally enough, this is opening the find text input, which I don't want.. it is also inserting a space.. Any clues how I can make a macro that will just go to the next space without requiring the text input - and how to make the corresponding previous version?

Also, when I hover the mouse over macros.goNextSpace in keybindings.json, I get a pop-up that says Value is not accepted. Valid values: ....

Rob
:)

Copy a line and Paste it

I want to copy a line in a source code file, drop down 10 lines, highlight the line I end up at and paste the copied line over the top.

I am having trouble with the highlight a line and copy it and then the second highlight a line and paste over it.

Is there a forum for this extension that I can post this kind of question?

Replacing text

Hello

I love your macros. Something I did in Notepad++ was convert a path from \ to / by using the selected text. I tried to call replaceInFilesWithSelectedText with arguments, but it just brings the replace panel, doing nothing.
Is there a way to achieve this with macros as of now ?

Record Macro

do you have to script your macro or can you record it?

blockCommentDown

Hey Mate,
Love the extension.
Thought this might be helpful.

To 'commentDown' a block instead of just one line:

"macros": {
"commentDown": [
    "macros.expandSelection",
    "editor.action.clipboardCopyAction",
    "editor.action.commentLine",
    "editor.action.insertLineAfter",
    "editor.action.insertLineAfter",
    "editor.action.clipboardPasteAction",
    ]
}

Cheers,
dunks

Type character

Hello,
Could you please help me? How does this extension can type some string in editor? I would like to put text block in to editor when shortcut is pressed.

autocomplete problem with type command

So my problem is that when inserting some text using type command, autocomplete seems active and destroy my flow.
For exemple: the output of this macros is "something()" i don't want the closing parentheses.

"macros" : {
    "doStuff": [{
        "command": "type", "args": { "text" : "something("}
     }]
}

Trouble getting a save&close macro to work

I wrote the following where my intent is to bind a key to save a document then close it:

"macros": {
    "saveAndCloseActiveEditor":  [
      "workbench.action.files.save",
      "workbench.action.closeActiveEditor"
    ]
  }

However, it doesn't work as I'd hoped as I still get prompted to save the document:

[Window Title]
Visual Studio Code - Insiders

[Main Instruction]
Do you want to save the changes you made to settings.json?

[Content]
Your changes will be lost if you don't save them.

[Save] [Don't Save] [Cancel]

Strangely, it's as though the closeActiveEditor didn't wait for save to complete. If I cancel that dialog, the document is actually saved from the prior macro run, and if I run it again, it will therefore be able to close without the prompt.

Any idea what's going on here and how I might get around it? Thanks!

Macro into terminal

I'm able to make a macro that switches focus to the embedded terminal, but if the macro tries to type a command in the terminal, it gets pasted in the former code window. Here's what I have:

         "workbench.action.terminal.focus",
         {"command": "type", "args": {"text": "oh hai"}},
      ]

What am I missing? Thanks!

Unknown configuration setting

When I put the following in my settings.json file (user settings, not workspace), I get the following:

Unknown configuration setting

If I put the command in my workspace settings file, I don't get that error. However, the macros don't work unless they are placed in the user settings file but then I have that warning in my Problems panel.

Order of Operations Seems to Be off with Cursors and Type

Maybe I'm not understanding this correctly, but it isn't working as expected.

Here is an example:

"example": [
    "cursorDown",
    {"command": "type", "args": {"text": "down"}},
    "cursorUp"
    {"command": "type", "args": {"text": "up"}}
],

What I end up with when the cursor (represented with the pipe (|) below) starts are line 1:

1 downup|
2
3

I'm expecting it to do:

1 up|
2 down
3

Inserting wait/timeout between commands

Is there currently any way to introduce a (preferably adjustable) timeout in between macro commands?
If not, I think it would make a super-useful addition to this brilliant extension!

My use-case:
I am using the jest extension for vscode that allows for automatic running tests within VSCode's extension pane. However, sometimes (not blaming the plugin author) it doesn't seem to work. The only workaround is to stop and start the runner. To make this simpler, I'm using this macros extension to do the restarting automatically. However, sometimes the restartJest macro fails because the jest.stop hasn't completed execution properly before the next command jest.start is called. Inserting a short timeout (of say, 200ms) would be good enough to make the macro work smoothly.

Thoughts?

how to make a command happen after command that takes input?

I would like to run workbench.action.git.push after workbench.action.git.input-commit is completed, but git push is called immediately after the commit input appears and before I fill it out and submit it.

Anyone have any ideas to solve this?

perhaps even just a way to delay the push command by 15 seconds.

not working w/ basic sequence

I have the following macro defined:

  "macros": {
    "changeCase.apply_TitleCase":[
      "editor.action.transformToLowercase",
      "extension.changeCase.upperFirst",
    ]
  },

Only the transformToLowercase action is executed.

Output text as part of macro

Is it possible to output a string to the editor as part of a macro? I don't want to ask for input from the user, just output a string that is defined in the macro.

For example, to go to end of line and add a semi-colon, I've got this but it doesn't work:

"macros": {
    "eolSemiColon": [
        "cursorEnd",
        ";"
        ]
    }
}

Issue with editor commands

keybindings

    {
        "key": "ctrl+k",
        "command": "macros.moveUpFromTerminalAndHidePanel",
        "when": "terminalFocus"
    },

user settings

    "macros": {
        "moveUpFromTerminalAndHidePanel": [
            "editor.action.focusFirstEditorGroup",
            "editor.action.togglePanel"
        ]
    }

From the terminal panel, I press ctrl+k and nothing happens. Am I doing something wrong? I tried creating a simple macro which moves the cursor in the editor a couple of times and it worked fine.

Feature request: macro recorder

One thing I really like about most macro-enabled editors is the ability to "record" my action into a temporary macros, and then play it back multiple times.

This would be a great feature for you macros extension.

Command to type into Terminal

This would be an incredibly useful utility if it could input text into the integrated terminal. The command, type, args notation doesn't input.

Variables of VSC snipped are ignored using macro

Hello,
I'm using macro to insert custom snippet from selected var in newline.

My snippet is:

	"customprintr": {
		"prefix": "customprintr",
	 	"body": [
			"\t \\$printMe = ${CLIPBOARD}; "
	 	],
	 	"description": "Insert custom print_r"
	}	

and my macro is:

"macros": {
        "printr": [
            "editor.action.smartSelect.grow",
            "editor.action.clipboardCopyAction",
            "editor.action.insertLineAfter",
            {
             "command": "editor.action.insertSnippet",
                "args": {
                    "name": "customprintr",
                    "langId": "php",
                }
           },
        ]
    }, 

The problem is that after macro execution my clipboard is changed. My goal is obtain the same thing without change my clipboard, using for example ${TM_SELECTED_TEXT} in the snippet. I tried, but used with macro this variable is ignored.

Link to snippet variables https://code.visualstudio.com/docs/editor/userdefinedsnippets#_creating-your-own-snippets

Someone can help me?

Message-less commit & sync

Note: this is more a use-case than a bug report.

Attempting to have a no-message stage-commit-sync command (for vscode + overleaf easy integration). Used this great extension to get me almost all the way there.. However, not quite sure how to also remove the need to manually write a commit? Thoughts?

settings.json

    //--- Macros (install macros geddski extension)
    "macros": {
        "gitCommitSync": [
            "git.stageAll",
            "git.commitAll",
            // "git.pushAll",
            "git.sync"
        ]
    },

keybindings.json

    {
        "key": "ctrl+alt+cmd+/",
        "command": "macros.gitCommitSync",
    },

Feature request: Run on save

It would be really nice to be able to run macros when saving. For example, I have a macro set up that takes care of a bunch of formatting from different formatters. It would be nice if this run every time I saved a document.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.