Skip to content

Conversation

Mytherin
Copy link
Collaborator

@Mytherin Mytherin commented Mar 31, 2024

This PR adds support for using the .edit (or \e for short) commands to open an editor to either (a) edit the previous history entry, or (b) edit the current buffer. The editor that is opened is by default vi, but can be configured using either the DUCKDB_EDITOR, EDITOR or VISUAL environment variables (in that order). When the command is used, the specified editor is opened with the current contents of the edit buffer. If the editor closes successfully (e.g. :wq in vim) the edit buffer is replaced with the contents of the file and any queries within the file are executed. If the editor closes unsuccessfully (e.g. :q! in vim) the previous buffer is kept around.

Example usage:

D .edit -- opens an editor containing the previous history entry
D SELECT *
  FROM tbl
  .edit -- opens an editor containing the above text

@vlulla
Copy link

vlulla commented Apr 1, 2024

I tried using the .edit command and it did not load the editor with the previous query. Also, it appears that the query is not executed when the query is complete. I.e., i have to hit enter after the query gets transferred to the cli (after :wq). This is different from \e behavior of psql. This is the paragraph on psql's documentation describing behavior of \e

If you edit a file or the previous query, and you quit the editor without modifying the file, the query buffer is cleared. Otherwise, the new contents of the query buffer are re-parsed according to the normal rules of psql, treating the whole buffer as a single line. Any complete queries are immediately executed; that is, if the query buffer contains or ends with a semicolon, everything up to that point is executed and removed from the query buffer. Whatever remains in the query buffer is redisplayed. Type semicolon or \g to send it, or \r to cancel it by clearing the query buffer.

Anyways, many thanks for the prompt work on this feature. I'm excited to start using it as soon as it arrives in the cli!

@github-actions github-actions bot marked this pull request as draft April 1, 2024 13:36
@Mytherin
Copy link
Collaborator Author

Mytherin commented Apr 1, 2024

Thanks for trying it out - I've addressed your comments and I think it should behave similarly now, could you take another look?

@Mytherin Mytherin marked this pull request as ready for review April 1, 2024 13:36
@vlulla
Copy link

vlulla commented Apr 1, 2024

I tried it and it works swimmingly! Thanks for the quick turnaround!

@Mytherin Mytherin merged commit df19560 into duckdb:main Apr 2, 2024
github-actions bot pushed a commit to duckdb/duckdb-r that referenced this pull request Apr 2, 2024
Merge pull request duckdb/duckdb#11456 from bodand/main
Merge pull request duckdb/duckdb#11447 from Mytherin/editshell
Merge pull request duckdb/duckdb#11452 from Mytherin/windowsunicoderead
@Mytherin Mytherin deleted the editshell branch June 7, 2024 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants