mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-12 21:18:53 +00:00
don't import bash history if not default fish hist
Don't import the bash history if the user has specified that a non-default fish history file should be used. Also, rename the var that specifies the fish history session ID from `FISH_HISTFILE` to `FISH_HISTORY`. Fixes #4172
This commit is contained in:
parent
2cc0107dbf
commit
ec14527545
5 changed files with 21 additions and 16 deletions
|
@ -64,11 +64,11 @@ history delete --prefix "foo"
|
|||
|
||||
By default interactive commands are logged to `$XDG_DATA_HOME/fish/fish_history` (typically `~/.local/share/fish/fish_history`).
|
||||
|
||||
You can set the `FISH_HISTFILE` variable to another name for the current shell session. The default value (when the variable is unset) is `fish` which corresponds to `$XDG_DATA_HOME/fish/fish_history`. If you set it to e.g. `fun`, the history would be written to `$XDG_DATA_HOME/fish/fun_history`. An empty string means history will not be stored at all. This is similar to the private session features in web browsers.
|
||||
You can set the `FISH_HISTORY` variable to another name for the current shell session. The default value (when the variable is unset) is `fish` which corresponds to `$XDG_DATA_HOME/fish/fish_history`. If you set it to e.g. `fun`, the history would be written to `$XDG_DATA_HOME/fish/fun_history`. An empty string means history will not be stored at all. This is similar to the private session features in web browsers.
|
||||
|
||||
You can change `FISH_HISTFILE` at any time (by using `set -x FISH_HISTFILE "session_name"`) and it will take effect right away. If you set it to `"default"`, it will use the default session name (which is `"fish"`).
|
||||
You can change `FISH_HISTORY` at any time (by using `set -x FISH_HISTORY "session_name"`) and it will take effect right away. If you set it to `"default"`, it will use the default session name (which is `"fish"`).
|
||||
|
||||
Other shells such as bash and zsh use a variable named `HISTFILE` for a similar purpose. Fish uses a different name to avoid conflicts and signal that the behavior is different (session name instead of a file path).
|
||||
Other shells such as bash and zsh use a variable named `HISTFILE` for a similar purpose. Fish uses a different name to avoid conflicts and signal that the behavior is different (session name instead of a file path). Also, if you set the var to anything other than `fish` or `default` it will inhibit importing the bash history. That's because the most common use case for this feature is to avoid leaking private or sensitive history when giving a presentation.
|
||||
|
||||
\subsection history-notes Notes
|
||||
|
||||
|
|
|
@ -846,7 +846,7 @@ The user can change the settings of `fish` by changing the values of certain var
|
|||
|
||||
- `history`, an array containing the last commands that were entered.
|
||||
|
||||
- `FISH_HISTFILE`: Name of the history session. It defaults to `fish` (which typically means the history will be saved in `~/.local/share/fish/fish_history`). This variable can be changed by the user. It does not have to be an environment variable. You can change it at will within an interactive fish session to change where subsequent commands are logged.
|
||||
- `FISH_HISTORY`: Name of the history session. It defaults to `fish` (which typically means the history will be saved in `~/.local/share/fish/fish_history`). This variable can be changed by the user. It does not have to be an environment variable. You can change it at will within an interactive fish session to change where subsequent commands are logged.
|
||||
|
||||
- `HOME`, the user's home directory. This variable can be changed by the user.
|
||||
|
||||
|
@ -1131,7 +1131,7 @@ History searches can be aborted by pressing the escape key.
|
|||
|
||||
Prefixing the commandline with a space will prevent the entire line from being stored in the history.
|
||||
|
||||
The command history is stored in the file `~/.local/share/fish/fish_history` (or `$XDG_DATA_HOME/fish/fish_history` if that variable is set) by default. However, you can set the `FISH_HISTFILE` environment variable to change the name of the history session (resulting in a `<session>_history` file); both before starting the shell and while the shell is running.
|
||||
The command history is stored in the file `~/.local/share/fish/fish_history` (or `$XDG_DATA_HOME/fish/fish_history` if that variable is set) by default. However, you can set the `FISH_HISTORY` environment variable to change the name of the history session (resulting in a `<session>_history` file); both before starting the shell and while the shell is running.
|
||||
|
||||
Examples:
|
||||
|
||||
|
|
|
@ -614,7 +614,7 @@ static void react_to_variable_change(const wcstring &key) {
|
|||
invalidate_termsize(true); // force fish to update its idea of the terminal size plus vars
|
||||
} else if (key == L"FISH_READ_BYTE_LIMIT") {
|
||||
env_set_read_limit();
|
||||
} else if (key == L"FISH_HISTFILE") {
|
||||
} else if (key == L"FISH_HISTORY") {
|
||||
history_destroy();
|
||||
reader_push(history_session_id().c_str());
|
||||
}
|
||||
|
|
|
@ -52,6 +52,9 @@
|
|||
//
|
||||
// Newlines are replaced by \n. Backslashes are replaced by \\.
|
||||
|
||||
// This is the history session ID we use by default if the user has not set env var FISH_HISTORY.
|
||||
#define DFLT_FISH_HISTORY_SESSION_ID L"fish"
|
||||
|
||||
// When we rewrite the history, the number of items we keep.
|
||||
#define HISTORY_SAVE_MAX (1024 * 256)
|
||||
|
||||
|
@ -1733,9 +1736,11 @@ static bool should_import_bash_history_line(const std::string &line) {
|
|||
/// commands. We can't actually parse bash syntax and the bash history file does not unambiguously
|
||||
/// encode multiline commands.
|
||||
void history_t::populate_from_bash(FILE *stream) {
|
||||
bool eof = false;
|
||||
// We do not import bash history if an alternative fish history file is being used.
|
||||
if (history_session_id() != DFLT_FISH_HISTORY_SESSION_ID) return;
|
||||
|
||||
// Process the entire history file until EOF is observed.
|
||||
bool eof = false;
|
||||
while (!eof) {
|
||||
auto line = std::string();
|
||||
|
||||
|
@ -1800,9 +1805,9 @@ void history_sanity_check() {
|
|||
}
|
||||
|
||||
wcstring history_session_id() {
|
||||
wcstring result = L"fish";
|
||||
wcstring result = DFLT_FISH_HISTORY_SESSION_ID;
|
||||
|
||||
const env_var_t session_id = env_get_string(L"FISH_HISTFILE");
|
||||
const env_var_t session_id = env_get_string(L"FISH_HISTORY");
|
||||
if (!session_id.missing()) {
|
||||
if (session_id.empty()) {
|
||||
result = L"";
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
# vim: set filetype=expect:
|
||||
# We're going to use three history files, including the default, to verify
|
||||
# that the FISH_HISTFILE variable works as expected.
|
||||
# that the FISH_HISTORY variable works as expected.
|
||||
set default_histfile "../test/data/fish/fish_history"
|
||||
set my_histfile "../test/data/fish/my_history"
|
||||
set env_histfile "../test/data/fish/env_history"
|
||||
|
||||
# =============
|
||||
# Verify that if we spawn fish with no FISH_HISTFILE env var it uses the
|
||||
# Verify that if we spawn fish with no FISH_HISTORY env var it uses the
|
||||
# default file.
|
||||
# =============
|
||||
set fish_pid [spawn $fish]
|
||||
|
@ -34,7 +34,7 @@ expect_prompt -re "\r\n$hist_line\r\n" {
|
|||
# history file is not written to.
|
||||
set cmd2 "echo $fish_pid my histfile"
|
||||
set hist_line "- cmd: $cmd2"
|
||||
send "set FISH_HISTFILE my\r"
|
||||
send "set FISH_HISTORY my\r"
|
||||
expect_prompt
|
||||
send "$cmd2\r"
|
||||
expect_prompt
|
||||
|
@ -62,7 +62,7 @@ expect_prompt -re "\r\n$hist_line\r\n" {
|
|||
# Switch back to the default history file.
|
||||
set cmd3 "echo $fish_pid default histfile again"
|
||||
set hist_line "- cmd: $cmd3"
|
||||
send "set FISH_HISTFILE default\r"
|
||||
send "set FISH_HISTORY default\r"
|
||||
expect_prompt
|
||||
send "$cmd3\r"
|
||||
expect_prompt
|
||||
|
@ -94,15 +94,15 @@ expect_prompt -re "\r\n$hist_line\r\n" {
|
|||
send "exit\r"
|
||||
close $spawn_id
|
||||
|
||||
# Set the FISH_HISTFILE env var.
|
||||
set ::env(FISH_HISTFILE) env
|
||||
# Set the FISH_HISTORY env var.
|
||||
set ::env(FISH_HISTORY) env
|
||||
|
||||
# Spawn a new shell.
|
||||
set prompt_counter 1
|
||||
set fish_pid [spawn $fish]
|
||||
expect_prompt
|
||||
|
||||
# Verify that the new fish shell is using the FISH_HISTFILE value for history.
|
||||
# Verify that the new fish shell is using the FISH_HISTORY value for history.
|
||||
set cmd4 "echo $fish_pid env histfile"
|
||||
set hist_line "- cmd: $cmd4"
|
||||
send "$cmd4\r"
|
||||
|
|
Loading…
Reference in a new issue