Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=347
 
|id=347
 
|previous=346
 
|previous=346
 
|next=348
 
|next=348
|created=October 18, 2002
+
|created=2002
|complexity=intermediate
+
|complexity=basic
|author=Kartik Agaram
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=18/11
 
|rating=18/11
  +
|category1=
  +
|category2=
 
}}
 
}}
  +
To format the current paragraph:
<pre>
 
  +
*<code>gqap</code>
map &lt;silent&gt; &lt;C-j&gt; :let line=line(".")&lt;CR&gt;:let col=col(".")&lt;CR&gt;gqap:exec ":".line&lt;CR&gt;:exec "normal " . col . "\|"&lt;CR&gt;
 
  +
*<code>gwap</code> same, but restores cursor position
imap &lt;silent&gt; &lt;C-j&gt; &lt;Esc&gt;:let line=line(".")&lt;CR&gt;:let col=col(".")&lt;CR&gt;gqap:exec ":".line&lt;CR&gt;:exec "normal " . col . "\|"&lt;CR&gt;a
 
</pre>
 
   
 
==Comments==
 
==Comments==
  +
{{todo}}
Why not simply use &lt;C-O&gt; after gq command?
 
  +
*Thanks to anon who pointed out that gwap makes the original tip obsolete (I have removed it).
 
  +
*We don't have any other tips that explain basics of paragraph formatting, so this may as well be it.
----
 
  +
*Need to explain what "format" means here.
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 &lt;Esc&gt; gqap &lt;C-o&gt; doesn't restore cursor position. At least for me.
 
  +
*Need to mention textwidth, formatexpr, formatprg and probably more (but keep it simple).
 
----
 
Ok, mea culpa - I always used gq}, and it worked. The gqap is different.
 
 
----
 
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&lt;zqfm&gt;&lt;esc&gt;gqip?&lt;zqfm&gt;&lt;cr&gt;df&gt;
 
 
----
 
Absolutely outstanding.
 
 
----
 

Latest revision as of 05:25, 13 July 2012

Tip 347 Printable Monobook Previous Next

created 2002 · complexity basic · version 6.0


To format the current paragraph:

  • gqap
  • gwap same, but restores cursor position

Comments[]

 TO DO 

  • Thanks to anon who pointed out that gwap makes the original tip obsolete (I have removed it).
  • We don't have any other tips that explain basics of paragraph formatting, so this may as well be it.
  • Need to explain what "format" means here.
  • Need to mention textwidth, formatexpr, formatprg and probably more (but keep it simple).