Skip to content

Commit 75160cb

Browse files
committed
Show how to use an external editor from SQL client
1 parent 7e3f521 commit 75160cb

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

v2.1/use-the-built-in-sql-client.md

+25
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,31 @@ In this example, we create a table and then use `\|` to programmatically insert
579579
+---+
580580
~~~
581581

582+
### Edit SQL statements in an external editor
583+
584+
In applications that use [GNU Readline](https://tiswww.case.edu/php/chet/readline/rltop.html) (such as [bash](https://www.gnu.org/software/bash/)), you can edit a long line in your preferred editor by typing `Ctrl-x Ctrl-e`. However, CockroachDB uses the BSD-licensed [libedit](https://thrysoee.dk/editline/), which does not include this functionality.
585+
586+
If you would like to be able to edit the current line in an external editor by typing `C-x C-e` as in `bash`, do the following:
587+
588+
1. Install the `vipe` program (from the [moreutils](https://joeyh.name/code/moreutils/) suite of tools).
589+
2. Edit your `~/.editrc` to add the following line, which takes advantage of the SQL client's ability to [run external commands](#run-external-commands-from-the-sql-shell):
590+
591+
{% include copy-clipboard.html %}
592+
~~~
593+
cockroach:bind -s ^X^E '^A^K\\\| echo \"^Y\" | vipe\r'
594+
~~~
595+
596+
This tells libedit to translate `C-x C-e` into the following commands:
597+
598+
1. Move to the beginning of the current line.
599+
2. Cut the whole line.
600+
3. Paste the line into your editor via `vipe`.
601+
4. Pass the edited file back to the SQL client when `vipe` exits.
602+
603+
{{site.data.alerts.callout_info}}
604+
Future versions of the SQL client may opt to use a different back-end for reading input, in which case please refer to this page for additional updates.
605+
{{site.data.alerts.end}}
606+
582607
### Allow potentially unsafe SQL statements
583608
584609
The `--safe-updates` flag defaults to `true`. This prevents SQL statements that may have broad, undesired side-effects. For example, by default, we cannot use `DELETE` without a `WHERE` clause to delete all rows from a table:

0 commit comments

Comments
 (0)