Why a macro? You could very well use a snippet instead. For example (with NeoSnippet):
Create a new global snippet with :NeoSnippetEdit _
(replace _
with a filetype if you want a dedicated snippet) and add this content:
snippet log
console.log 'printing ${1:TARGET}'
console.log JSON.stringify($1, ${2:undefined}, 2)${0}
Save and exit the file.
Usage:
Visually select your text, hit Tab (or the key you have configured to trigger a snippet) and you will be asked for the snippet name to be inserted: write 'log', Enter and the above code will be inserted, with cursor on 1
st position and the TARGET
replaced by your selected text, in both positions. Hit Tab again to accept the text and jump to the second marker (undefined
) - you can edit it, or not, and if you hit Tab again, the cursor will be placed at the end of the construct ( position 0
).
Now, because this is a snippet, you can also use it ... before you have the text to be logged: you write log
and hit Tab, and the snippet will be expanded: write logThis
(or whatever text you want to log), hit Tab, accept undefined
or change it, hit Tab again and ... that's it.
If you prefer using UltiSnips
instead, see this link.
l
by using"ly
instead ofy
and<C-R>l
instead of<C-R>"
.