How can I quickly quote/unquote words and change quoting (e.g. from \'
to \"
) in Vim? I know about the surround.vim plugin, but I would like to use
Adding Quotes
I started using this quick and dirty function in my .vimrc
:
vnoremap q :call QuickWrap("'")
vnoremap Q :call QuickWrap('"')
function! QuickWrap(wrapper)
let l:w = a:wrapper
let l:inside_or_around = (&selection == 'exclusive') ? ('i') : ('a')
normal `>
execute "normal " . inside_or_around . escape(w, '\')
normal `<
execute "normal i" . escape(w, '\')
normal `<
endfunction
So now, I visually select whatever I want (typically via viw
- visually select inside word) in quotes and press Q for double quotes, or press q for single quotes.
Removing Quotes
vnoremap s :call StripWrap()
function! StripWrap()
normal `>x`
I use vim-textobj-quotes so that vim treats quotes as a text objects. This means I can do vaq
(visually select around quotes. This finds the nearest quotes and visually selects them. (This is optional, you can just do something like f"vww
). Then I press s
to strip the quotes from the selection.
Changing Quotes
KISS. I remove quotes then add quotes.
For example, to replace single quotes with double quotes, I would perform the steps:
1. remove single quotes: vaqs
, 2. add new quotes: vwQ
.