Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm currently using gVim on Windows XP, and I have 2 follow-ups to my core question:

What is the best method of finding the line with the most characters?

My current method: I use the regex search :/^\(\p\)\{#number#,}$), and I keep increasing the integer #number# until I get just one match. In the case of my file, it is a line of only 81K characters - not 916,657 as I previously thought. I know this because when the cursor is on that line I press g + Ctrl+g and get the column count of 81K.

Followup 1) Is the question "What is the best method of finding the line with the most columns?" the same as #2 above?

Followup 2) What does the second number mean when I open a file and see the following line at the bottom of the screen:

enter image description here

I interpret this to mean that the file has 14,871 lines, and at least one row has 916,657 columns. I checked that the file does have 14,871 lines, but I have not been able to understand the purpose of the second (916K).

share|improve this question
    
Thanks to all for the answers. Each attacks the problem from a different angle! Shows the versatility of Vim. –  drapkin11 Mar 9 '11 at 23:39
1  
For its simplicity and elegance, I will use Dennis Williamson's approach. From Eelvex's I'll continue to hone my vim commands, and from garyjohn's, my vim scripting. –  drapkin11 Mar 10 '11 at 0:18

4 Answers 4

up vote 2 down vote accepted

The second number is the total character count in the whole file. If you do:

$ wc -l -c filename

you should see the same two numbers (lines and total characters). In fact, you can do:

:!wc -l -c %

Here's a plugin called textfilter (download) that includes a function to find the longest line.

Or you can use this to find the length of the longest line:

:echo max(map(range(1, line('$')), "col([v:val, '$'])")) - 1

then you can use that number like this:

/^.\{248\}$
share|improve this answer
    
I think you need col() if the number is to be used in conjunction with /^.\{num\} ($ is optional here) –  Eelvex Mar 9 '11 at 20:53
1  
@Eelvex: I made that correction. –  Dennis Williamson Mar 9 '11 at 21:13
    
very interesting - I've never thought of using Vim in the way of your Line 3, ":echo max(map(range ..." –  drapkin11 Mar 9 '11 at 23:37
1  
@drapkin11: It's from :h virtcol() –  Dennis Williamson Mar 9 '11 at 23:43

There must be a better way but the following will also do:

%s/./a/g         "Replace everything with 'a's
sort!            "Sort by column length
ggy$             "Go to first line (longest) and copy it
u                "Undo the sorting
/<c-r>"          "Search for the longest line
mm               "Mark it 'm'
u                "Undo the replace
'm               "Go to the mark - there!
share|improve this answer
1  
garyjohn's is the better way :) –  Eelvex Mar 9 '11 at 20:34
    
+1 for the answer and the comment! –  drapkin11 Mar 9 '11 at 20:47
    
Thanks. Meanwhile, Dennis's also came up and I suppose others will follow :) –  Eelvex Mar 9 '11 at 20:52
    
/<c-r>"... what does this command do? I looked it up and it means "Redo"? After I press <c-r>, I see a quotes, then when I press ", what I previously copied gets pasted. But I'm not sure what the " actually does here. –  drapkin11 Mar 9 '11 at 23:33
1  
@drapkin11: see help for i_CTRL-R = insert mode <C-R>. It inserts the contents of a register. " is the "last copy or cut" register. –  Eelvex Mar 10 '11 at 6:18

Cam't speak to the first question, but that second number in the file load message is the total number of characters in the file.

share|improve this answer

Here's a function that does that.

function MaxLine()
    let maxcol = 0
    let lnum = 1
    while lnum <= line("$")
        call cursor(lnum, 0)
        if col("$") > maxcol
            let maxcol = col("$")
            let maxline = lnum
        endif
        let lnum += 1
    endwhile
    echo "Line" maxline "has" maxcol - 1 "characters"
endfunction

You can execute it with

:call MaxLine()

or define a command or mapping to call it.

Note that finding the line with the most actual columns is the same as the line with the most characters. That may not be the same as the line with the most bytes. If you want to find the line with the most virtual columns, replace col("$") with virtcol("$").

share|improve this answer
    
this is inspiring me to learn scripting in Vim. The function works, but for some reason, I need to run it twice... first time nothing gets returned, second time, it spits out "Line" maxline "has" maxcol - 1 "characters"... I'm guessing this is intended. –  drapkin11 Mar 9 '11 at 23:35
    
No, it should work the same every time. I can reproduce that behavior on Vim 7.3.125 on a Fedora Linux system, but I can't explain it. I set 'verbose' to 15 and compared the first and second runs--no difference in the logs. Could be a bug in Vim; could be a bug in the function I'm just not seeing. –  garyjohn Mar 10 '11 at 0:46
    
Ok, it must be my gVim settings. I have it behaving like 'windows', which might account for the behaviour. In any case your response had me hit the reference books which is good. –  drapkin11 Mar 10 '11 at 0:50
    
On scripting: Once you get the hang of it, it is really neat. There is a lot I can do from within Vim with just a few keystrokes. It's like shell scripting: after doing the same task a few times you realize that you do it all in one step and without having to type options and file names every time with just a few lines of shell script or Vim script. –  garyjohn Mar 10 '11 at 0:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.