Converts line endings of text files from CRLF (\r\n) to LF (\n) or the reverse.

convert_line_endings(
  files,
  direction = c("to_lf", "to_crlf"),
  root = NULL,
  force = FALSE,
  silent = TRUE
)

Arguments

files

Character vector of file paths; these may be relative to root.

direction

Whether conversion is to LF ("to_lf") or to CLRF ("to_crlf").

root

An optional directory path; if present the files argument is considered relative to root.

force

Logical; TRUE will only have effect in non-Windows systems. See Details.

silent

Logical. Whether to print the return value to the console.

Value

The files argument; invisibly unless silent = FALSE.

Details

The conversion to LF is especially helpful to maintain file integrity across platforms in combination with a distributed version control system like git.

To prevent unneeded rewriting, the "to_lf" direction is only executed in Windows systems, unless force = TRUE.

The function is designed so that it can take the output of git2rdata::write_vc() as input in a pipeline; still the root argument may need to be repeated in such case.

Note

The function borrows from TAF::dos2unix() and TAF::unix2dos(), which work on single files.

Git configuration to prevent auto-replacement of line endings

To prevent git from automatically replacing line endings in specific files when checking out new versions of those files, set LF as the required line ending of specific file types in a .gitattributes file in the root of the git repository, and commit this file so that collaborators use the same setting. An example line in .gitattributes looks like this (this example makes line endings of yml files always use LF):

*.yml text eol=lf

Note that, with this setting, to have existing files updated by git they first need to be removed from the working directory, after which you can do a checkout again for these files.

See also

Other functions regarding file management for N2KHAB projects: checksum(), download_zenodo(), fileman_folders(), fileman_up(), get_zenodo_versions(), locate_n2khab_data()

Examples

if (FALSE) {
files <- c(file1, file2)
convert_line_endings(files)
}