0

I am trying to have a remap in my vimrc that adds a snippet, then goes to the next line in Insert mode:

:nnoremap <leader>b oimport pdb;pdb.set_trace()<esc> o

The snippet import pdb;pdb.set_trace() gets inserted into my current buffer, but the cursor in vim remains on the same line. Is there any way to have the cursor move to the next line after inserting the snippet?

AruniRC
  • 5,070
  • 7
  • 43
  • 73
  • 1
    Possible duplicate of [Insert predefined text on keyboard shortcut](https://stackoverflow.com/questions/17086362/insert-predefined-text-on-keyboard-shortcut) – AruniRC Jan 06 '19 at 05:44

2 Answers2

2

How about this:

nnoremap <leader>b oimport pdb;<CR>pdb.set_trace()<CR>
Ralf
  • 1,773
  • 8
  • 17
2

For me, your mapping works, except at the end of the buffer (where it beeps and keeps the cursor at the end of the inserted line, as you report).

The reason is the space between the <esc> and o. :help <Space> is a motion (to the right, just like l), and if the cursor cannot move there (at the end of the buffer, maybe also elsewhere depending on the 'whichwrap' option), Vim beeps and aborts the mapping; i.e. all keys that come after that are ignored.

The fix is easy: Drop the superfluous whitespace, and the mapping will work everywhere!

nnoremap <leader>b oimport pdb;pdb.set_trace()<esc>o
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324