1

This question already has an answer here:

The default vim filetype commands create new bindings beginning with [ and ]. Is there a way to prevent them from doing so or control which bindings are created?

I like using [ and ] to scroll up and down by half pages, by analogy with { and } for paragraph navigation.

nnoremap ] <c-d>
vnoremap ] <c-d>
xnoremap ] <c-d>

nnoremap [ <c-u>
vnoremap [ <c-u>
xnoremap [ <c-u>

It works well for the text filetype, but programming language filetypes typically have bindings beginning with [ and ], for instance for Python files there are many structural navigation commands that I tend not to use:

...
n  [M          *@:call <SNR>25_Python_jump('n', '\v\S\n*(^(\s*\n*)*(class|def|async def)|^\S)', 'Wb', 0, v:count1)<CR>
n  [m          *@:call <SNR>25_Python_jump('n', '\v^\s*(class|def|async def)>', 'Wb', v:count1)<CR>
n  []          *@:call <SNR>25_Python_jump('n', '\v\S.*\n+(def|class)', 'Wb', 0, v:count1)<CR>
n  [[          *@:call <SNR>25_Python_jump('n', '\v^(class|def|async def)>', 'Wb', v:count1)<CR>
...
n  ]M          *@:call <SNR>25_Python_jump('n', '\v\S\n*(%$|^(\s*\n*)*(class|def|async def)|^\S)', 'W', 0, v:count1)<CR>
n  ]m          *@:call <SNR>25_Python_jump('n', '\v%$|^\s*(class|def|async def)>', 'W', v:count1)<CR>
n  ][          *@:call <SNR>25_Python_jump('n', '\v%$|\S.*\n+(def|class)', 'W', 0, v:count1)<CR>
n  ]]          *@:call <SNR>25_Python_jump('n', '\v%$|^(class|def|async def)>', 'W', v:count1)<CR>

The file that defines this is located at

/usr/share/vim/vim81/ftplugin/python.vim

and does not appear to check the value of a "configuration variable" to determine whether it should bind keys.

Is there a creative way to overrule it?

marked as duplicate by Hotschke, Community Jan 8 at 8:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    There are quite a few [ / ] mappings. Some of them pretty use full (e.g. ]p and [I). Have you thought about a different set of keys? Maybe <up> / <down> or just use <c-u> & <c-d>. Personally, I bind <d-j> & <d-k>, but that is MacVim specific – Peter Rincker Jan 7 at 23:23
3

You can unmap these within a file called ~/.vim/after/ftplugin/python.vim or $HOME/vimfiles/after/ftplugin/python.vim on windows, etc. containing:

silent! nunmap <buffer> ]]
silent! nunmap <buffer> [[
silent! nunmap <buffer> ][
silent! nunmap <buffer> []
" and so on ...

vim executes scripts of the form after/ftplugin/{filetype}.vim after ftplugin/{filetype}.vim so you cna provide arbitrary overrides to the defaults.

Alternatively, you can use autocmds within your vimrc as follows:

function! PythonUnmaps()
    silent! nunmap <buffer> ]]
    silent! nunmap <buffer> [[
    silent! nunmap <buffer> ][
    silent! nunmap <buffer> []
    " and so on...
endfunction
augroup my_python_overrides
    au!
    autocmd FileType python call PythonUnmaps()
augroup END

Both of these approaches are general, you can replace python with whichever filetype you want to alter the settings for.

  • Is there a way to override it from within the .vimrc itself? – Gregory Nisbet Jan 7 at 21:48
  • edited that into the answer – Mass Jan 7 at 22:24
2

According to the help :h no_mail_maps and :h no_plugin_maps, it should be possible to prevent the creation of a filetype specific mapping by setting the variable:

:let g:no_<filetype>_maps = 1`

in your .vimrc, with <filetype> being replaced by python in your case. Unfortunately, the official python filetype plugin does not support this guard yet.

However, the upstream python Vim filetype repository does already support that as of commit db75a4614ffef770

So you could simply clone that plugin locally and use that, until the maintainer has sent update runtime files to Bram for inclusion with Vim (and Bram has committed it to the Vim repository).

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