Is there a way to tab through fields and values in a json file in VIM as you would form fields on a web form?


There is not native way to do this however a few mappings should do the trick.

Add the following to ~/.vim/after/ftplugin/javascript.vim:

nnoremap <buffer> <tab> :<c-u>call search('\v("<bar>null<bar>\d+<bar>true<bar>false<bar>\[\]<bar>\{\})\ze\n=\s*[]:,}]')<bar>execute "normal! vi". (getline('.')[col('.')-1] == '"' ? '"' : 'w')<cr>
xmap <buffer> <tab> <esc>`>l<tab>

The basic idea is to search for items followed by ,, }, ].

For more information see:

:h :search
:h <bar>
:h /\|
:h /\ze
:h :execute
:h :normal
:h getline()
:h col()
:h c_CTRL-U
| improve this answer | |
  • Nice quick fix. It might break though, if you are sitting inside a string which contains one of the matching patterns. There have been a couple of questions recently about looking up the syntax attached to characters. That could be used to skip unwanted matches. – joeytwiddle Aug 18 '15 at 0:49
  • 1
    It would break if the latter part of the mapping didn't use vi" to actually select the string. You could also do some fancy look-behind to avoid sniffing syntax. – Peter Rincker Aug 18 '15 at 1:55
  • Awesome. This mapping is smarter than me. – joeytwiddle Aug 19 '15 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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