开发者

longest line in vim?

开发者 https://www.devze.com 2022-12-17 01:22 出处:网络
Is there a command to determine length of a longest line in vim? And to appe开发者_StackOverflow中文版nd that length at the beginning of the file?Gnu\'s wc command has a -L --max-line-length option wh

Is there a command to determine length of a longest line in vim? And to appe开发者_StackOverflow中文版nd that length at the beginning of the file?


Gnu's wc command has a -L --max-line-length option which prints out the max line length of the file. See the gnu man wc. The freebsd wc also has -L, but not --max-line-length, see freebsd man wc.

How to use these from vim? The command:

:%!wc -L

Will filter the open file through wc -L and make the file's contents the maximum line length.

To retain the file contents and put the maximum line length on the first line do:

:%yank
:%!wc -L
:put

Instead of using wc, Find length of longest line - awk bash describes how to use awk to find the length of the longest line.

Ok, now for a pure Vim solution. I'm somewhat new to scripting, but here goes. What follows is based on the FilterLongestLineLength function from textfilter.

function! PrependLongestLineLength ( )
  let maxlength   = 0
  let linenumber  = 1
  while linenumber <= line("$")
    exe ":".linenumber
    let linelength  = virtcol("$")
    if maxlength < linelength
      let maxlength = linelength
    endif
    let linenumber  = linenumber+1
  endwhile

  exe ':0'
  exe 'normal O'
  exe 'normal 0C'.maxlength
endfunction

command PrependLongestLineLength call PrependLongestLineLength()

Put this code in a .vim file (or your .vimrc) and :source the file. Then use the new command:

:PrependLongestLineLength

Thanks, figuring this out was fun.


If you work with tabulations expanded, a simple

:0put=max(map(getline(1,'$'), 'len(v:val)'))

is enough.

Otherwise, I guess we will need the following (that you could find as the last example in :h virtcol(), minus the -1):

0put=max(map(range(1, line('$')), "virtcol([v:val, '$'])-1"))


:!wc -L %
rather than
:%!wc -L

To append that length at the beginning of the file:
:0r !wc -L % | cut -d' ' -f1


Here is a simple, hence easily-remembered approach:

  • select all text: ggVG
  • substitute each character (.) with "a": :'<,'>s/./a/g
  • sort, unique: :'<,'>sort u
  • count the characters in the longest line (if too many characters to easily count, just look at the column position in the Vim status bar)

I applied this to examine Enzyme Commission (EC) numbers, prior to making a PostgreSQL table:

I copied the ec_numbers data to Calc, then took each column in Neovim, replaced each character with "a",

:'<,'>s/./a/g

and then sorted for unique lines

:'<,'>sort u

aaaaaaa
aaaaaaaa
aaaaaaaaa
aaaaaaaaaa
aaaaaaaaaaa

... so the longest EC number entry [x.x.x.x] is 11 char, VARCHAR(11).

Similarly applied to the Accepted Names, we get

aaaaa
aaaaaa
...
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

i.e. the longest name is 147 char: VARCHAR(200) should cover it!

longest line in vim?


For neovim users:

local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)

local width = #(lines[1])

for _, line in ipairs(lines) do
  if #line > width then
    width = #line
  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消