Vim Tips Wiki

Yank last line and trim to the cursor

Redirected from VimTip766

1,623pages on
this wiki
Add New Page
Add New Page Talk0
Tip 766 Printable Monobook Previous Next

created 2004 · complexity intermediate · author Levin Du · version 6.0

I often come across lines like these:

p_ST->localconnectionOption->typeofService = 0;
p_ST->localconnectionOption->gain = 0;
p_ST->localconnectionOption->r_reservation = 0

Though Vim has completion mode, it's a wasting time of repeately typing 'p_ST->local<C-N>->...'. So I write this mapping:

:inoremap <c-f> <Esc>:let g:saved_col=virtcol(".")<CR>ddkYp:exe "normal ".(g:saved_col+1)."\|"<CR>C

Take above lines for example. First, I type one line (with autoindent turn on, the cursor position is shown as "|" after finishing the line with Return):

p_ST->localconnectionOption->typeofService = 0;

then I use <Tab> and <Space> to move the cursor to the right place:

p_ST->localconnectionOption->typeofService = 0;

and press <Ctrl>-f, can you see the magic? It turns out to:

p_ST->localconnectionOption->typeofService = 0;


Here's an alternative mapping.

ino <silent> <c-f> <c-o>:exe "norm! kly0jPD"<CR>

I want to recommend <C-X><C-L> - line completion. Not exactly the same as this, but useful in similar situations. After a line completion, you can use <C-P> and <C-N> as usual for previous and next match.

See :help ins-completion.

What about <C-X><C-N>? I just discovered this recently as something between "whole-line completion" <C-X><C-L> and "word completion" <C-N>. It works like <C-N> completion, but it's more like "complete more".

So from the previous example:

p_ST->localconnectionOption->typeofService = 0;



you get:




you get:




you get:


No spacing/tabbing and scripting necessary!

It is worth to mention CTRL-y (and CTRL-e) and the wordwise CTRL-y tip on this occasion -

Also on Fandom

Random Wiki