There seems to be quite a few questions around g@, its quite a confusing topic. And I can't get it to work.

I created these two mappings to edit a line from python dictionary form to variable assignment form, and back (so to switch between these two lines):

  'foo' : 'bar',      
  foo = 'bar'

Heres the mappings:

" Python variable to dict
nnoremap <leader>{ I'<ESC>ea'<ESC>f=r:A,<ESC>j
" Python dict to variable
nnoremap <leader>= ^xelxf:r=$xj

It works great, however often one wishes to run it multiple times, so I would like it to repeat with the dot operator:

I tried to follow https://vim.fandom.com/wiki/Mapping_keys_in_Vim_-_Tutorial_(Part_2)#Supplying_a_count_to_a_map

" Python variable to dict
nmap <leader>{ @g="I'<C-V><ESC>ea'<C-V><ESC>f=r:A,<C-V><ESC>j"<CR>

" Python dict to variable
nmap <leader>= @g='^xelxf:r=$xj'<CR>

But its not working

1 Answer 1


The following works:

" Python variable to dict
nmap <leader>{ @="I'<C-V><ESC>ea'<C-V><ESC>f=r:A,<C-V><ESC>j"<CR>

" Python dict to variable
nmap <leader>= @='^xelxf:r=$xj'<CR>

You seem to have added a g after the @. That was causing the problem.

UPDATE: From this SO answer

To get a dot-repeatable mapping, use this function.

function! s:var2dict(...)
  if a:0
    " perform operation
    " let save_cursor = getcurpos()
    execute "normal! I'\<ESC>ea'\<ESC>f=r:A,\<ESC>j^"
    " call setpos('.', save_cursor)
    " unlet save_cursor
    " set up
    let &operatorfunc = matchstr(expand('<sfile>'), '[^. ]*$')
    echom &operatorfunc
    return "g@\<space>"
nnoremap <expr> <leader>{ <sid>var2dict()

The function uses :h operatorfunc to set this mapping as the operation to perform the next time DOT is pressed. More detailed explanation is given at the SO answer linked here

This function does not (yet) support counts.

  • Good find! Although it now works the first time, it's no better than the orginal version in that if I press '.' (dot), it does not repeat the operations, it just repeats the last 'x' (delete char). Jul 28, 2022 at 14:25
  • Right. Because you're only mapping (simulating) a repeatable action. Vim itself is not seeing it as a discrete action. Interesting use case. I remember reading about a solution somewhere. Lemme see if I can find it.
    – 3N4N
    Jul 28, 2022 at 14:33
  • @run_the_race, tpope to the rescue. vimcasts.org/episodes/…
    – 3N4N
    Jul 28, 2022 at 14:48
  • @run_the_race, updated the answer
    – 3N4N
    Jul 28, 2022 at 15:40
  • working brilliantly, well done! Jul 29, 2022 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.