About: Automatically reload files with mixed line-endings in DOS fileformat   Sponge Permalink

An Entity of Type : owl:Thing, within Data Space : 134.155.108.49:8890 associated with source dataset(s)

This autocmd in your vimrc will do just that. If it finds any occurrences of a carriage return at the end of a line, and the file was read in Unix format, it will automatically re-read the file in DOS format. This will automatically correct the issue for good when you save the file, but it is most useful for times when you cannot save the file for some reason (maybe it is in a code module that is "locked down" for example). autocmd BufReadPost * nested \ if !exists('b:reload_dos') && !&binary && &ff=='unix' && (0 < search('$', 'nc')) | \ let b:reload_dos = 1 | \ e ++ff=dos | \ endif

AttributesValues
rdfs:label
  • Automatically reload files with mixed line-endings in DOS fileformat
rdfs:comment
  • This autocmd in your vimrc will do just that. If it finds any occurrences of a carriage return at the end of a line, and the file was read in Unix format, it will automatically re-read the file in DOS format. This will automatically correct the issue for good when you save the file, but it is most useful for times when you cannot save the file for some reason (maybe it is in a code module that is "locked down" for example). autocmd BufReadPost * nested \ if !exists('b:reload_dos') && !&binary && &ff=='unix' && (0 < search('$', 'nc')) | \ let b:reload_dos = 1 | \ e ++ff=dos | \ endif
Version
  • 7(xsd:integer)
dbkwik:vim/property/wikiPageUsesTemplate
Previous
  • 1661(xsd:integer)
Category
  • Fileformat
Author
  • Fritzophrenic
Subpage
  • /201010
Complexity
  • basic
Created
  • 2010-10-14(xsd:date)
ID
  • 1662(xsd:integer)
NEXT
  • 1663(xsd:integer)
abstract
  • This autocmd in your vimrc will do just that. If it finds any occurrences of a carriage return at the end of a line, and the file was read in Unix format, it will automatically re-read the file in DOS format. This will automatically correct the issue for good when you save the file, but it is most useful for times when you cannot save the file for some reason (maybe it is in a code module that is "locked down" for example). autocmd BufReadPost * nested \ if !exists('b:reload_dos') && !&binary && &ff=='unix' && (0 < search('$', 'nc')) | \ let b:reload_dos = 1 | \ e ++ff=dos | \ endif Most of this is self-explanatory, but there are a couple tricks to it: * The 'n' flag is used on the search, so that the cursor does not move. * The autocmd is nested. This allows other autocmds, like those that load syntax highlighting, etc., to also fire on the reload. * Because the autocmd is nested, it will itself fire again on the reload. Thus, we use the b:reload_dos variable to detect when it has already fired so we do not keep firing recursively. This can be improved a bit. It might be nice to have a warning of some sort when this auto-reload occurs. Or maybe you want to change as few lines as possible, so you can manually convert the fileformat to the one that changes the fewest lines (for example, if only 2 lines out of 12000 have DOS-style endings, why convert the whole file to DOS?). Also, the detection of DOS-format endings uses a search, which could take a very long time on very large files. The following version (when possible) limits the length of time allowed for the search before giving up, and additionally gives a message to the user by redirecting the output of an :s command with the 'n' flag to count matches without making changes and parsing it to output a message like, "File has 2 DOS-style lined endings out of 12000 lines." " When loading a file, if it reads in as Unix, but has a DOS line ending, " and is not in binary mode, reload it in DOS format. Do this AFTER loading " last known position to prevent always opening on last line. " " Time out the search after 1/10 second. Timeouts only available in 7.1.211 " and up, so just risk long loads in earlier versions. if (v:version > 701 || v:version == 701 && has("patch211")) autocmd BufReadPost * nested \ if !exists('b:reload_dos') && !&binary && &ff=='unix' && (0 < search('$', 'nc', 0, 100)) | \ let b:reload_dos = 1 | \ redir => s:num_dos_ends | \ silent %s#$##n | \ redir END | \ e ++ff=dos | \ echomsg "File has ". \ substitute(s:num_dos_ends, '^\_.\{-}\(\d\+\).*', '\1', ''). \ " DOS-style line endings out of ".line('$')." lines." | \ endif else autocmd BufReadPost * nested \ if !exists('b:reload_dos') && !&binary && &ff=='unix' && (0 < search('$', 'nc')) | \ let b:reload_dos = 1 | \ e ++ff=dos | \ endif endif autocmd BufReadPost * if exists('b:reload_dos') | unlet b:reload_dos | endif
Alternative Linked Data Views: ODE     Raw Data in: CXML | CSV | RDF ( N-Triples N3/Turtle JSON XML ) | OData ( Atom JSON ) | Microdata ( JSON HTML) | JSON-LD    About   
This material is Open Knowledge   W3C Semantic Web Technology [RDF Data] Valid XHTML + RDFa
OpenLink Virtuoso version 07.20.3217, on Linux (x86_64-pc-linux-gnu), Standard Edition
Data on this page belongs to its respective rights holders.
Virtuoso Faceted Browser Copyright © 2009-2012 OpenLink Software