1

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?

1 Answer 1

4

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
3
  • 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. Aug 18, 2015 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. Aug 18, 2015 at 1:55
  • Awesome. This mapping is smarter than me. Aug 19, 2015 at 0:37

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.