1

I'd like to know if a file uses LF or CRLF line endings. I'm on Windows with vim installed. I don't want to convert the files, just know which line endings they use.

I know I can get the file format (dos or unix) used by running :set ff?, bit I can't find a way to have the result printed on standard output. :print seem to only print range of lines, and :echo doesn't print anything on stdout.

I tried using scripted ex mode:

vim -e -s myfile < ../script.vim

with script.vim containing:

echo ff?
1p
quit

The output contains the first line of myfile, so the 1p command is executed, but I can't see the result echo ff?. What's the right command to output something?

2

Replace echo with set in your script, and your command works fine.

See :help -s-ex:

The output of these commands is displayed (to stdout):
:print
:list
:number
:set         to display option values.

(Emphasis mine.)

N.B. The following command also works*, and doesn't require the extra script.vim file:

vim -e -s -c "set ff?" -c "quit" myfile

For outputting the results of other commands to stdout, you may want to look into the :redir command, e.g.

vim -es -c 'redir >> /dev/stdout' -c 'smile' -c 'q'

However, note that getting this working in the cmd.exe Windows Command Prompt will probably require writing the output into the buffer first and then outputting it via :print.

Finally, I'm pretty sure you're already aware of this, but noting for future readers that the fileformat option doesn't necessarily tell you what line endings the file has, only how Vim is interpreting it: the value depends on your fileformats setting.

* I'd actually write it like this: vim -es -c "set fileformat | q" myfile

  • This is strange, set ff? is the first thing I tried, but a that time I had tried it with the syntax vim -s ../script.vim myfile, and in that mode :set ff? doesn't work as intended. – liberforce Jul 9 '18 at 12:31
  • One of my constraints is to have it run with vim on Windows. This works: vim -e -s -c "set ff?" -c "quit" myfile, i.e one must use double quotes. I couldn't get vim -e -s myfile < script.vim to work. – liberforce Jul 9 '18 at 12:37
  • 1
    @liberforce That's because writing the output of set commands to stdout is specifically a feature of silent mode. Reading from a script :h -s is an entirely different mode of operation, which does not have that feature. – Rich Jul 9 '18 at 12:38
  • @liberforce There's several different environments you can run Vim in on Windows. Are you running in the standard Windows Command Prompt cmd.exe? Is that what you were using when you wrote your question? I can't get your original command to work at all in cmd.exe, but if it's printing out output from 1p, I'd be surprised if it doesn't print out the output of :set. Might be worth reporting to the Vim developers if that's the case. – Rich Jul 9 '18 at 12:55
  • Yeah, I tried running both on Linux + bash (vim 7.4) and Windows + cmd.exe (vim 8.0). vim -e -s myfile < script.vim works on Linux but freezes on Windows. But the 1p was tested on Linux, on Windows I just can't get reading the script from stdin to work, so there may be something I'm doing wrong. – liberforce Jul 9 '18 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.