Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #347 - Format paragraph without changing the cursor position

Created: October 18, 2002 21:02 Complexity: intermediate Author: Kartik Agaram Version: 6.0 Karma: 18/11 Imported from: Tip#347

map <silent> <C-j> :let line=line(".")<CR>:let col=col(".")<CR>gqap:exec ":".line<CR>:exec "normal " . col . "\

Comments

Why not simply use <C-O> after gq command?

Anonymous , October 23, 2002 1:20


Hmm... let me try to describe a case. Let's say you open a text file and start inserting text somewhere in between without ever adding to the jump list (just arrow keys, '{}'). Now typing <Esc> gqap <C-o> doesn't restore cursor position. At least for me.


Kartik , October 23, 2002 17:52


Ok, mea culpa - I always used gq}, and it worked. The gqap is different.

Anonymous , October 24, 2002 6:33


It may be more desirable to preserve the cursor's logical position (i.e., which word it is currently on), rather than its line/col position.

The following nmap does this by inserting a small and unusual bogus text at the current cursor position; formatting the paragraph; using search to get to the bogus word; and then deleting it.

nmap gb i<zqfm><esc>gqip?<zqfm><cr>df>


ds26--AT--gte.com (Dorai Sitaram) , March 13, 2003 11:47


Absolutely outstanding. Thanks, Dorai!

Kartik , October 23, 2003 19:07


Is there a way to make "gqap" use filling spaces to have block alignment instead of left alignment in the lines?

thomas--AT--schuerger.com , November 23, 2003 12:56


Advertisement