Why Would Vim Create a New File Every Time You Save a File

Why would vim create a new file every time you save a file?

It is trying to protect you from disk and os problems. It writes out a complete copy of the file, and when it is satisfied this has finished properly, renames this file to the required filename. Hence, new inode number.

If there were a crash during the save process, the original file would remain untouched, possibly saving you from losing the file completely.

Why does VIM create a X~ file for every X?

its a backup file, every time you save while modifying something it will create one. There's an option to turn it off.

See here from the vim faq

How to stop Vim from creating/opening a new file?

You could add this function to your .bashrc (or equivalent). It checks that its command-line arguments exist before it invokes vim. If you really want to create a new file you can pass --new to override the checks.

vim() {
local args=("$@")
local new=0

# Check for `--new'.
for ((i = 0; i < ${#args[@]}; ++i)); do
if [[ ${args[$i]} = --new ]]; then
new=1
unset args[$i] # Don't pass `--new' to vim.
fi
done

if ! (( new )); then
for file in "${args[@]}"; do
[[ $file = -* ]] && continue # Ignore options.

if ! [[ -e $file ]]; then
printf '%s: cannot access %s: No such file or directory\n' "$FUNCNAME" "$file" >&2
return 1
fi
done
fi

# Use `command' to invoke the vim binary rather than this function.
command "$FUNCNAME" "${args[@]}"
}

vim changes modification time of folder on saving file in it under windows 7

It's probably the creation and deletion of the backup file.

vim creates a backup before writing the new version of the file, so you can get the old version back in case of a crash.

You can turn off backups with :set nobackup nowritebackup or change the location with :set backupdir=someplaceelse

The backupcopy option may also have some effect. It can try to rename the new file into place. Try yes and no for backupcopy and see if there is a difference. But note there are other side effects... see :help backupcopy. And the behavior may depend on whether your backupdir is on the same filesystem.

How to save a file with a new name in VIM while switching to that new buffer (and closing the original)

The sav command should do what you want.

Reference:

:sav[eas][!] [++opt] {file}

Save the current buffer under the name {file} and set the filename
of the current buffer to {file}.

The previous name is used for the alternate file name. The [!] is
needed to overwrite an existing file. When 'filetype' is empty
filetype detection is done with the new name, before the file is
written. When the write was successful 'readonly' is reset.

Save new file in vim where filename is stored in a register?

Vimscript is evaluated exactly like the Ex commands typed in the : command-line. There were no variables in ex, so there's no way to specify them. When typing a command interactively, you'd probably use <C-R> to insert variable or register contents:

:write <C-R>a<CR>

... but in a script, :execute must be used. All the literal parts of the Ex command must be quoted (single or double quotes), and then concatenated with the variables; for registers, use the special @ sigil:

execute 'write' @a

To handle special characters in the filename, add this:

execute 'write' fnameescape(@a)

Alternative

If each line corresponds to a single file and its contents (and you have many such lines), creating a new buffer and saving it (in a macro) will take quite some time. You can alternatively use the low-level writefile() function and skip the buffer creation:

:global/^/ let [filename, content] = matchlist(getline('.'), '\(\S\+\)\s\(.*\)')[1:2] | call writefile([content], filename)

Does it have to be Vim?

Also, such a task might be easier to perform in a scripting language (often as a one-liner), e.g. in Python or Perl.



Related Topics



Leave a reply



Submit