Search code examples
visual-studio-2010eventsvsxvs-extensibility

Intercept Visual Studio paste action


Is it possible to intercept the paste event of visual studio in order to transform the text being pasted in? I know I can write my own command to do the smart paste, but it's a chore for most people to map this command to a keyboard shortcut. Besides, most people are used to Ctrl+V. Any chance I can plug in my wares in there?


Solution

  • It's a command, and you can handle it by adding a command filter in an extension and handling the command before the editor command handling implementation sees it.

    Here's a command handler template. I believe the paste command ID is VSConstants.VSStd2KCmdID.PASTE (using the VSConstants.VSStd2K command guid for the group).

    Note, however, that pasting can be much harder than it looks, when you take into account things like pasting box-copied content, pasting into a multi-line selection, empty selection cut/copy and paste, and language service formatting after paste. So, if you are trying to do this with an extension you want to publish, it may be harder than you expect :-/