5

In Vim, it's a quick 3-character command to change what's inside the current quoted string (e.g., ci"), but is there a simple way to change what type of quotes are currently surrounding the cursor?

Sometimes I need to go from "blah" to """blah""" or "blah" to 'blah' (in Python source code) and I'd ideally like to do it quickly using default key bindings.

Nick Knowlson
  • 7,185
  • 6
  • 47
  • 63
Mzzzzzz
  • 4,770
  • 7
  • 30
  • 47

2 Answers2

18

Try the surround.vim plugin. I find it an essential addition to any vim installation.

Jay
  • 56,361
  • 10
  • 99
  • 123
  • To go from quotes to triple-quotes, do `ysa""wysa""`. It is worth putting in a macro or binding it. For example, `:noremap :normal ysa""wysa""` will bind this to `Ctrl`+`c`. Alternatively, you can just map `:noremap :normal ysiw"w` to surround any one word with quotes, and then press `Ctrl`+`c` or whatever your binding is *n* times to add *n* sets of quotes. – Jay Sep 11 '10 at 01:13
  • I've been using vi since 1980 and ya learn something new every day. – Peter Rowell Sep 11 '10 at 05:04
2

Surround.vim is great, but I don't think it'll handle your triple-quoted needs directly.

The way I've done stuff along these lines (when surround wasn't appropriate) was to use %, make the change, then double-backtick to go back to the starting point. E.g. if the cursor is somewhere in a single-quoted string, do f'%, make the change, then double-backtick and ..

dash-tom-bang
  • 17,383
  • 5
  • 46
  • 62