How can I clear the history without history -c?

Hello Garuda users.

I have a question regarding the terminal konsole, I have searched for how to clear the history and it appears that I must delete the contents of the bash_history file and then use the command

history -c 

and

history -w

, however I have already deleted the contents of the bash_history, I have entered history -c and history -w, but they only print the history, I hope for your help, thank you.

HI @SCK, welcome to the community. Please post the output of garuda-inxi for all Issues & Assistance topics.


history is managed differently in Fish than it is in Bash. If you are interested in learning more about it, run man history in the terminal.

man history
HISTORY(1)                                                      fish-shell                                                     HISTORY(1)

NAME
       history - show and manipulate command history

SYNOPSIS
       history [search] [--show-time] [--case-sensitive]
                        [--exact | --prefix | --contains] [--max N] [--null] [--reverse]
                        [SEARCH_STRING ...]
       history delete [--case-sensitive]
                      [--exact | --prefix | --contains] SEARCH_STRING ...
       history merge
       history save
       history clear
       history clear-session

DESCRIPTION
       history is used to search, delete, and otherwise manipulate the history of interactive commands.

       The following operations (sub-commands) are available:

       search Returns history items matching the search string. If no search string is provided it returns all history items. This is the
              default  operation if no other operation is specified. You only have to explicitly say history search if you wish to search
              for one of the subcommands. The --contains search option will be used if you don't specify a different search  option.  En‐
              tries  are  ordered  newest  to oldest unless you use the --reverse flag. If stdout is attached to a tty the output will be
              piped through your pager by the history function. The history builtin simply writes the results to stdout.

       delete Deletes history items. The --contains search option will be used if you don't specify a different  search  option.  If  you
              don't  specify  --exact a prompt will be displayed before any items are deleted asking you which entries are to be deleted.
              You can enter the word "all" to delete all matching entries. You can enter a single ID (the number in square  brackets)  to
              delete  just  that single entry. You can enter more than one ID separated by a space to delete multiple entries. Just press
              [enter] to not delete anything. Note that the interactive delete behavior is a feature of the history function. The history
              builtin only supports --exact --case-sensitive deletion.

       merge  Immediately incorporates history changes from other sessions. Ordinarily fish ignores history changes from sessions started
              after the current one. This command applies those changes immediately.

       save   Immediately writes all changes to the history file. The shell automatically saves the history file; this option is provided
              for internal use and should not normally need to be used by the user.

       clear  Clears the history file. A prompt is displayed before the history is erased asking you to confirm you really want to  clear
              all history unless builtin history is used.

       clear-session
              Clears the history file from all activity of the current session. Note: If history merge or builtin history merge is run in
              a session, only the history after this will be erased.

       The following options are available:

       These flags can appear before or immediately after one of the sub-commands listed above.

       -C or --case-sensitive
              Does  a  case-sensitive  search. The default is case-insensitive. Note that prior to fish 2.4.0 the default was case-sensi‐
              tive.

       -c or --contains
              Searches items in the history that contain the specified text string. This is the default for the --search  flag.  This  is
              not currently supported by the delete subcommand.

       -e or --exact
              Searches  or  deletes items in the history that exactly match the specified text string. This is the default for the delete
              subcommand. Note that the match is case-insensitive by default. If you really want an exact match, including  letter  case,
              you must use the -C or --case-sensitive flag.

       -p or --prefix
              Searches items in the history that begin with the specified text string. This is not currently supported by the delete sub‐
              command.

       -t or --show-time
              Prepends  each  history entry with the date and time the entry was recorded. By default it uses the strftime format # %c%n.
              You can specify another format; e.g., --show-time="%Y-%m-%d %H:%M:%S "  or  --show-time="%a%I%p".  The  short  option,  -t,
              doesn't  accept  a strftime format string; it only uses the default format. Any strftime format is allowed, including %s to
              get the raw UNIX seconds since the epoch.

       -z or --null
              Causes history entries written by the search operations to be terminated by a NUL character rather than a newline. This al‐
              lows the output to be processed by read -z to correctly handle multiline history entries.

       -*NUMBER* -n NUMBER or --max NUMBER
              Limits the matched history items to the first NUMBER matching entries. This is only valid for history search.

       -R or --reverse
              Causes the history search results to be ordered oldest to newest. Which is the order used by most shells.  The  default  is
              newest to oldest.

       -h or --help
              Displays help for this command.

EXAMPLE
          history clear
          # Deletes all history items

          history search --contains "foo"
          # Outputs a list of all previous commands containing the string "foo".

          history delete --prefix "foo"
          # Interactively deletes commands which start with "foo" from the history.
          # You can select more than one entry by entering their IDs separated by a space.

CUSTOMIZING THE NAME OF THE HISTORY FILE
       By default interactive commands are logged to $XDG_DATA_HOME/fish/fish_history (typically ˜/.local/share/fish/fish_history).

       You  can  set the fish_history variable to another name for the current shell session. The default value (when the variable is un‐
       set) 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_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 con‐
       flicts 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.

NOTES
       If you specify both --prefix and --contains the last flag seen is used.

       Note  that  for  backwards  compatibility each subcommand can also be specified as a long option. For example, rather than history
       search you can type history --search. Those long options are deprecated and will be removed in a future release.

COPYRIGHT
       2023, fish-shell developers

3.6                                                            Mar 25, 2023                                                    HISTORY(1)

To clear the history, run:

history clear
2 Likes

I just tried the command and it continues printing the history without deleting it, do I need to use sudo or something like that?

╭─sck@SCKlaptop in ~ took 1s
╰─λ history clear
2023-09-19 23:10:12 reset
2023-09-19 23:09:39 clear
2023-09-19 23:02:10 history clear | history clear
2023-09-19 23:01:31 history clear
2023-09-19 23:00:06 history 1mclear
2023-09-19 22:57:57 sudo history clear

Please.

1 Like

Just as you say @BluishHumility, it is history clear, but I can’t understand why the history is kept intact, am I doing something wrong?

1m-e 22mor 1m--exact0m
Searches  or deletes items in the history that exactly match the specified text string. This is the default for the 1mdelete 22msubcommand. Note that the match is case-insensitive by default. If you really
want an exact match, including letter case, you must use the 1m-C 22mor 1m--case-sensitive 22mflag.

1m-p 22mor 1m--prefix0m
Searches items in the history that begin with the specified text string. This is not currently supported by the 1mdelete 22msubcommand.

1m-t 22mor 1m--show-time0m
Prepends each history entry with the date and time the entry was recorded. By default it uses the strftime format 1m# %c%n22m. You can specify another format;  e.g.,  1m--show-time="%Y-%m-%d  %H:%M:%S  "  22mor
1m--show-time="%a%I%p"22m. The short option, 1m-t22m, doesn't accept a strftime format string; it only uses the default format. Any strftime format is allowed, including 1m%s 22mto get the raw UNIX seconds since the
epoch.

1m-z 22mor 1m--null0m
Causes  history entries written by the search operations to be terminated by a NUL character rather than a newline. This allows the output to be processed by 1mread -z 22mto correctly handle multiline his‐
tory entries.

1m-22m*NUMBER* 1m-n 4m22mNUMBER24m or 1m--max 4m22mNUMBER0m
Limits the matched history items to the first 4mNUMBER24m matching entries. This is only valid for 1mhistory search22m.

1m-R 22mor 1m--reverse0m
Causes the history search results to be ordered oldest to newest. Which is the order used by most shells. The default is newest to oldest.

1m-h 22mor 1m--help0m
Displays help for this command.

1mEXAMPLE0m
history clear
# Deletes all history items

history search --contains "foo"
# Outputs a list of all previous commands containing the string "foo".

history delete --prefix "foo"
# Interactively deletes commands which start with "foo" from the history.
# You can select more than one entry by entering their IDs separated by a space.

1mCUSTOMIZING THE NAME OF THE HISTORY FILE0m
By default interactive commands are logged to 1m$XDG_DATA_HOME/fish/fish_history 22m(typically 1m~/.local/share/fish/fish_history22m).

You can set the 1mfish_history 22mvariable to another name for the current shell session. The default value (when the variable is unset) is 1mfish 22mwhich corresponds to 1m$XDG_DATA_HOME/fish/fish_history22m. If you  set
it to e.g. 1mfun22m, the history would be written to 1m$XDG_DATA_HOME/fish/fun_history22m. 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 1mfish_history 22mat any time (by using 1mset -x fish_history "session_name"22m) and it will take effect right away. If you set it to 1m"default"22m, it will use the default session name (which is 1m"fish"22m).

1m-e 22mor 1m--exact0m
Searches  or deletes items in the history that exactly match the specified text string. This is the default for the 1mdelete 22msubcommand. Note that the match is case-insensitive by default. If you really
want an exact match, including letter case, you must use the 1m-C 22mor 1m--case-sensitive 22mflag.

1m-p 22mor 1m--prefix0m
Searches items in the history that begin with the specified text string. This is not currently supported by the 1mdelete 22msubcommand.

1m-t 22mor 1m--show-time0m
Prepends each history entry with the date and time the entry was recorded. By default it uses the strftime format 1m# %c%n22m. You can specify another format;  e.g.,  1m--show-time="%Y-%m-%d  %H:%M:%S  "  22mor
1m--show-time="%a%I%p"22m. The short option, 1m-t22m, doesn't accept a strftime format string; it only uses the default format. Any strftime format is allowed, including 1m%s 22mto get the raw UNIX seconds since the
epoch.

1m-z 22mor 1m--null0m
Causes  history entries written by the search operations to be terminated by a NUL character rather than a newline. This allows the output to be processed by 1mread -z 22mto correctly handle multiline his‐
tory entries.

1m-22m*NUMBER* 1m-n 4m22mNUMBER24m or 1m--max 4m22mNUMBER0m
Limits the matched history items to the first 4mNUMBER24m matching entries. This is only valid for 1mhistory search22m.

1m-R 22mor 1m--reverse0m
Causes the history search results to be ordered oldest to newest. Which is the order used by most shells. The default is newest to oldest.

1m-h 22mor 1m--help0m
Displays help for this command.

1mEXAMPLE0m
history clear
# Deletes all history items

history search --contains "foo"
# Outputs a list of all previous commands containing the string "foo".

history delete --prefix "foo"
# Interactively deletes commands which start with "foo" from the history.
# You can select more than one entry by entering their IDs separated by a space.

1mCUSTOMIZING THE NAME OF THE HISTORY FILE0m
By default interactive commands are logged to 1m$XDG_DATA_HOME/fish/fish_history 22m(typically 1m~/.local/share/fish/fish_history22m).

You can set the 1mfish_history 22mvariable to another name for the current shell session. The default value (when the variable is unset) is 1mfish 22mwhich corresponds to 1m$XDG_DATA_HOME/fish/fish_history22m. If you  set
it to e.g. 1mfun22m, the history would be written to 1m$XDG_DATA_HOME/fish/fun_history22m. 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 1mfish_history 22mat any time (by using 1mset -x fish_history "session_name"22m) and it will take effect right away. If you set it to 1m"default"22m, it will use the default session name (which is 1m"fish"22m).

Other  shells such as bash and zsh use a variable named 1mHISTFILE 22mfor 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 1mfish 22mor 1mdefault 22mit will inhibit importing the bash history. That's because the most common use case for this feature is to avoid leaking private or sen‐
sitive history when giving a presentation.

1mNOTES0m
If you specify both 1m--prefix 22mand 1m--contains 22mthe last flag seen is used.

Note that for backwards compatibility each subcommand can also be specified as a long option. For example, rather than 1mhistory search 22myou can type 1mhistory --search22m. Those long options are deprecated and will
be removed in a future release.

This information is still missing:

You can get this output by running this command in the terminal:

garuda-inxi

What leads you to believe the history is kept intact?

After clearing the history, run the command with no arguments to see what is in the history.

history
2 Likes

You can try doing the same with

~/.local/share/fish/fish_history

as mentioned in man page of this command. And yes please follow the template, garuda-inxi please. We can’t have exceptions for anyone opening a help request.

I can actually replicate the issue SCK is facing. I too can’t clear my fish shell’s history with

history clear

In fact none of the options or flags I give history seem to be registered by it for some reason, e.g.,

history --help

according to man should output help but it returns the history of my shell instead. :thinking:
Yep, just confirmed with bash. There is something wrong with fish’s history it’s not parsing any flags or verbs passed to it.
eg,
In bash

history --help
history --help
history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
Display or manipulate the history list.

Display the history list with line numbers, prefixing each modified
entry with a `*'.  An argument of N lists only the last N entries.

Options:
-c        clear the history list by deleting all of the entries
-d offset delete the history entry at position OFFSET. Negative
offsets count back from the end of the history list

-a        append history lines from this session to the history file
-n        read all history lines not already read from the history file
and append them to the history list
-r        read the history file and append the contents to the history
list
-w        write the current history to the history file

-p        perform history expansion on each ARG and display the result
without storing it in the history list
-s        append the ARGs to the history list as a single entry

If FILENAME is given, it is used as the history file.  Otherwise,
if HISTFILE has a value, that is used, else ~/.bash_history.

If the HISTTIMEFORMAT variable is set and not null, its value is used
as a format string for strftime(3) to print the time stamp associated
with each displayed history entry.  No time stamps are printed otherwise.

Exit Status:
Returns success unless an invalid option is given or an error occurs.

but the same in fish

2023-09-20 19:44:58 bash
2023-09-20 19:44:44 ls /usr/lib/virtualbox/
2023-09-20 19:44:28 ls /sbin/vboxconfig
2023-09-20 19:43:52 history | less
2023-09-20 19:43:46 history --clear
2023-09-20 19:42:08 history clear-session
2023-09-20 19:41:00 history search rm
2023-09-20 19:40:39 man history
2023-09-20 19:40:34 history -C rm
2023-09-20 19:36:05 history clear
2023-09-20 19:34:51 history --help | less
2023-09-20 19:34:26 history --help
2023-09-20 19:32:33 ls ~/.local/share/fish/fish_history
2023-09-20 19:32:20 ls .local/share/fish/fish_history
2023-09-20 19:30:47 ls
2023-09-20 19:30:46 rm \n
2023-09-20 19:30:00 rm '\n'
2023-09-20 19:29:28 ls $XDG_DATA_HOME/fish/fish_history
2023-09-20 19:29:12 echo $XDG_DATA_HOME
2023-09-20 19:27:03 cat .local/share/fish/fish_history
2023-09-20 19:26:58 ls .local/share/fish/
2023-09-20 10:59:56 history -- --help

It’s the same for any flag or verb, fish’s history is simply not reading it.

1 Like

Do you need it all or is this enough?

╭─scc@SCClaptop in ~ took 35ms
╰─λ garuda-inxi
System:
Kernel: 6.5.3-zen1-1-zen arch: x86_64 bits: 64 compiler: gcc v: 13.2.1
clocksource: hpet available: acpi_pm
parameters: BOOT_IMAGE=/@/boot/vmlinuz-linux-zen
root=UUID=f3a280ec-22f8-4ca8-ab9a-e18ab7301c8f rw rootflags=subvol=@
quiet quiet rd.udev.log_priority=3 vt.global_cursor_default=0 loglevel=3
ibt=off
Desktop: KDE Plasma v: 5.27.8 tk: Qt v: 5.15.10 wm: kwin_x11 vt: 2
dm: SDDM Distro: Garuda Linux base: Arch Linux

Info:
Processes: 287 Uptime: 4m wakeups: 1 Memory: total: 6 GiB
available: 5.73 GiB used: 2.22 GiB (38.7%) Init: systemd v: 254
default: graphical tool: systemctl Compilers: gcc: 13.2.1 Packages:
pm: pacman pkgs: 1407 libs: 428 tools: octopi,paru Shell: fish v: 3.6.1
default: Bash v: 5.1.16 running-in: konsole inxi: 3.3.29
Garuda (2.6.16-1):
System install date:     2023-07-26
Last full system update: 2023-09-20
Is partially upgraded:   No
Relevant software:       snapper NetworkManager dracut
Windows dual boot:       No/Undetected
Failed units:
╭─scc@SCClaptop in ~ took 2s
╰─λ history
2023-09-20 09:02:11 garuda-inxi
2023-09-20 08:58:19 ls
2023-09-20 08:53:41 reset
2023-09-20 08:52:27 cat /etc/shells
2023-09-20 08:51:57 history -c
2023-09-20 08:47:44 sudo garuda-update
2023-09-20 08:46:04 history clear
2023-09-20 08:22:49 man history
2023-09-20 08:21:03 rm Limpiador.sh
2023-09-20 08:20:36 sudo ./Limpiador.sh
2023-09-20 08:15:32 vim Limpiador.sh
2023-09-20 08:15:19 chmod +x Limpiador.sh
2023-09-20 08:14:54 cd Escritorio/
2023-09-20 08:14:00 man --help
2023-09-20 08:13:47 clear
2023-09-20 08:11:53 bash
2023-09-19 23:25:52 shutdown now
2023-09-19 23:23:09 chmod + x Limpiador.sh
2023-09-19 23:22:45 sudo chmod + x Limpiador.sh
2023-09-19 23:22:23 mv Limpiardor.bash Limpiador.sh
2023-09-19 23:21:29 touch Limpiardor.bash
2023-09-19 23:19:28 history delete "all"
2023-09-19 23:19:07 history delete "sudo"
2023-09-19 23:17:59 history clear-session
2023-09-19 23:02:10 history clear | history clear

I cannot reproduce this.

❯ history clear
If you enter 'yes' your entire interactive command history will be erased
Are you sure you want to clear history? (yes/no) yes
Command history cleared!

~ took 4s
❯ history

~
❯

I didn’t particularly want my history cleared, but here we are. :joy:

1 Like

Ah I found out why this is happening, it seems to be a case of bad alias creation in fish config. :sweat:

# Fish command history
function history
builtin history --show-time='%F %T '
end

using in terminal

builtin history clear

works :+1: replacing clear with other flags and verbs works too.

8 Likes

Good catch, I removed that function a long time ago and completely forgot about it. :sweat_smile:

2 Likes

Thank you very much Bluish Humility and especially the last one with the strange name, they were very kind, I wish you a good day.

1 Like

:rofl: I guess I should look for a better username to use online. btw, I got this one with a “cool discord user names” on google. I saw this at the top of the list and decided to use it everywhere. So I am quite open to suggestions.

1 Like

:grimacing:

KmA :slight_smile:
I like it short :wink:

2 Likes

So I assume we should remove it, right?

Yeah we should, I checked with functions --erase history and history command works flawlessly.
Removed it from config file and checked, no problems.

It’s kind of a neat function because it provides the timestamps, and the formatting is a bit less clunky than just using the --show-time flag on its own.

❯ history --show-time
# Wed 20 Sep 2023 01:01:30 PM EDT
history -h
# Wed 20 Sep 2023 01:00:23 PM EDT
bash
# Wed 20 Sep 2023 01:00:09 PM EDT
history
# Wed 20 Sep 2023 12:48:21 PM EDT
micro /etc/pacman.conf

~
❯ history --show-time='%F %T '
2023-09-20 13:02:16 history --show-time
2023-09-20 13:01:30 history -h
2023-09-20 13:00:23 bash
2023-09-20 13:00:09 history
2023-09-20 12:48:21 micro /etc/pacman.conf

But it seems like the search options might not be compatible with the other options.

history - show and manipulate command history

history [search] [--show-time] [--case-sensitive]
                 [--exact | --prefix | --contains] [--max N] [--null] [--reverse]
                 [SEARCH_STRING ...]
history delete [--case-sensitive]
               [--exact | --prefix | --contains] SEARCH_STRING ...
history merge
history save
history clear
history clear-session

So we would have to write in some logic to allow the function to honor the other possible options. :thinking:

Perhaps we could just alias history --show-time='%F %T ' to history -t so folks who use that flag get the cleaner formatting, and if anyone wants to take it further than that they can tweak it on their own.

-t or --show-time
       Prepends each history entry with the date and time the entry was recorded. By default it
       uses  the  strftime  format  #   %c%n.   You   can   specify   another   format;   e.g.,
       --show-time="%Y-%m-%d  %H:%M:%S " or --show-time="%a%I%p". The short option, -t, doesn't
       accept a strftime format string; it only uses the default format. Any strftime format is
       allowed, including %s to get the raw UNIX seconds since the epoch.
3 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.