
Sample Asciidoc File
By:
bairui on
Jan 17th, 2012 | syntax:
None | size: 1.77 KB | hits: 93 | expires: Never
Jump to the Longest Line
========================
Barry Arthur
v0.6, January 13, 2012
:pygments:
__The evolution of a Vimmer__
Ignoring the prosaic question of ``Why would you ever want to?!'',
consider how, in your editor of choice, you would jump to the longest
line in the file.
Vim provides a _lot_ of jump commands, possibly more so than any
other editor. We can jump to a matching item; jump between words,
sentences and paragraphs; jump between classes or #defined regions in
a source file; jump between files and back and more.
Jump-to-longest-line, however, is missing. This forces us to craft a
solution using either existing builtin Vim commands, the ex commands,
VimL (Vim's scripting language), or (more typically) a mixture of all
three.
We compare three different approaches to solving this problem, each
one arguably a little further along in the ``evolution'' of a Vim
user.
Approach #1: Normal and Ex Commands
-----------------------------------
.++LongestLine_Ex++ - Uses mostly Vim Normal and Ex commands:
[source,vim,numbered]
------------------------------------------------------------------
function! LongestLine_Ex()
global/^/substitute/^/\=len(getline('.')).' '.line('.').' '
sort n
normal! Gwyiwu
return @"
endfunction
-------------------------------------------------------------------
This style of solution would be considered by capable Vim users who
have not yet ventured very far (if at all) into VimL -- limiting their
choices to just Normal Mode and Ex commands.
NOTE: Although this solution is presented here in a function (for
comparative purposes with the other solutions presented later), these
sorts of solutions are typically hand-typed when needed or, at best,
briefly stored in macros.