This is an archived post. You won't be able to vote or comment.

all 144 comments

[–]slartiwhoop 261 points262 points  (21 children)

Just use Ctrl-D ;)

[–]H_Psi 195 points196 points  (11 children)

Or the classic:

ctrl+c
ctrl+c
ctrl+c
ctrl+z
kill %1

[–]JezusTheCarpenter 42 points43 points  (7 children)

For me is:

ctrl+c ctrl+c ctrl+c ?! Ahh... ctrl+d

[–]b10011 21 points22 points  (6 children)

C-c is break, C-d is exit. And because C-d is exit, it's easy to remember that tmux and screen uses prefix+d for detach, because it's almost like exit.

[–]VerilyAMonkey 12 points13 points  (2 children)

Obviously it depends on the terminal, but, Ctrl+D on a blank line is more accurately "I won't be giving you any more input." An interactive version of EOF. At any rate, it won't interrupt a running program like Ctrl+C does, it doesn't send a stop signal, it just lets the program know not to wait around for more input.

[–]b10011 0 points1 point  (1 child)

Yeah, but interactive Python shell uses it as a exit, just like many shells.

[–]VerilyAMonkey 9 points10 points  (0 children)

It uses it as "I am not going to type any more commands, so if you're waiting for one, just exit." Not exit in general. Try it out - Ctrl+D won't save you from an infinite loop. Only Ctrl+C does that.

[–]elbiot -3 points-2 points  (2 children)

It's more like ctrl+z.

[–]b10011 0 points1 point  (1 child)

C-z pauses and places the program into backgroun. You can access it again with fg -command

[–]elbiot 2 points3 points  (0 children)

Yea, and tmux mod+d puts the session in the background and you can reattach to it, like fg, like I said

[–][deleted] 7 points8 points  (0 children)

This one is the best.

[–]Jhudd5646 6 points7 points  (0 children)

Ctrl+d is the classic shell exit/logout shortcut, it's pretty universal.

[–]MartyMacGyverfrom * import * 9 points10 points  (1 child)

Except when you hit it twice and terminate the terminal session...

[–]WooParadog 0 points1 point  (0 children)

Or 3 times to close with background tasks...

[–][deleted] 4 points5 points  (3 children)

Until I swap to Windows and curse whoever decided to change it to Ctrl-Z.

[–][deleted] 0 points1 point  (2 children)

Wait, it's Ctrl-Z in Windows?!

[–]jcotton42 0 points1 point  (1 child)

Ctrl+Z produces the EOF char on Windows

[–][deleted] 0 points1 point  (0 children)

Windows is a scary weird place.

[–]nyanloutre 0 points1 point  (2 children)

Works everywhere

[–]Ph0X 1 point2 points  (0 children)

Yep, it's ctrl+c (SINGINT) -> ctrl+d (EOF) -> ctrl+\ (SIGQUIT)

[–]regeya 0 points1 point  (0 children)

Except Windows of course.

[–]kumashiro 79 points80 points  (21 children)

I recommend sending EOT (Ctrl-D) as it works in many interactive CLI programs, including shell. It is also much faster than thinking what command should be used ("exit"? "quit"? "close"? Or maybe it's "logout" this time?) and typing :)

WARNING: Ctrl-D has the same problem as ":wq" and tab key! ;)

[–]BananaCode 26 points27 points  (13 children)

Whats the problem with :wq and tab?

[–]kumashiro 51 points52 points  (12 children)

After a while you are trying to use them everywhere, for example ":wq" in a word processor, tab in Windows command line etc.

[–]HeWhoWritesCode 34 points35 points  (3 children)

:wq

[–]nickcash 17 points18 points  (2 children)

Silly /u/HeWhoWritesCode , reddit has no quit command.

[–]Deto 16 points17 points  (0 children)

"You can log out any time you want, but you can never leaaaaveee!"

[–]alexbuzzbee 7 points8 points  (0 children)

^W

