0

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!

Myx
  • 1,792
  • 5
  • 23
  • 37
  • I'm confused... are you looking for a way for VIM to insert the missing ""s for you? – Billy ONeal Apr 30 '10 at 18:35
  • So in gedit, for example, if I write the above line, I don't get any compile errors because it considers that what is on the next line is just a continuation of the previous line and it counts the end of my expression when it sees ";". With vim, I get an error when I have the above line, where I have pressed ENTER between `on` and `line`. So what do I have to do in vim so that the compiler understands that the two lines are 1 statement and not 2? – Myx Apr 30 '10 at 18:39
  • Effectively the same question as http://stackoverflow.com/questions/1752079/in-c-can-a-long-printf-statement-be-broken-up-into-multiple-lines (this one is C++ instead of C, but the answer is the same) – James McNellis Apr 30 '10 at 18:40
  • @Myx: Clearly there's a difference between the actual text you're creating with those two editors. I'm not sure what... maybe you're using windows/dos newlines in one but not the other? But you need to figure out what they're doing differently. Maybe try diffing the files created in each. – Cascabel Apr 30 '10 at 18:41
  • @Myx: So what you really want is wordwrap in VIM, correct? – Billy ONeal Apr 30 '10 at 18:42
  • @James McNellis: From the OP's claim that the same text produced in different editors gives different results, I think there's something going on here besides needing to concatenate string literals. – Cascabel Apr 30 '10 at 18:43
  • So the answers below solved my problems. I'm used to coding in gedit and I didn't run into this problem when using gedit so I thought it was an editor problem not a simple C syntax problem. Maybe gedit does something so that I don't have to end the line with " and begin the next line with "... – Myx Apr 30 '10 at 19:45

4 Answers4

5

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
  • 17,383
  • 5
  • 46
  • 62
2

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
  • 14,226
  • 3
  • 42
  • 60
1

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

cs80
  • 257
  • 1
  • 3
  • Try it: #include int main() { printf("Hello \ world\n"); } – cs80 Apr 30 '10 at 18:47
  • 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 Apr 30 '10 at 18:47
  • 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 Apr 30 '10 at 18:57
  • I agree with Kevin & Hudson that the trailing \ is not as good as using separate string literals, especially for readability, but it does allow you to continue a long line. – cs80 Apr 30 '10 at 19:00
1

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.

Cascabel
  • 479,068
  • 72
  • 370
  • 318