views:

127

answers:

4

Hello:

How can I break long lines when writing c++ code in vim? For example, if I have something like

56 fprintf(stderr, "Syntax error reading recursion value on 
57                line %d in file %s\n", line_count, filename);

I get the following compile errors:

:56:25: warning: missing terminating " character
:56: error: missing terminating " character
:57: error: stray ‘\’ in program
:57:37: warning: missing terminating " character
:57: error: missing terminating " character

I'm a vim newbie.

Thanks!

+5  A: 

That's not a Vim problem, that's a C problem.

Put quotes at the end of one line and the start of the other. Maybe you're looking for this:

fprintf(stderr, "Syntax error reading recursion value on "
                "line %d in file %s\n", line_count, filename);

...and if you want to know how to turn one-long-line into two, if you're splitting mid-string, go to where you want to split and then type 'i' followed by quote-enter-quote. Vim will follow your cindent rules when aligning the second line.

Alternatively, maybe it's a view problem? If you have a linebreak in there, it'll give you a compile error. However, in vim it is possible to have it appear to break the line, put set wrap and set lbr in your vimrc file. Check out :help lbr for info. There's also a way to configure the "leader" on the line, so you know it's a view-only linebreak.

dash-tom-bang
+2  A: 

my advice would be to not break the string -

instead do

    fprintf (stderr,  
             "Syntax error reading recursion value on line %d in file %s\n", 
             line_count, 
             filename);
KevinDTimm
+1  A: 

Put a trailing \ on the end of the line you want to continue.

cs80
I don't believe that works inside string literals.
Billy ONeal
Try it:#include <stdio.h>int main() { printf("Hello \world\n");}
cs80
it does work, but you have to be careful of your spacing as everything on the new line (after '\') is part of the string.
KevinDTimm
While it does work and is standards compliant, the problem is that it will also add all of the whitespace that is used to indent the second portion of the line. The string literal concatenation approach is probably the best solution in this case.
Hudson
cs80
+1  A: 

Like Billy ONeal, I'm a bit confused why you're asking this as a Vim question. The code you need to write is:

fprintf(stderr, "Syntax error reading recursion value on "
                "line %d in file %s\n", line_count, filename);

Note that there's no comma - when you remove the extra whitespace, that's just two string literals together. They'll be combined into one, which I believe is exactly what you want.

Jefromi
you'll want to edit your response :) look carefully
KevinDTimm
@KevinDTimm: Was that about the missing space that I already caught?
Jefromi
Yes, it was....
KevinDTimm