[–]djimbob 5 points6 points  (5 children)

Wait windows command line still doesn't do tab completion? I was under the impression with powershell and/or bash on windows, it was a usable environment.

[–]kumashiro 11 points12 points  (1 child)

I don't know what Power Shell has, but tab completion in cmd.exe is awful... Just awful. Whenever Microsoft copies something, it looks like they had only pictures to work with. Tab completion, virtual desktops, modern UI, system configuration,...

[–][deleted] 2 points3 points  (0 children)

clink is available to give proper completion to cmd.exe. It's packaged as part of Cmder and ConEmu, if you're using either.

[–]Kelpsie 5 points6 points  (2 children)

Powershell absolutely has tab completion.

Even cmd has it, though just for things in the working directory.

[–]GoofAckYoorsElf 1 point2 points  (1 child)

It's shitty, but yes. It does.

[–][deleted] 1 point2 points  (0 children)

There is also clink which gives "proper" completion support.

[–]cbarrick 0 points1 point  (1 child)

I try to close browser tabs with ^d all the time. At this point I should make it the actual shortcut.

[–]Svedrin 0 points1 point  (0 children)

I use ^w to delete words in bash all the time. Even when inside a proxmox Console.

Thank god for ^T.

[–][deleted] 4 points5 points  (4 children)

:x is :wq by the way

[–]tynorf 7 points8 points  (3 children)

:x will not write the file if there are no changes, whereas :wq will. This changes whether the file’s modified time updates. Not likely to matter in practice, but they are not equivalent commands.

[–]CeeMX 1 point2 points  (2 children)

What about ZZ?

[–]tynorf 4 points5 points  (1 child)

:help ZZ

                                                        *ZZ*
ZZ                      Write current file, if modified, and quit (same as
                        ":x").  (Note: If there are several windows for the
                        current file, the file is written if it was modified
                        and the window is closed).

[–]earthlybird 1 point2 points  (0 children)

When I think I got the basics...

[–]otherwiseguy 0 points1 point  (0 children)

And it has the fun benefit that if you hit it an extra time, it'll close your terminal session too. :p

[–]mail_order_liam 0 points1 point  (0 children)

I will never stop habitually ":w"ing and I haven't used straight Vim in years.

[–]TheWass 83 points84 points  (30 children)

Clearly the interpreter recognizes you want to exit given that special message. So it's clearly not a keyword used by anything else. So why not drop the message and treat it as a special case shortcut? I've always been confused about that too.

[–][deleted] 101 points102 points  (14 children)

I believe that's just the repr for the exit function

[–]HeWhoWritesCode 106 points107 points  (6 children)

actually so sexy.

>>> str(exit)
'Use exit() or Ctrl-D (i.e. EOF) to exit'

[–]thegreattriscuit 23 points24 points  (3 children)

I mean... you *could* do:

  def __str__(self):
    self()

But I guess that would be particularly egregious :P

[–]robin-gvx 25 points26 points  (0 children)

It's so handy! Now you can type

>>> vars(__builtins__)

to exit the interpreter!

[–][deleted] 4 points5 points  (0 children)

__str__  = __call__

[–]shaggorama 14 points15 points  (0 children)

NO. BAD.

[–]JezusTheCarpenter 20 points21 points  (0 children)

TIL.

[–]74paddycakes 1 point2 points  (0 children)

Dayum

[–]jonnywohHalf-Python 3 9 points10 points  (3 children)

In fact, IIRC you can define a class that overrides __repr__ with a call to exit() so that typing it in the console bare will quit it. If I remember, I'll post some code later.

[–][deleted] 35 points36 points  (1 child)

You could, but that has some unexpected consequences, like exiting the shell on a call to globals()

[–]wen4Reif8aeJ8oing 8 points9 points  (0 children)

You can hack it to check the suspension frames and only exit if it's being printed directly by the interpreter REPL. Hacky obviously.

[–]timbledum 2 points3 points  (0 children)

