0
function! JumpDef()
    normal! <C-]>
endfunction
if &filetype != "elixir"
    nnoremap <C-]> :call JumpDef()<CR>
endif

If the filetype is not elixir, pressing <C-]> does nothing. I would have expected the above to do exactly the same thing as if the above code was not there. Why does it not exist?

1 Answer 1

5

The notation you're using for CTRL-] doesn't work in that context. The command you're using will act as though you had typed the following five keystrokes in normal mode: <C-]>

In order to invoke the CTRL-] functionality, use the following instead:

:execute "normal! \<C-]>"

(See :help :execute and :help string for more details of how this works.)

As an aside, if the code you've pasted is in your vimrc, it won't have the effect you're aiming for, of creating a mapping that doesn't apply to the elixir filetype.

The code in your vimrc is run when Vim is starting up before the file is opened: at that time, 'filetype' will never be set to elixir, and thus the mapping will always be created.

One simple way to fix this is to move the filetype check into the mapping:

function! JumpDef()
    if &filetype != "elixir"
        execute "normal! \<C-]>"
    endif
endfunction

nnoremap <C-]> :call JumpDef()<CR>

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.