2
votes

With VIM, I can use Tim Pope's wonderful vim-surround plugin to place the cursor within a string and issue the ds" keyboard sequence in order to unquote it. How can an equivalent be achieved in VS natively or with Resharper?

  • VIM + vim-surround:

    "aaa|aaaaa" ---> press ds" ---> aaa|aaaaa

  • In VS, I know I can use a regex Find/Replace query with:

    |"aaaaaaaa" ---> press Ctrl+H ---> ...

    Find: "(.+?)"|'(.+?)' Replace with: $1

    ... press Alt+R to replace next, and we have: aaaaaaaa (the next matching quoted string is selected)

... but I'd like to turn it into a macro triggered by a keyboard shortcut.

I installed the Macros for Visual Studio extension from the Visual Studio Gallery, but this doesn't register interactions with the Find/Replace dialogue when recording a macro.

I tried writing a Macro myself based on Using Search and Replace Macros in Visual Studio, but it doesn't work. I'm getting errors that e.g. vsFindTarget and vsFindPatternSyntax.vsFindPatternSyntaxRegExpr are not defined.

Here's my failed macro code:

dte.ExecuteCommand("Edit.Replace");

dte.Find.FindWhat = "\"(.+?)\"|'(.+?)'";
dte.Find.ReplaceWith = "$1"
dte.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
dte.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
dte.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
dte.Find.Action = vsFindAction.vsFindActionReplace
dte.Find.Execute()

I looked at Microsoft's documentation mentioned in this SO answer, but I'm getting issues as above. Makes me think that Macros for Visual Studio extension has it's own API on top of the native VS API, or (most likely) I'm just not getting it.

Any help would be appreciated. Thanks!

1
vsFindTarget class is in the EnvDTE namespace. You can try to access it like EnvDTE.vsFindTarget.vsFindTargetCurrentDocument.Sergey Vlasov
Unfortunately, this didn't work: 'EnvDTE' is undefined.Michael
It seems some of the information I need is here: How do I start developing VSIX extensions? and here: How do I set keyboard shortcuts in a VSPackage?. I'll post back if I figure it out.Michael
I also found this: How to: Automate Text Search and Replace. I just have to put it all together.Michael
Sorry, I don't have much experience with Macros for Visual Studio extension. As an alternative to developing a vsix extension, you can run this macro with my Visual Commander.Sergey Vlasov

1 Answers

0
votes

I settled for integrating Vim with Visual Studio using Vim as an external tool, but with console Vim rather than gVim:

  1. In VS, go to Tools > External Tools... > Add

    • Title: &Vim
    • Command: C:\[...]\usr\bin\mintty.exe (or wherever you have mintty.exe - e.g. it's part of the Git for Windows installation)
    • Arguments: "C:\[...]\usr\bin\vim.exe" +"call cursor($(CurLine),$(CurCol))" +"runtime visualstudioinvoke.vim" "$(ItemFileName)$(ItemExt)" (use the actual path to your vim.exe; a very long path might not work)
    • Initial directory: $(ItemDir)
    • Optionally, set the tool as the first one, for convenience (see point 4)
    • Click OK
  2. Create ~/.vim/visualstudioinvoke.vim or ~/vimfiles/visualstudioinvoke.vim depending on whichever works (for me, the former did) with the following contents:


    set autoread
    autocmd CursorMoved,CursorMovedI * call CheckTime()

    " How many seconds to wait between file timestamp checks
    let s:check_time_delay=5
    let s:last_check_time=reltime()

    function CheckTime()
      if(reltime(s:last_check_time)[0] > s:check_time_delay)
        let s:last_check_time=reltime()
        checktime %
      endif
    endfunction

The above makes sure Vim automatically reloads the file if it's been changed in VS (or anywhere else outside of Vim). The check happens on cursor move within Vim, but not more often than every 5 seconds.

  1. Back in VS, go to Tools > Options > Environment > Documents and thick Detect when file is changed outside the environment and Reload modified files unless there are unsaved changes

  2. Add a keyboard shortcut for the Vim external tool in VS:

    • Go to Tools > Options > Environment > Keyboard and Show commands containing: Tools.ExternalCommand1 (assuming Vim is the first external tool).
    • Under Press shortcut keys, I used the [Ctrl]+[`] (backtick) combo; use whatever you like.

That's it. Now you can quickly open in Vim the file you're looking at in VS and the cursor position is preserved. Also the saved changes are synchronised automatically between the editors. For the text unquoting, make sure you have vim-surround plugin in your Vim.