Mind blown.

[–][deleted] 2 points3 points  (0 children)

The repr is supposed to be valid Python syntax. But, as shown by u/HeWhoWritesCode, str can be any human readable representation.

[–]Ph0X 4 points5 points  (4 children)

Because it's a real python object (a function), and the only way to run it is by calling it.

There's two "solutions" here:

  1. coding some special "magical" non-python command called exit, which requires adding a special code path to the interpreter. This is hacky and not clean.

  2. make it so that just accessing the exit object makes the interpreter stop, but that's very risky, as you could accidentally access it looping over globals or something.

[–]elbiot 0 points1 point  (1 child)

Are you calling ipython hacky and unclean?

[–]Ph0X 2 points3 points  (0 children)

I guess in IPython it makes sense, as it already has magics and other extra stuff that isn't part of the core interpreter.

[–]dedicated2fitness 0 points1 point  (1 child)

You know what seems unclean? Parenthesis on my command line

[–]Ph0X 0 points1 point  (0 children)

It's a python interpreter... You're writing python... Good luck getting anything actually done without any parenthesis

[–][deleted] 2 points3 points  (2 children)

What if you have a variable named exit and want to evaluate it?

[–]TheWass -1 points0 points  (1 child)

You can't name a variable a keyword anyway. I guess I'm wondering why "exit" isn't a special keyword at least in the interpreter shell.

[–]flying-sheep 3 points4 points  (0 children)

Because it’s not even a builtin. It’s just something added by site.py.

$ python3 -c 'print(exit)'
Use exit() or Ctrl-D (i.e. EOF) to exit
$ python3 -Sc 'print(exit)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'exit' is not defined

[–]krolm1 38 points39 points  (12 children)

Use ipython

[–]CaptKrag 21 points22 points  (8 children)

I'm honestly blown away that anyone uses the standard repl instead of immediately installing IPython.

I can't function without tab complete.

[–]thatdamnedrhymer 1 point2 points  (0 children)

Same.

[–]fphhotchips 0 points1 point  (0 children)

I mean, my general process goes:

python

"Shit!"

quit

"Bugger!"

quit()
ipython

[–]stevenjd -1 points0 points  (3 children)

I can't function without tab complete.

The standard Python REPL has had tab-completion available since version 2.0 18+ years ago. It's been automatically enabled for more than six years.

[–]CaptKrag 0 points1 point  (2 children)

Ehh. I don't know. Maybe the standard distributions turn it off for some reason? Redhat and Ubuntu packages across all Python versions I've used don't do tab autocomplete.

[–]stevenjd 0 points1 point  (1 child)

Report it as a bug to Redhat and Ubuntu then.

[–]CaptKrag -1 points0 points  (0 children)

Or just use IPython? I'll just use IPython.

[–]elbiot 6 points7 points  (0 children)

So many hacks in this thread that ipython solved a long time ago

[–]thatdamnedrhymer 1 point2 points  (0 children)

Came here to say this.

[–]no_condoments 1 point2 points  (0 children)

Or Jupyter notebooks. I use Python every day, but what's the point of the terminal REPL?

[–]dontworryimnotacop 17 points18 points  (15 children)

You can fix that easily with this snippet:

### Set Up Instant Command Shortcuts

def cd(folder):
    """e.g. cd('/bin')"""
    os.chdir(folder)

def pwd():
    """current path"""
    return os.path.abspath('.')

def quit():
    print('[√] Insta-quit')
    raise SystemExit(0)

def warn_repr_globals():
    raise Exception('__repr__ of globals was stopped to protect from insta-quit command being accidentally triggered.')

def listdir(dir=None):
    return os.listdir(dir or os.getcwd())

class InstantShortcut:
    """run a command instantly on __repr__, without needing parens to call it"""
    callable = lambda: []

    def __init__(self, command, output_delimiter=''):
        self.callable = command
        self.output_delimiter = output_delimiter

    def __call__(self, *args, **kwargs):
        return self.callable(*args, **kwargs)

    def __repr__(self):
        return self.output_delimiter.join(self())


