White space at the end of a line

  • Thread starter Spiros Bousbouras
  • Start date
S

Spiros Bousbouras

I'm thinking of adding a command to vim for removing
white space from the end of each line of a C source
file. Can anyone think of a situation where such white
space might be useful ?
 
V

vippstar

I'm thinking of adding a command to vim for removing
white space from the end of each line of a C source
file. Can anyone think of a situation where such white
space might be useful ?

//\<no-space>
printf("Never called!\n");
//\<space>
printf("Called!\n");
 
R

Richard Tobin

I'm thinking of adding a command to vim for removing
white space from the end of each line of a C source
file. Can anyone think of a situation where such white
space might be useful ?
[/QUOTE]
//\<no-space>
printf("Never called!\n");
//\<space>
printf("Called!\n");

He said "useful", not "obfuscating"!

-- Richard
 
S

Spiros Bousbouras

//\<no-space>
printf("Never called!\n");
//\<space>
printf("Called!\n");

I wouldn't call a construct like

//\<no-space>
printf("Never called!\n");

useful. But thanks for the suggestion.
 
R

Richard Harter

I'm thinking of adding a command to vim for removing
white space from the end of each line of a C source
file. Can anyone think of a situation where such white
space might be useful ?

Consider

#define foo \<space>
#define foo \<no_space>

The behaviours are different.



Richard Harter, (e-mail address removed)
http://home.tiac.net/~cri, http://www.varinoma.com
In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die
 
K

Keith Thompson

Spiros Bousbouras said:
I'm thinking of adding a command to vim for removing
white space from the end of each line of a C source
file. Can anyone think of a situation where such white
space might be useful ?

As others have said, the example presented by vippstar isn't what I'd
call *useful*, except perhaps to determine which programmer to fire.

Before I saw that example, I would have guessed that there's no case
where trailing white space is significant. One exception, sort of, is
that a line-joining \ can't be followed by white space (which is a
dumb rule IMHO), but in that case the white space makes the program
illegal.

However, depending on what text editor you use, it might be convenient
to have blank lines indented by the same amount as the non-blank lines
surrounding them. For example (end-of-line marked with '$'):

int x;$
$
x = 42;$

vs.

int x;$
$
x = 42;$

It makes no difference to the compiler, of course,
 
C

CBFalconer

Spiros said:
I'm thinking of adding a command to vim for removing white space
from the end of each line of a C source file. Can anyone think of
a situation where such white space might be useful ?

Yes.
 
D

Duncan Muirhead

I'm thinking of adding a command to vim for removing
white space from the end of each line of a C source
file. Can anyone think of a situation where such white
space might be useful ?
I can't, but of course a danger with such file modifications
is that (some) source control systems/patchers might be fooled into
thinking you had made many changes, for which others working
on the same code might not thank you.
 
C

CBFalconer

Duncan said:
I can't, but of course a danger with such file modifications is
that (some) source control systems/patchers might be fooled into
thinking you had made many changes, for which others working on
the same code might not thank you.

I am not familiar with vim, but thi can probably be done without
any alterations. Just use a macro.
 
J

Joachim Schmitz

CBFalconer said:
I am not familiar with vim, but thi can probably be done without
any alterations. Just use a macro.
Please expand on that, how could a macro (assuming you talk #define here)
remove trailing whitespace from some source file?
And how could it be added without modifying that source file?

Bye, Jojo
 
V

vippstar

Please expand on that, how could a macro (assuming you talk #define here)
remove trailing whitespace from some source file?
And how could it be added without modifying that source file?

Bye, Jojo

You assumed wrong.
Yes, it's OT to talk about vim macros.
Theres a thin line between pedantry and stupidity.
 
R

Roberto Waltman

Spiros said:
I'm thinking of adding a command to vim for removing
white space from the end of each line of a C source
file.

What is wrong with :%s/[\t ][\t ]*$// ? Just define it as a macro.
Can anyone think of a situation where such white
space might be useful ?

No.

(I do this all the time when comparing different
versions of a file to avoid "false positives")

Roberto Waltman

[ Please reply to the group,
return address is invalid ]
 
C

Chris Torek

I'm thinking of adding a command to vim for removing
white space from the end of each line of a C source
file. Can anyone think of a situation where such white
space might be useful ?

As several people note, this requires defining "useful".

I will add, here, that attempting to make use of "invisible" trailing
white space in C programs, e.g., by writing a slash-slash comment
(in C99) followed by a backslash followed by a blank followed by
a newline, so that the line following this line appears to be
commented out but actually is not, is a bad idea. This is the
case not only because it is obfuscatory, but also because it may
malfunction on some systems.

There are still some (now rare) file systems Out There that are
unable to distinguish between an "empty" line and a "line consisting
only of blanks". In particular, they see a file as a set of
"records" of fixed length. A line shorter than the fixed record
length is blank-padded, and a line exactly the fixed record length
is left alone. (It is physically impossible to have a line longer
than the record length.) In other words, all lines are exactly
the same length, hence the name "fixed length records".

If a C compiler on such a system reads such files, it will[%] remove
all "trailing blanks" from the file itself, as if you (the OP above)
had used your vim command. So an attempt to use C99 comments to
*pretend* to comment out the next line, on such a compiler, actually
*will* comment out the next line. (One can thus use the attempt
to discover whether the compiler is reading fixed-length records
and stripping trailing whitespace from them.)

[% Actually, it could also leave them in, giving surprising behavior
for what the programmer *thought* were trailing backslashes. The
systems I have heard of do delete trailing white-space, though, to
avoid these surprises.]
 
S

Spiros Bousbouras

Spiros said:
I'm thinking of adding a command to vim for removing
white space from the end of each line of a C source
file.

What is wrong with :%s/[\t ][\t ]*$// ? Just define it as a macro.

Nothing. It is one possibility but I'm thinking
of something more elaborate perhaps. Whether it
does anything will depend on whether a boolean
option is set, possibly it will give a warning
for each modified line and to take into account
what Keith Thompson said possibly it will leave
alone lines containing only white space.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top