Vi: Line Too Long

vi: Line too long

(1) Apparently so :-)

I've never run into a limitation in vim (a) before but it may be that the vi shipping with HPUX is not vim.

(2) What sort of analysis are you doing on log files with vi?

This is the sort of task perfectly suited to text processing tools like sed, awk and Perl, in order of increasing awesomeness.


(a): You may want to consider grabbing vim if you don't have it already. From the vi_diff part of the documentation (differences between vi and vim):

Vim has only a few limits for the files that can be edited {Vi: can not handle characters and characters above 128, has limited line length, many other limits}.

Maximum line length: On machines with 16-bit ints (Amiga and MS-DOS real mode): 32767, otherwise 2147483647 characters. Longer lines are split.

Maximum number of lines: 2147483647 lines.

Maximum file size: 2147483647 bytes (2 Gbyte) when a long integer is 32 bits. Much more for 64 bit longs. Also limited by available disk space for the swap-file.

Line too long in 'vi'. Commands alternatives to navigate to the last lines of a file

Finally the solution using "tail" with arguments of the last lines to show.

With this command and "more" I can navigate to the last lines and jump over the line with the too-long-problem:

tail -1000 file-with-line-too-long.txt | more

It's a managed and limited machine without permissions to install any programs.

How to open a file having 50k lines in vi editor?

Since (according to the comments) vi tells you 'line too long', you will have to find a way to wrap the ultra-long line(s) in the file so that 'vi' can handle them. Or install vim; I've not noticed it have limits on line length.

How to wrap?

It might be simplest to write the wrapper yourself - there probably are tools built in to do the job, but you have to find the right one. This is a crude but effective pure filter written in C - it took me as long to write it as it took to type it:

#include <stdio.h>
#include <string.h>

int main(void)
{
char buffer[1024];

while (fgets(buffer, sizeof(buffer), stdin) != 0)
{
const char *endl = strchr(buffer, '\n') ? "" : "\n";
printf("%s%s", buffer, endl);
}
return 0;
}

It reads lines up to 1023 bytes long and spits those back out unchanged. Longer lines get split; as it reads the initial segments of the line, buffer has no newline at the end, so we artificially add one. This wraps ultra-long lines. Compile it as 'linesplitter'; use as:

linesplitter <build.log > wrapped.build.log

Clearly, you can enhance it to process lists of files or accept arguments to control the length at which lines split. If you want to get fancy, you can split at white space. There are a myriad tools that will probably also do the job; I know I have several of my own that do different variants of this task, written at various times since the mid-80s.

Perl?

If you prefer (you probably should), you can use Perl instead:

perl -p -e 'while (length($_) > 1024)
{ printf "%.1024s\n", $_; $_ = substr($_, 1024); }'

There might be a more compact way of writing that, but it works which is 90% of the battle. You can test it with:

perl -e 'print "abcdefgh" x 300, "\n"' | ...

That generates a line with 2400 bytes; the linesplitter program and the Perl script both spit out 3 lines of data, though there's a difference in the outputs because one limits the lines to 1023 and the other to 1024 bytes (plus newline).

Others?

What can be done in Perl can be done in Python. You might be able to use awk (at least nawk). You might be able to use sed. You might be able to use pr. There might be a program fmt.

What to use?

Depending on your skill level with the different tools, I suggest that the Perl is quite adequate for the job. The C is close to trivial too if you're familiar with compiling C programs; I do that all day so it is the way my mind tends to work.

Note that the solution is unequivocally working around a limitation in vi diagnosed by vi. I'm 99.9% sure it will handle 1 KiB lines with ease; it might well handle quite a bit longer lines. You choose the number to split on. And I've not had to reformat even fairly ghastly build logs using vim, so maybe you should install that?

Vim hides lines that are too long to be displayed completely

If I understand your problem correctly, setting :set display=lastline will help.

    Change the way text is displayed.  This is comma separated list of
flags:
lastline When included, as much as possible of the last line
in a window will be displayed. "@@@" is put in the
last columns of the last screen line to indicate the
rest of the line is not displayed.
truncate Like "lastline", but "@@@" is displayed in the first
column of the last screen line. Overrules "lastline".
uhex Show unprintable characters hexadecimal as <xx>
instead of using ^C and ~C.

When neither "lastline" nor "truncate" is included, a last line that
doesn't fit is replaced with "@" lines.

You can also try :set nowrap.

line too long error when trying to grep log files

You should try

find -type f -exec grep PATTERN {} +

The command line limitation can be found on linux with

$ getconf ARG_MAX

On *BSD with

$ sysctl kern.argmax

See http://www.cyberciti.biz/faq/argument-list-too-long-error-solution/ to go further.

cygwin: line numbers in vi or vim have too much space in front, how is it removed?

In vim, you can influence the width of the number column by the 'numberwidth' setting. You can't however have a smaller width, that the largest number would be wide (e.g. you can't set the width to 3 while you have more than 999 lines.)

Read the details at :h 'numberwidth'

In VIM, how do I break one really long line into multiple lines?

Vim does this very easy (break lines at word boundaries).

gq{motion} % format the line that {motion} moves over
{Visual}gq % format the visually selected area
gqq % format the current line
...

I'd suggest you check out :help gq and :help gw.

Also setting textwidth (tw) will give you auto line break when exceeded during typing. It is used in gq too, though if disabled gq breaks on window size or 79 depending on which comes first.

:set tw=80

By setting format options to include text width vim will automatically break at the tw setting.

:set fo+=t


Related Topics



Leave a reply



Submit