1

I want to map W to jump to the next keyword of the language

or

jump to the next interestingly syntax highlighted word

eg:

For the following snippet

SELECT *
FROM Fruits
WHERE Fruits.name = 'Apple' AND Fruits.color = 'Red' AND Fruits.taste = 'crisp'

A desirable result when pressing W should go to SELECT->FROM->WHERE->AND or something similar

1 Answer 1

2

You can do it like this:

    function! MoveCursor(forward)
        let stop_re = 'sql\%(Keyword\|Operator\|Statement\)'
        let curpos=getcurpos()
        while search('\<', 'W'.a:forward)
            if synIDattr(synID(line("."), col("."), 1), "name") =~ stop_re
                break
            endif
        endwhile
        if synIDattr(synID(line("."), col("."), 1), "name") !~ stop_re
            call setpos('.', curpos)
        endif
    endfunction

    nnoremap W :call MoveCursor('')<cr>
    nnoremap B :call MoveCursor('b')<cr>

Where you want to stop, you configure with the stop_re which is a regular expression, taking the syntax groups on which to stop. You can check with :echo synIDattr(synID(line('.'),col('.'),1),'name') with the cursor on an interesting syntax definition and add it to that regular expression.

The rest is basically, search forward (or backward) for beginning of the next word, check that it matches your stop regular expression and continue or stop.

2
  • Ok thanks! But this function only works for sql filetypes right? Is there anyway to extend it to any general filetype with syntax highlighting? Commented Aug 25, 2017 at 7:21
  • 2
    depends on what you define as interesting. There is no general approach, for each filetype you would have to define a custom set of syntax groups on which to stop. Commented Aug 25, 2017 at 7:22

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.