6

I am using GNU Indent to format C code in my project.

By default backup files are created ending with a ~.

I don't want to have any backup files created, is there a way to disable it?

jasonwryan
  • 71,734
  • 34
  • 193
  • 226
ivokosir
  • 79
  • 3

4 Answers4

8

Use the source, which is in Mercurial. A comment in backup.c says

 * Finally, if VERSION_CONTROL is "none" or "never", backups are not
 * made.  I suggest you avoid this behaviour.

The allowed values for VERSION_CONTROL are listed in the same file:

{
    {none,              "never"},    /*!< Don't make backups. */
    {none,              "none"},     /*!< Ditto */
    {simple,            "simple"},   /*!< Only simple backups */
    {numbered_existing, "existing"}, /*!< Numbered if they already exist */
    {numbered_existing, "nil"},      /*!< Ditto */
    {numbered,          "numbered"}, /*!< Numbered backups */
    {numbered,          "t"},        /*!< Ditto */
    {unknown,           0}           /*!< Initial, undefined value. */
};

The same information is in the NEWS file, indicating that it dates to version 1.3 (late 1990s), so it probably is available on your system.

It is not in the indent manual:

The type of backup file made is controlled by the value of the environment variable VERSION_CONTROL. If it is the string ‘simple’, then only simple backups will be made. If its value is the string ‘numbered’, then numbered backups will be made. If its value is ‘numbered-existing’, then numbered backups will be made if there already exist numbered backups for the file being indented; otherwise, a simple backup is made. If VERSION_CONTROL is not set, then indent assumes the behaviour of ‘numbered-existing’.

For what it's worth, I tested GNU indent 2.2.11 (it works).

Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268
  • 1
    Thanks! This should be the accepted answer, as it allows exactly what the OP asks for. – Popup Mar 29 '19 at 15:02
5

Looking through the man page for indent and the official GNU documentation I only see 2 methods for controlling this behavior.

The environment variables:

  • SIMPLE_BACKUP_SUFFIX
  • VERSION_WIDTH

I tried various tricks of setting the width to 0 and also setting the SIMPLE_BACKUP_WIDTH to nothing (""). Neither had the desired effect. I think you're only course of action would be to create a shell alias and/or function to wrap the command indent to do what you want.

Example

$ function myindent() { indent "$@"; rm "$@"~; }

Then when I run it:

$ myindent ev_epoll.c

I get the desired effect:

$ ls -l | grep ev_epo
-rw-r--r--.   1 saml saml     7525 Dec 13 18:07 ev_epoll.c
slm
  • 363,520
  • 117
  • 767
  • 871
  • Thanks, i did something similar, but i used `$*` and for loop instead of `$@` so i can have multiple files. I still think it would be better if there would be something simple like `-noback` option to disable backups. – ivokosir Dec 14 '13 at 10:39
  • @user54552 - yeah I was very surprised that there was no option. The code for that program can't be overly complex, you could take it and probably add that switch yourself and submit it back as a patch. LMK if you're interested I can help you find the code repo if you're looking for it. – slm Dec 14 '13 at 12:51
0

Using mktemp:

a=$(mktemp)
indent -o "$a" file
mv "$a" file

Using Vim in Ex mode:

ex -sc '%!indent -st' -cx file

Example

Zombo
  • 1
  • 5
  • 43
  • 62
0

To make explicit the answer from Thomas Dickey,

declare -x VERSION_CONTROL=none 

works good for me.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
  • 1
    It may also be useful to mention that instead of running that line which makes a change to behavior for the remainder of the session, they may want to just prefix their single calls to Indent ( ie "VERSION_CONTROL=none indent -flags and arguments"), as well. – Jason Rush Dec 05 '17 at 21:20