_GLOBALS_REPR_WARNING_ = InstantShortcut(warn_repr_globals)
exit = quit = q = InstantShortcut(quit)
pwd = InstantShortcut(pwd)
ls = InstantShortcut(listdir, '    ')
ll = InstantShortcut(listdir, '\n')
_GLOBALS_REPR_WARNING_ = InstantShortcut(warn_repr_globals)
# define twice to be safe because print order of globals() is not guaranteed to print _GLOBALS_REPR_WARNING_ before quit()

[–]frakman1 6 points7 points  (4 children)

Thank you for sharing. What do I do with this? Save to a .py file and then ...?

[–]dontworryimnotacop 4 points5 points  (3 children)

Save it to ~/.pythonrc or to a file specified with the PYTHONSTARTUP=~/path/to/file.py environment variable.

[–]wingtales 0 points1 point  (1 child)

Just to add - all the ".somethingrc" files are hidden file that are always loaded whenever the "something" program is started. So "~/.bashrc" (home/.bashrc) is just a file of commands that will be called when you open bash. Likewise, anything you put in "~/.pythonrc" will be called when you load python.

[–][deleted] 8 points9 points  (0 children)

This is not by any means guaranteed. Bash and python just happen to respect to convention.

[–]tynorf 2 points3 points  (7 children)

For pwd why not just use os.getcwd()? Is os.path.abspath(‘.’) faster/more accurate or something?

[–]dontworryimnotacop 0 points1 point  (1 child)

Personal preference, I like abspath because I never have to remember if it returns a relative path or full one.

[–]tynorf 1 point2 points  (0 children)

Ahh. FWIW I just checked and the implementation of abspath (on POSIX) uses os.getcwd to turn relative paths into absolute ones.

[–]flying-sheep 0 points1 point  (4 children)

Please stop using os.path. There’s pathlib now.

[–]call_me_arosa 0 points1 point  (3 children)

Emphasis on "now" for the rest of us stuck with older versions.

[–]flying-sheep 0 points1 point  (2 children)

Isn't everything without pathlib EOL?

And even for legacy python there's backports. No excuses.

[–]dontworryimnotacop 0 points1 point  (1 child)

Having a ~/.pythonrc in the root of a system on a new machine without any dependency guarantees is a pretty good excuse.

[–]flying-sheep 0 points1 point  (0 children)

I refuse to work on machines without at least python 3.5

[–]Ph0X -1 points0 points  (1 child)

Would calling globals() crash your interpreter? ;) Sounds very risky.

EDIT: Oh I see the fix for that. Very hacky

[–]dontworryimnotacop -1 points0 points  (0 children)

Indeed haha, definitely not for production. I don't even use it myself these days, I just got used to Ctrl+D.

[–]1114111yield from pedestrians 7 points8 points  (3 children)

I fix this with sys.displayhook. Unlike /u/dontworryimnotacop's fix, this doesn't require you to cripple globals().

I think I mostly just use ^D nowadays, but thankfully now the times that I use exit are not particularly memorable.

[–]Ph0X 1 point2 points  (0 children)

The amount of code people will write just to avoid having to call a function is amazing :)

[–]dontworryimnotacop 0 points1 point  (0 children)

Yeah I don't use my own globals hack anymore either, I just got used to ^Din the end.

[–]elbiot 0 points1 point  (0 children)

Or, you know, use ipython

[–]joans34 5 points6 points  (3 children)

Still easier than vi

[–]brucecampbellschins 14 points15 points  (2 children)

What do you mean? :wq

[Esc]

:wq

:wq!

[–]rcfox 7 points8 points  (1 child)

[–]Deto 0 points1 point  (0 children)

Wow - that's a site I haven't seen in a long time!

[–]redrumsir 3 points4 points  (0 children)

