How to disable editing my history in bash

前端 未结 4 1449
独厮守ぢ
独厮守ぢ 2020-12-31 02:17

In bash, when I go back in history, edit some command and run it, this edited command is appended to history and the original one is left intact. But every once in a while I

相关标签:
4条回答
  • 2020-12-31 02:48

    I somehow manage to affect the original command, i.e. my edit replaces the original command back in history.

    Right. If you go back in your history and edit the line without pressing return to execute the command but instead moving to another history entry, you've just edited the history entry. If you then list your history, you will see a * on the line indicating that you edited it. I find this "feature" immensely frustrating. Others have provided good examples of how to reproduce this.

    My goal is to avoid this, so any edit to a previous command always gets appended to history and never replaces the original.

    I too wanted to disable it. I found the solution via this answer over on unix.stackexchange.

    To summarize, you need to enable the revert-all-at-newline readline setting which is off by default. If the setting is on then bash will revert any changes you made to your history when you execute the next command.

    To enable this setting in your shell, you should add the following to your ~/.inputrc file and then restart your shell:

    $include /etc/inputrc
    set revert-all-at-newline on
    

    The first line is needed because I guess that if you supply your own .inputrc file the default /etc/inputrc file is not included which is probably not what you want.

    0 讨论(0)
  • 2020-12-31 02:53

    What do

    echo $HISTCONTROL
    echo $HISTIGNORE
    

    give you?

    Edit:

    I was able to reproduce behavior similar to what you've seen by following these steps:

    • At the shell prompt, enter:

      echo abcd
      echo efgh

    • Press up arrow twice, so "echo abcd" is shown

    • Press 1 to add that character at the end

    • Press escape to enter command mode

    • Press left arrow twice so the cursor is on the "c"

    • Press x to delete the "c"

    • Press enter

    Now as you step back through history, you'll see a new entry at the end:

    echo abd1
    

    and the entry that previously had "echo abcd" will now look like this:

    echo abcd1
    

    This is one way, I'm sure there are others.

    0 讨论(0)
  • 2020-12-31 03:04

    If you go back to some previous command and edit it, but then DON'T execute it (instead using history commands to go to some other command and execute it), then the edits will remain there in your history list.

    Pressing Ctrl + C, after editing, counters this behaviour. It leaves the original command in tact i.e. it cancels remembering edits to the original.

    0 讨论(0)
  • 2020-12-31 03:06

    Here's my own answer, please correct or provide more details if you can.

    When the "vi" option is set in bash ("set -o vi" -- "Use a vi-style command line editing interface"), there are two modes of editing a command from history.

    The first mode (let's call it "basic") is when you start editing immediately using Backspace, Del and character keys.

    The other mode is the "vi mode", entered when you hit Esc.

    If you want to keep your history intact, DO NOT use both modes in the same edit. I don't know how bash works exactly, but you can think of it this way:

    1. Entering the "vi mode" applies any changes done in "basic mode" to the original command, and creates a copy of the command that you can edit further using vi-style commands.
    2. The changes get applied when you hit Enter (execute), Up, Down or j,k (move to another command in history).
    3. The changes do not get applied if you hit Ctrl-C.
    4. Using either basic or vi-style editing ALONE does not affect the original command in history.
    0 讨论(0)
提交回复
热议问题