最长行在vim? [英] longest line in vim?

查看:201
本文介绍了最长行在vim?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有命令来确定vim中最长行的长度?

Is there a command to determine length of a longest line in vim? And to append that length at the beginning of the file?

推荐答案

Gnu的wc命令有一个-L --max-line-长度选项,打印出文件的最大行长度。请参阅 gnu man wc 。 freebsd wc也有-L,但不是-max-line-length,参见 freebsd man wc

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.

如何从vim使用这些?命令:

How to use these from vim? The command:

:%!wc -L

将通过wc -L过滤打开的文件,并使文件的内容为最大行长。

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

保留文件内容并将最大行长放在第一行do:

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

:%yank
:%!wc -L
:put

而不是使用wc,查找最长行的长度 - awk bash 介绍如何使用awk找到最长行的长度。

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

好,现在是一个纯Vim解决方案。我有点新的脚本,但这里。以下内容基于 textfilter 的FilterLongestLineLength函数。

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()

将此代码放在.vim文件(或.vimrc)中,然后:source文件。然后使用新命令:

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.

这篇关于最长行在vim?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