vim remap with a newline at the end - vim

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?

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

How about this:
nnoremap <leader>b oimport pdb;<CR>pdb.set_trace()<CR>

Related

Vim substitute till end of line in normal mode

I have some text yanked using yiw. Now, on another line, I want to replace text from current cursor position until end of line, so I try v$p. However, this also deletes the newline at the end of the line which I don't want to happen. How do I address this?
You can:
adjust the visual area before putting:
v$hp
use the :help g_ motion if you don't have trailing space:
vg_p
You can delete into the null register and then paste with "_Dp
Alternatively, if you want to save the text you're deleting, you can delete in the normal buffer, and past from the 0 buffer with D"0p
I find register keys clunky, so I have maps in my .vimrc for them:
" leader means "another option key like cmd or meta, but one you get to choose"
let mapleader = "\<Space>"
" get the yank register, not the thing you just deleted
nnoremap <Leader>p "0p
nnoremap <Leader>P "0P
" don't copy the thing you're gonna delete into the delete register
nnoremap <Leader>d "_d
nnoremap <Leader>D "_D
With these binds, it becomes Dp or D p depending on whether you want to keep the deleted text.
If you don't want to mess with registers at all, there's always the simple PlD.

vim how to break new line without entering Insert mode

I have a few lines of codes that are too that I would like to break into 2 lines at certain location.
So instead of moving to the position then presse I to insert mode then Enter to break line then finally ESc back.
Is there way I can do it easier in normal mode only?
Many thanks.
You can define some simple mapping for it:
" <C-Enter> Insert single / [count] newline.
nnoremap <C-CR> i<CR><Esc>
Note that <C-CR> probably only works in GVIM, not in a terminal; choose a different key if necessary.
Here's an additional mapping that keeps the cursor on the original line:
" <C-S-Enter> Append single / [count] newline.
function! s:AppendCRSetPos()
keepjumps call setpos("''", getpos('.'))
return ''
endfunction
nnoremap <expr> <SID>(AppendCRSetPos) <SID>AppendCRSetPos()
nnoremap <script> <C-S-CR> <SID>(AppendCRSetPos)i<CR><Esc>g``
I have the following in my .vimrc
nnoremap S i<cr><esc>^mwgk:silent! s/\v +$//<cr>:noh<cr>`w
Which will split the line on pressing capital S.
Hope this helps
There is a vim key for that:
r<Enter>

Cursor moves to next line when leaving insert mode with remapped <C-Space>

I have remapped <C-Space> to <Esc> using the this line in my .vimrc:
inoremap <C-Space> <Esc>
when running under MacVim or gVim (I use inoremap <Nul> <Esc> when I don't have gui_running)
It works fine except when I leave insert mode with the cursor at the end of the line. That is, if I do A to put the cursor at the end of the line, and then type <C-Space>, the cursor moves down to the first column of the next line. Hitting <Esc> leaves the cursor on the last character.
Why does it do this, and how can I stop it? The <Nul> remapping doesn't have this problem.
Most likely you have a trailing space in your mapping. Removing it should solve your problem. Space move the cursor one character forward.

vim insert mode mapping doesn't work

I want to move one word forward in insert mode.
Why this doesn't work:
inoremap ,w <esc>w
But this works(back one word):
inoremap ,b <esc>b
That's because when the cursor is at the beginning of a word, the <Esc> will move the cursor one character left (this is a bit unintuitive, but default vi behavior), and the w will only move to the original position.
This should work:
inoremap ,w <esc>ww
mapping critique
I don't particularly like your mappings:
starting it with , adds a delay whenever you type a comma
do you really need a command that leaves insert mode and moves the cursor? <Esc>b achieves the same and also is two keystrokes (many users remap the <Esc> key to be in a less cumbersome position)
if you really must navigate in insert mode, there's already <C-Left> / <C-Right>.

In Vim, can I format a whole file and end with the cursor where it started?

I set up this mapping in my .vimrc and it works great...
" Auto indent entire file
nmap <C-f> gg=G
imap <C-f> <ESC>gg=G
However, after the operation the cursor has moved to line 1, column 1.
Is there a way to do it so that if I'm in the middle of the file somewhere the cursor will remain where it is?
Sure, use marks (:help mark):
nmap <C-f> mtgg=G't
imap <C-f> <ESC><C-f>
Before executing gg=G, the current cursor position is saved to mark t. After the operation, 't jumps back to the mark.
Ctrl+O is good for walking back through the jump list. '' will move you back to the last line in the jump list (or `` to go back to the last line and column).
Unfortunately, there isn't an "entire buffer" text object, so gg=G requires moving back two places in the jump list.
Brian's solution above will work for a macro, but as a good tip, note that Ctrl+O will go to the previous cursor position in the jump list. So if you ever do an operation that moves away, you can step back to a previous position.
Why not use ma to mark the current position in buffer a, and after the transformation use ``a(i.e.backtick+a`) to return to that position ? Here's an article on using marks to move around.
As jamessan wrote, Ctrl+o jumps back to the last posistion in the jumplist. After calling gg=G, this has to be called twice.
Thus, you can use a mapping without marks:
map <silent> <C-f> gg=G<C-o><C-o>
imap <silent> <C-f> <Esc> gg=G<C-o><C-o>

Resources