Using ChatGPT in the Terminal
I found this tool which lets you use Chat GPT from the terminal: https://github.com/TheR1D/shell_gpt
Maybe you’re thinking, “haha, funny trick, but why would I want to do that, are you trying to prove something?”
But I promise you, if you give this a try, you won’t be disappointed.
You can read all about the functionality and configuration in the readme, but here are just some examples:
Basic query:
sgpt “hello chatgpt”
The output is Markdown formatted, so you can pipe it to pandoc:
sgpt “hello chatgpt” pandoc -t plain
As with a session on chat.openai.com, you can use named sessions so that it remembers the context of your chats (by default, it starts a new chat for every query)
sgpt –chat my_chat_name “hello chatgpt”
Using the -s
option, it will ensure that a shell command is output, and will give you keyboard shortcuts E (execute), D (describe), or A (abort).
sgpt -s “generate me a 500x500 image of random colors. Use low frequency for the noise so there are larger features. Make sure the output is colored and not black and white”
Which outputs:
convert -size 500x500 xc: +noise Random -virtual-pixel tile -blur 0x10 -auto-level -separate -combine output.png [E]xecute, [D]escribe, [A]bort: e
And, upon executing, produces the image:
You can also pipe command output into sgpt:
cat test.rb
class Foo
def bar
puts "ok"
end
end
cat test.rb cgpt -s convert this to javascript
Here is the JavaScript equivalent of your Ruby code:
class Foo {
bar() {
console.log("ok");
}
}
This code defines a class Foo with a method bar that logs the string
“ok” to the console.
I have also set up some shortcuts for these commands. I use fish shell, but these could probably be converted to bash without much difficulty (Chat GPT might help?)
The cn
function can called either by itself to start a new named chat (with a random name), or with a provided name argument. In either case, the name will saved to an environment variable so you don’t have to continually enter it for subsequent commands:
function cn
echo "previous chat name: $SGPT_CHAT_NAME"
set -l _sgpt_chat_name (LC_ALL=C tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 20 | head -n 1)
if set -q argv[1]
set _sgpt_chat_name $argv[1]
end
set -gx SGPT_CHAT_NAME $_sgpt_chat_name
echo "new chat name: $SGPT_CHAT_NAME"
end
Example usage:
cn
# => previous chat name: qQ76mbHQKOubmMO1VxIk
# => new chat name: XV6MdnDbp0fOUXZVdV83
cn my_new_chat_name
# => previous chat name: XV6MdnDbp0fOUXZVdV83
# => new chat name: my_named_chat
The c
function is a wrapper for sgpt --chat
but uses the saved chat name (or generates a new one, if one isn’t available), and pipes the output to pandoc. You will need to escape double quotes and some other special characters in the query string.
function c
if not set -q SGPT_CHAT_NAME
set -gx SGPT_CHAT_NAME (LC_ALL=C tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 20 | head -n 1)
end
sgpt --chat $SGPT_CHAT_NAME "$argv" | pandoc -t plain
end
Example usage:
# Note you have to escape question mark
c what color is the mix of red and green\?
cat file.json | c do some analysis on this file
And finally, the cs
function which is basically the same as c
but uses the - s
flag and doesn’t pipe the output to pandoc:
function cs
if not set -q SGPT_CHAT_NAME
set -gx SGPT_CHAT_NAME (LC_ALL=C tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -$
end
sgpt -s --chat $SGPT_CHAT_NAME "$argv"
end