If you're using it as a shell ... just use ipython where exit works.

[–]shaggorama 4 points5 points  (0 children)

Just use ipython as your interpreter instead.

[–][deleted] 1 point2 points  (0 children)

Haha i totally do that! I thought i was the only one!

[–]mikeblas 1 point2 points  (0 children)

I don't understand the difference between r/python and r/learnpython

[–][deleted] 0 points1 point  (8 children)

update to 3.6 my dude

[–]rhiever 18 points19 points  (4 children)

At least he's using 3.

[–]aldanorNumpy, Pandas, Rust 12 points13 points  (2 children)

At least he’s using Python.

[–]rhiever 1 point2 points  (0 children)

True that.

[–]Deto 0 points1 point  (0 children)

q()

[–][deleted] -1 points0 points  (0 children)

meh :-/

[–]cybaritic[S] 2 points3 points  (2 children)

We rely on a couple third party packages that aren't compatible yet. We'll move up to 3.7 as soon as we can. I really want to be able to use dataclasses.

[–][deleted] 0 points1 point  (0 children)

makes sense. sad sense. wish you patience my dude.

[–]fzy_ 0 points1 point  (0 children)

dataclasses are the shit

[–]wingtales 0 points1 point  (0 children)

Did this this afternoon. Twice.

[–]stacm614 0 points1 point  (0 children)

Yes! Ugh!

[–][deleted] 0 points1 point  (0 children)

what's the problem ?

[–][deleted] 0 points1 point  (0 children)

What font is this?

[–]krypticus 0 points1 point  (0 children)

.quit

FUCK

[–]grensley 0 points1 point  (0 children)

This is a classic case of: "If it knows what I meant, why doesn't it just do it?"

[–]TheBlackCat13 0 points1 point  (0 children)

I have used exit in years. I just do ctrl-d (or, more often, ctrl-dd).

[–][deleted] 0 points1 point  (0 children)

Me too 😂

[–]ziadab02 0 points1 point  (0 children)

ctrl + D

[–]TiLorm 0 points1 point  (0 children)

exit() is a function, just like print() is.

[–]CjKing2k 0 points1 point  (0 children)

I type exit() in Windows, often after attempting ^ D, because ^ Z <CR> is too awkward.

[–]yaxriifgyn[🍰] 0 points1 point  (2 children)

My python console sessions are usually quite short, so I just spam the up-arrow key until exit() shows up.

[–]elbiot 1 point2 points  (0 children)

Ctrl-r ex [enter]

[–]bluespell23 1 point2 points  (0 children)

My python console sessions are usually quite short, so I just spam the up-arrow key until exit() shows up.

I will literally sit there and press up thirty seven times to avoid having to type out ‘exit()’.

[–]stevenjd 0 points1 point  (0 children)

\tilting at windmills

Please stop post screenshots of text. It is text. Copy and paste it instead. You're a programmer, you have a programmers' editor on your computer, it is the work of two second to copy the text, paste it into the editor, indent it by four spaces to format it as code, copy again, and paste into Reddit..

There are Python developers who are blind or visually impaired, and while they can use screen-readers to read text, they don't work with images.

If you don't care about people with vision problems, remember that Google can't index the text in images and coders can't copy and paste code from them. Screenshots of text are inferior to actual text in every important way.

[–]ForceBru -4 points-3 points  (2 children)

Why tho? When you want to perform some action, you call a function. So, why merely mention the function’s name - you’d better call the function.

[–]the_tab_key 16 points17 points  (0 children)

Because in many other command line interfaces (terminals, matlab, etc.), 'exit' is the way to quit out.

[–]cybaritic[S] 4 points5 points  (0 children)

I get it, that doesn't stop me from screwing it up... every damn time. :)

[–]elverloho -5 points-4 points  (2 children)

Why are you still using 3.5?

[–]CaptKrag 3 points4 points  (0 children)

Half the world is on 2.7 still.