Given I sometime use single quotes and other times doubles quotes, here is another variation on B-Layer :substitute command that supports both, plus the reverse operation.
In order to remember the names, I've wrapped the two actions into commands.
command! -range=1
\ ToAttribute <line1>,<line2>s/\v\[(["'])([^"']+)\1]/.\2/gc
command! -range=1 -nargs=? -complete=customlist,s:compl_to_dict
\ ToDict call s:to_dict(<f-args>)
" Internal support functions for :ToDict
function! s:to_dict(...) range abort
let quote = get(a:, 1, "'")
exe printf('%s,%ss/\v.(\k+)/[%s\1%s]/gc', a:firstline, a:lastline, quote, quote)
endfunction
function! s:compl_to_dict(...) abort
return ['"', "'"]
endfunction
dscomes from this plugin: https://github.com/tpope/vim-surround - it's not "vanilla" Vim. (AFAIK anyway :) ) – Zoe is on strike Aug 19 '21 at 08:37