Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jump to the next python class or function in vim

Tags:

python

vim

I'm trying to jump to the next python class or function in vim with the following commands:

autocmd FileType python nnoremap <buffer> [[ ?^class|^\s*def<CR>
autocmd FileType python nnoremap <buffer> ]] /^class|^\s*def<CR>

But it doesn't work. Vim prompted:

Error detected while processing FileType Auto commands for "python":
E492: Not an editor command: ^\s*def<CR>

How to fix this?

like image 599
Searene Avatar asked Jun 27 '26 11:06

Searene


1 Answers

After lots of trying, I found the following code worked. I need to add \\ before |

autocmd FileType python nnoremap <buffer> [[ ?^class\\|^\s*def<CR>
autocmd FileType python nnoremap <buffer> ]] /^class\\|^\s*def<CR>

As an alternative way, I found that putting the two lines in ~/.vim/ftplugin/python.vim is more convenient

nnoremap [[ ?^class\|^\s*def<CR>
nnoremap ]] /^class\|^\s*def<CR>
like image 177
Searene Avatar answered Jun 29 '26 00:06

Searene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!