Based on the code that I can read here and on the documentation here, it appears that an option like this is not currently available.
However, the code indicates that the history.txt
file is located at ~/.config/nushell
. Utilizing this information, it is possible to accomplish what you asked by using my script below:
nushell_history_manager.py
import os
import sys
def delete_lines(file_path, line_numbers):
# open the file in read mode
with open(file_path, 'r') as f:
# read all the lines and store them in a list
lines = f.readlines()
# open the file in write mode
with open(file_path, 'w') as f:
for i, line in enumerate(lines):
# check if the current line number is not in the list of line numbers to delete
if i+1 not in line_numbers:
# if it's not, write the line to the file
f.write(line)
def print_table(file_path):
# open the file in read mode
with open(file_path, 'r') as f:
# read all the lines and store them in a list
lines = f.readlines()
# print the table header
print("╭──────┬───────────────────────────────╮")
print("│ ## │ command │")
print("├──────┼───────────────────────────────┤")
for i, line in enumerate(lines):
# print each line number and the corresponding command
print(f"│ {i+1:3} │ {line.strip():26} │")
# print the table footer
print("╰──────┴───────────────────────────────╯")
if __name__ == '__main__':
# set the file path to the history.txt file in the nushell config directory
file_path = os.path.expanduser('~/.config/nushell/history.txt')
# print the initial contents of the file in a table format
print_table(file_path)
# ask the user to enter line numbers to delete
line_numbers_str = input("Enter line numbers to delete (separated by commas): ")
# convert the entered line numbers to a list of integers
line_numbers = list(map(int, line_numbers_str.split(',')))
# delete the specified lines from the file
delete_lines(file_path, line_numbers)
# print the updated contents of the file in a table format
print_table(file_path)
Usage:
python nushell_history_manager.py
Runtime:
╭──────┬────────────────────╮
│ ## │ command │
├──────┼────────────────────┤
│ 1 │ history --clear │
│ 2 │ php --version │
│ 3 │ composer --version │
│ 4 │ node --version │
│ 5 │ npm --version │
│ 6 │ composer --version │
│ 7 │ history │
│ 8 │ php --version │
│ 9 │ history │
│ 10 │ php --version │
│ 11 │ history │
│ 12 │ composer --version │
╰──────┴────────────────────╯
Enter line numbers to delete (separated by commas): 7,9,11
╭──────┬────────────────────╮
│ ## │ command │
├──────┼────────────────────┤
│ 1 │ history --clear │
│ 2 │ php --version │
│ 3 │ composer --version │
│ 4 │ node --version │
│ 5 │ npm --version │
│ 6 │ composer --version │
│ 7 │ php --version │
│ 8 │ php --version │
│ 9 │ composer --version │
╰──────┴────────────────────╯