annoying difference between vim and notepad

51 views
Skip to first unread message

drlatex

unread,
Aug 23, 2009, 7:52:00 PM8/23/09
to vim...@googlegroups.com

Hello,

I have a set of files and I applied the command %j! to all of them, to put
all contents into one line for each file.

When I open the resulting files in vim, as expected, the file is 1 line long
(although millions of characters). And the normal mode command G doesn't do
anything because the file is one line long.

However, when I open the exact same file in windows notepad, all the
contents are on one line, EXCEPT that there's a \n at the end of the string
of characters (there's a 2nd line that is empty... and I have to press
"CTRL+end" and "backspace" to remove this last line).

The annoying things is that matlab follows notepad's convention (not that of
vim)....
When I read the number of characters in the string, it INCLUDES the \n
that's present at the end of the notepad files, even though when I open
these files in vim and give the command /\n I get no results.

So I have to open EACH file in notepad and type "CTRL+end" and "backspace"
to remove this surplus \n at the end of each file, but there's no automated
way of doing thing (like in vim I could just use argdo).

So my question is, why does vim not have the \n at the end after pressing
%j! , while matlab and notepad seem to thing there IS a \n at the end ?? And
is there a way to modify the files with vim so that matlab and notepad don't
add this \n to the end ??



--
View this message in context: http://www.nabble.com/annoying-difference-between-vim-and-notepad-tp25108663p25108663.html
Sent from the Vim - General mailing list archive at Nabble.com.

Marc Weber

unread,
Aug 23, 2009, 8:17:05 PM8/23/09
to vim_use
Excerpts from drlatex's message of Mon Aug 24 01:52:00 +0200 2009:

> So my question is, why does vim not have the \n at the end after pressing
> %j! , while matlab and notepad seem to thing there IS a \n at the end ?? And
> is there a way to modify the files with vim so that matlab and notepad don't
> add this \n to the end ??

:h binary and :h eol

To get no "\n" at the end of file (which is your end of line here)
:setlocal binary | setlocal noeol | w

If you want to do batch processing maybe readfile(..) and writefile(..)
in binary mode could do what you want as well.

So maybe this joins lines removing without adding a trailing "\n" as
well ? (untested)
:call writefile(join(readfile("file",1), "file.out","b"))

Even ifI 've missunderstood your goal this should help you solving your problem.

HTH
Marc W.

Vlad Dogaru

unread,
Aug 23, 2009, 8:24:46 PM8/23/09
to vim...@googlegroups.com
On Sun, 23 Aug 2009 16:52:00 -0700 (PDT)
drlatex <n_da...@hotmail.com> wrote:

> So my question is, why does vim not have the \n at the end after
> pressing %j! , while matlab and notepad seem to thing there IS a \n
> at the end ?? And is there a way to modify the files with vim so that
> matlab and notepad don't add this \n to the end ??

Hi,

what I can make from this (might be wrong):

vim automatically adds a newline at the end of any file. In Linux, de
end-of-line is '\n'. In Windows, it's '\r\n', and Notepad sees a
sole '\n' as junk.

You can either set Vim to Windows style EOLs, with :set ff=dos, or ask
vim not to append a newline to the final (and single, in your case)
line, with :set binary.

Hope this helps,
Vlad

pansz

unread,
Aug 23, 2009, 11:23:30 PM8/23/09
to vim...@googlegroups.com
drlatex 写道:

>
> However, when I open the exact same file in windows notepad, all the
> contents are on one line, EXCEPT that there's a \n at the end of the string
> of characters (there's a 2nd line that is empty... and I have to press
> "CTRL+end" and "backspace" to remove this last line).
>

Well, it seems no problem, just different program treat the line
differently.

For notepad and many windows-based programs: \n is not part of a line,
they treat \n as the separator between two lines, so if the last
character of a text file is \n, it will think the last line is an empty
line. (but there will not be an empty line in Vim)

For Vim and most unix-based programs: \n is not a separator, it is part
of a line, all lines must end with \n, so the last character of a text
file will always be \n. (In the whole Linux world, text file without \n
is an invalid or corrupted text file, a C program without an \n ending
may be an error in gcc)


This is the trend, and I think the unix way is actually better. Think if
you need to concatenate two text files? In windows-way you need to
append \n to the first file then do the concatenation, in unix-way you
only need to concatenate two text file.

So, all you can do is: leave the blank line there.

drlatex

unread,
Aug 24, 2009, 5:12:21 PM8/24/09
to vim...@googlegroups.com

Thank you Marc and Vlad for your suggestions and comments.
Also thanks to pansz for commenting, although 'leaving the blank line' is
not an option for me.

I'm glad I was able to learn about how to set files to binary and dos
format, although I was able to resolve the problem by using the unix command
tr -d '\n' <originalfilename.txt >newfilename.txt,
which removes all occurences of \n whether or not it's at the end.

Thank you all once again for your time and help!
--
View this message in context: http://www.nabble.com/annoying-difference-between-vim-and-notepad-tp25108663p25123662.html

Reply all
Reply to author
Forward
0 new messages