Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After trying @hobbs answer I noticed that when filtering the entire buffer through <code>perltidy</code> the cursor returned to byte 1, and I had to make a mental note of the original line number so I could go back after <code>:Tidy</code> completed.</p> <p>So building on @hobbs' and @Ignacio's answers, I added the following to my <code>.vimrc</code>:</p> <pre><code>"define :Tidy command to run perltidy on visual selection || entire buffer" command -range=% -nargs=* Tidy &lt;line1&gt;,&lt;line2&gt;!perltidy "run :Tidy on entire buffer and return cursor to (approximate) original position" fun DoTidy() let l = line(".") let c = col(".") :Tidy call cursor(l, c) endfun "shortcut for normal mode to run on entire buffer then return to current line" au Filetype perl nmap &lt;F2&gt; :call DoTidy()&lt;CR&gt; "shortcut for visual mode to run on the the current visual selection" au Filetype perl vmap &lt;F2&gt; :Tidy&lt;CR&gt; </code></pre> <p>(closing <code>"</code> added to comments for SO syntax highlighting purposes (not required, but valid vim syntax))</p> <p><code>DoTidy()</code> will return the cursor to its original position plus or minus at most <code>X</code> bytes, where <code>X</code> is the number of bytes added/removed by <code>perltidy</code> relative to the original cursor position. But this is fairly trivial as long as you keep things tidy :).</p> <p>[Vim version: 7.2]</p> <p><strong>EDIT:</strong> Updated <code>DoTidy()</code> to incorporate @mikew's comment for readability and for compatibility with Vim 7.0</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload