如何删除 Nushell 中的特定历史记录条目?

Cha*_*per 7 nushell

鉴于 Nushell 中存在这样的历史记录,我如何删除特定条目?例如条目 6、8 和 10?

\n
nu > history\n\xe2\x95\xad\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x95\xae\n\xe2\x94\x82  # \xe2\x94\x82      command       \xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n\xe2\x94\x82  0 \xe2\x94\x82 history --clear    \xe2\x94\x82\n\xe2\x94\x82  1 \xe2\x94\x82 php --version      \xe2\x94\x82\n\xe2\x94\x82  2 \xe2\x94\x82 composer --version \xe2\x94\x82\n\xe2\x94\x82  3 \xe2\x94\x82 node --version     \xe2\x94\x82\n\xe2\x94\x82  4 \xe2\x94\x82 npm --version      \xe2\x94\x82\n\xe2\x94\x82  5 \xe2\x94\x82 composer --version \xe2\x94\x82\n\xe2\x94\x82  6 \xe2\x94\x82 history            \xe2\x94\x82\n\xe2\x94\x82  7 \xe2\x94\x82 php --version      \xe2\x94\x82\n\xe2\x94\x82  8 \xe2\x94\x82 history            \xe2\x94\x82\n\xe2\x94\x82  9 \xe2\x94\x82 php --version      \xe2\x94\x82\n\xe2\x94\x82 10 \xe2\x94\x82 history            \xe2\x94\x82\n\xe2\x94\x82 11 \xe2\x94\x82 composer --version \xe2\x94\x82\n\xe2\x95\xb0\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x95\xaf\n
Run Code Online (Sandbox Code Playgroud)\n

And*_*ris 12

根据我可以在此处阅读的代码和此处的文档,看来这样的选项当前不可用。

\n

但是,代码表明该history.txt文件位于~/.config/nushell. 利用这些信息,可以使用下面的脚本来完成您所要求的任务:

\n

nushell_history_manager.py

\n
import os\nimport sys\n\ndef delete_lines(file_path, line_numbers):\n    # open the file in read mode\n    with open(file_path, \'r\') as f:\n        # read all the lines and store them in a list\n        lines = f.readlines()\n    # open the file in write mode\n    with open(file_path, \'w\') as f:\n        for i, line in enumerate(lines):\n            # check if the current line number is not in the list of line numbers to delete\n            if i+1 not in line_numbers:\n                # if it\'s not, write the line to the file\n                f.write(line)\n\ndef print_table(file_path):\n    # open the file in read mode\n    with open(file_path, \'r\') as f:\n        # read all the lines and store them in a list\n        lines = f.readlines()\n    # print the table header\n    print("\xe2\x95\xad\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x95\xae")\n    print("\xe2\x94\x82  ##  \xe2\x94\x82            command            \xe2\x94\x82")\n    print("\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4")\n    for i, line in enumerate(lines):\n        # print each line number and the corresponding command\n        print(f"\xe2\x94\x82 {i+1:3}  \xe2\x94\x82 {line.strip():26}    \xe2\x94\x82")\n    # print the table footer\n    print("\xe2\x95\xb0\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x95\xaf")\n\nif __name__ == \'__main__\':\n    # set the file path to the history.txt file in the nushell config directory\n    file_path = os.path.expanduser(\'~/.config/nushell/history.txt\')\n    # print the initial contents of the file in a table format\n    print_table(file_path)\n    # ask the user to enter line numbers to delete\n    line_numbers_str = input("Enter line numbers to delete (separated by commas): ")\n    # convert the entered line numbers to a list of integers\n    line_numbers = list(map(int, line_numbers_str.split(\',\')))\n    # delete the specified lines from the file\n    delete_lines(file_path, line_numbers)\n    # print the updated contents of the file in a table format\n    print_table(file_path)\n
Run Code Online (Sandbox Code Playgroud)\n

用法:

\n
python nushell_history_manager.py\n
Run Code Online (Sandbox Code Playgroud)\n

运行:

\n
\xe2\x95\xad\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x95\xae\n\xe2\x94\x82  ##  \xe2\x94\x82      command       \xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n\xe2\x94\x82   1  \xe2\x94\x82 history --clear    \xe2\x94\x82\n\xe2\x94\x82   2  \xe2\x94\x82 php --version      \xe2\x94\x82\n\xe2\x94\x82   3  \xe2\x94\x82 composer --version \xe2\x94\x82\n\xe2\x94\x82   4  \xe2\x94\x82 node --version     \xe2\x94\x82\n\xe2\x94\x82   5  \xe2\x94\x82 npm --version      \xe2\x94\x82\n\xe2\x94\x82   6  \xe2\x94\x82 composer --version \xe2\x94\x82\n\xe2\x94\x82   7  \xe2\x94\x82 history            \xe2\x94\x82\n\xe2\x94\x82   8  \xe2\x94\x82 php --version      \xe2\x94\x82\n\xe2\x94\x82   9  \xe2\x94\x82 history            \xe2\x94\x82\n\xe2\x94\x82  10  \xe2\x94\x82 php --version      \xe2\x94\x82\n\xe2\x94\x82  11  \xe2\x94\x82 history            \xe2\x94\x82\n\xe2\x94\x82  12  \xe2\x94\x82 composer --version \xe2\x94\x82\n\xe2\x95\xb0\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x95\xaf\n\nEnter line numbers to delete (separated by commas): 7,9,11\n\n\xe2\x95\xad\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x95\xae\n\xe2\x94\x82  ##  \xe2\x94\x82      command       \xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n\xe2\x94\x82   1  \xe2\x94\x82 history --clear    \xe2\x94\x82\n\xe2\x94\x82   2  \xe2\x94\x82 php --version      \xe2\x94\x82\n\xe2\x94\x82   3  \xe2\x94\x82 composer --version \xe2\x94\x82\n\xe2\x94\x82   4  \xe2\x94\x82 node --version     \xe2\x94\x82\n\xe2\x94\x82   5  \xe2\x94\x82 npm --version      \xe2\x94\x82\n\xe2\x94\x82   6  \xe2\x94\x82 composer --version \xe2\x94\x82\n\xe2\x94\x82   7  \xe2\x94\x82 php --version      \xe2\x94\x82\n\xe2\x94\x82   8  \xe2\x94\x82 php --version      \xe2\x94\x82\n\xe2\x94\x82   9  \xe2\x94\x82 composer --version \xe2\x94\x82\n\xe2\x95\xb0\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x95\xaf\n
Run Code Online (Sandbox Code Playgroud)\n

  • 非常好的答案 - 谢谢。我将 pmf 标记为已接受的,因为它是惯用的 Nushell,并且不需要外部 Python 脚本。但我也给你投了赞成票,因为它很棒。再次感谢。 (2认同)

pmf*_*pmf 11

nu 的history命令(尚未)提供类似于 bash 的删除项目的功能history -d。但是,您可以使用 查询历史文件所在的位置$nu.history-path,然后使用drop nth删除有问题的行。

open $nu.history-path | lines | drop nth 6 8 10 | save -f $nu.history-path
Run Code Online (Sandbox Code Playgroud)