Displaying last shell command to edit and execute
(1) By jose isaias cabrera (jicman) on 2022-07-29 14:27:07 [link] [source]
Greetings.
I know I have seen this here before, but I did a quick search and I could not find the answer, but imagine this command on the shell:
sqlite> select EACExt, ProjCBLExt from Project_Dashboard
...> WHERE ProjID = 'PR0000020232' AND
...> InsertDate =
...> (SELECT max(InsertDate) from Project_Dashboard where ProjID = 'PR0000020232')
...> ;
Is there a way to call that query back and be able to edit it to send back? I know I can use the up arrow a bunch of times and be able to execute it, and yes, I know that I can use a text editor to do this, but, I am finding myself needing this quicker way. If there is such a thing, please let me know. I tried .help on the shell, but nothing seemed too obvious to me. Thanks.
josé
(2.1) By Stephan Beal (stephan) on 2022-07-29 15:41:49 edited from 2.0 in reply to 1 [link] [source]
Is there a way to call that query back and be able to edit it to send back?
If your sqlite shell is built with readline support, use ctrl-r to enter "reverse search mode" and start typing part of the query, e.g. "CBL".
Readline can do many, many more things which are functions of its own, not sqlite's, and thus aren't documented as part of sqlite. See:
https://tiswww.case.edu/php/chet/readline/rluserman.html
Edit: nevermind, that doesn't work with multi-line inputs in the sqlite shell (but does in bash, so this is apparently a side effect of how multi-line handling is done in the sqlite shell).
(3.1) By Harald Hanche-Olsen (hanche) on 2022-07-29 15:50:22 edited from 3.0 in reply to 1 [source]
I think your problem here is that your command, as entered, is remembered as five distinct lines by readline. That makes it awkward, to say the least.
Assuming you're on a unix system1, if you end each line with ^V ^L (where the ^ character indicates holding the control key down), you will end up with the entire command as a single entity in readline. (You still need the enter key at the end to submit it.) You can then recall and edit it to your heart's content, as a single entity. But if it is very many lines, it is still a bit awkward, as there is no easy way to move the cursor between lines in the command, other than using left and right arrow keys a lot.
Edit: The correct keystroke is ^V^J, not ^V^N. (I was thinking of the escape sequence \n
commonly used to denote a newline.)
- ^ I don't know about windows, maybe it works there too
(4) By Stephan Beal (stephan) on 2022-07-29 15:45:15 in reply to 3.0 [link] [source]
But if it is very many lines, it is still a bit awkward, as there is no easy way to move the cursor between lines in the command, other than using left and right arrow keys a lot.
Slightly faster: esc-b moves backwards one "word" and esc-f moves forward one "word", noting that readline treats underscores as a word boundary by default. Depending on the terminal ctrl-left/right arrow may behave the same.
(6) By Trudge on 2022-07-31 00:24:57 in reply to 4 [link] [source]
On my Mac M1 Alt-Left arrow and Alt-right arrow achieve this.
(5) By jose isaias cabrera (jicman) on 2022-07-29 15:55:53 in reply to 3.0 [link] [source]
I guess EmEditor it is. :-) None of those two fine answers are available for me. Also, sometimes one just types a query to look up a value, but it's just out of the blue. No one is thinking to type something extra because I may just use it again. Maybe Dr. Hipp and the guys could come up with a Shell command recall and editor. Actually, something as simple as a
.history 1
That would display the last command on the shell without the ...> appends. .history 5 will display the last 5 commands. This would allow copy and paste up to the place of edition, and make the changes, and copy the rest of the query, and hit enter. Can you imagine if that was also part of this fine tool? WOW! :-) Thanks. (I know, selfish...)
(7) By MBL (UserMBL) on 2022-07-31 08:11:58 in reply to 1 [link] [source]
At least on Linux command shell BASH I learnt some time ago that using the CTRL+J at each line end will allow entering the command in one piece
but only after the final semicolon ; use the RETURN button to finish input and execute the SQL statement.
That will allow to have the whole command as one line in the command line history.
I tried the same trick on Windows but that did not work.
(8) By Harald Hanche-Olsen (hanche) on 2022-07-31 09:07:26 in reply to 7 [link] [source]
A lone ctrl-J does not work for me with sqlite on the mac out of the box.
However, I can include the following line in ~/.inputrc
to make it work:
"\C-j": "\C-v\C-j"
That assumes, of course, that you have not rebound ctrl-V in some nonstandard way.