Thread: Confusing behavior of psql's \e
If you quit the editor without saving, the current query buffer or the last executed SQL statement get run. This can be annoying and disruptive, and it requires you to empty the file and save it if you don't want to execute anything. But when editing a script, it is a clear POLA violation: test=> \! cat q.sql SELECT 99; test=> SELECT 42; ?column? ---------- 42 (1 row) test=> \e q.sql [quit the editor without saving] ?column? ---------- 42 (1 row) This is pretty bad: you either have to re-run the previous statement or you have to empty your script file. Both are unappealing. I have been annoyed about this myself, and I have heard other prople complain about it, so I propose to clear the query buffer if the editor exits without modifying the file. This behavior is much more intuitive for me. Yours, Laurenz Albe
Attachment
On 11/30/20 22:38, Laurenz Albe wrote: > I have been annoyed about this myself, and I have heard other prople > complain about it, so I propose to clear the query buffer if the > editor exits without modifying the file. Or how about keeping the query buffer content unchanged, but not executing it? Then it's still there if you have another idea about editing it. It's easy enough to \r if you really want it gone. One downside I can foresee is that I could form a habit of doing something that would be safe in psql version x but would bite me one day if I'm in psql version x-- for some reason. Regards, -Chap
On Tue, 2020-12-01 at 11:03 -0500, Chapman Flack wrote: > On 11/30/20 22:38, Laurenz Albe wrote: > > I have been annoyed about this myself, and I have heard other prople > > complain about it, so I propose to clear the query buffer if the > > editor exits without modifying the file. > > Or how about keeping the query buffer content unchanged, but not > executing it? Then it's still there if you have another idea about > editing it. It's easy enough to \r if you really want it gone. What if the buffer was empty? Would you want to get the previous query in the query buffer? I'd assume not... Yours, Laurenz Albe
On 12/01/20 11:21, Laurenz Albe wrote: > On Tue, 2020-12-01 at 11:03 -0500, Chapman Flack wrote: >>> complain about it, so I propose to clear the query buffer if the >>> editor exits without modifying the file. >> >> Or how about keeping the query buffer content unchanged, but not >> executing it? Then it's still there if you have another idea about >> editing it. It's easy enough to \r if you really want it gone. > > What if the buffer was empty? Would you want to get the previous > query in the query buffer? I'd assume not... I took your proposal to be specifically about what happens if the editor is exited with no change to the buffer, and in that case, I would suggest making no change to the buffer, but not re-executing it. If the editor is exited after deliberately emptying the editor buffer, I would expect that to be treated as emptying the query buffer. I don't foresee any case that would entail bringing a /previous/ query back into the query buffer. Regards, -Chap
On Tue, 2020-12-01 at 11:34 -0500, Chapman Flack wrote: > On 12/01/20 11:21, Laurenz Albe wrote: > > On Tue, 2020-12-01 at 11:03 -0500, Chapman Flack wrote: > > > > I propose to clear the query buffer if the > > > > editor exits without modifying the file. > > > > > > Or how about keeping the query buffer content unchanged, but not > > > executing it? Then it's still there if you have another idea about > > > editing it. It's easy enough to \r if you really want it gone. > > > > What if the buffer was empty? Would you want to get the previous > > query in the query buffer? I'd assume not... > > I took your proposal to be specifically about what happens if the editor > is exited with no change to the buffer, and in that case, I would suggest > making no change to the buffer, but not re-executing it. > > If the editor is exited after deliberately emptying the editor buffer, > I would expect that to be treated as emptying the query buffer. > > I don't foresee any case that would entail bringing a /previous/ query > back into the query buffer. I see I'll have to try harder. The attached patch changes the behavior as follows: - If the current query buffer is edited, and you quit the editor, the query buffer is retained. This is as it used to be. - If the query buffer is empty and you run \e, the previous query is edited (as it used to be), but quitting the editor will empty the query buffer and execute nothing. - Similarly, if you "\e file" and quit the editor, nothing will be executed and the query buffer is emptied. - The same behavior change applies to \ef and \ev. There is no need to retain the definition in the query buffer, you can always run the \ev or \ev again. I consider this a bug fix, but one that shouldn't be backpatched. Re-executing the previous query if the editor is quit is annoying at least and dangerous at worst. I'll add this patch to the next commitfest. Yours, Laurenz Albe
Attachment
Laurenz Albe <laurenz.albe@cybertec.at> writes: > Attached is version 6. Pushed with some mostly-cosmetic fiddling. One thing I changed that wasn't cosmetic is that as you had it, the behavior of "\e file" varied depending on whether the query buffer had been empty, which surely seems like a bad idea. I made it do discard_on_quit always in that case. I think there might be a case for discard_on_quit = false always, ie maybe the user wanted to load the file into the query buffer as-is. But it seems like a pretty weak case --- you'd be more likely to just use \i for that situation. regards, tom lane
On Sat, 2021-04-03 at 17:43 -0400, Tom Lane wrote: > Laurenz Albe <laurenz.albe@cybertec.at> writes: > > Attached is version 6. > > Pushed with some mostly-cosmetic fiddling. > > One thing I changed that wasn't cosmetic is that as you had it, > the behavior of "\e file" varied depending on whether the query > buffer had been empty, which surely seems like a bad idea. > I made it do discard_on_quit always in that case. I think there > might be a case for discard_on_quit = false always, ie maybe > the user wanted to load the file into the query buffer as-is. > But it seems like a pretty weak case --- you'd be more likely > to just use \i for that situation. Thanks for that! Yours, Laurenz Albe