@@ -146,6 +146,17 @@ def notify_callback(notify: Notify):
146146 )
147147
148148
149+ def get_editor ():
150+ """Pick the external editor for ``\\ e``/``\\ ev``/``\\ ef``/``\\ ne``.
151+
152+ Mirrors psql, which checks ``PSQL_EDITOR`` first, then ``EDITOR``, then
153+ ``VISUAL``. Returning ``None`` when none are set lets click fall back to
154+ its platform default, so the behaviour is unchanged for anyone who wasn't
155+ setting ``PSQL_EDITOR``.
156+ """
157+ return os .environ .get ("PSQL_EDITOR" ) or os .environ .get ("EDITOR" ) or os .environ .get ("VISUAL" ) or None
158+
159+
149160class PGCli :
150161 default_prompt = "\\ u@\\ h:\\ d> "
151162 max_len_prompt = 30
@@ -331,7 +342,7 @@ def edit_named_query(self, pattern, **_):
331342 return [(None , None , None , "Usage: \\ ne <name>" )]
332343
333344 existing = NamedQueries .instance .get (name )
334- sql , message = special .open_external_editor (sql = existing or "" )
345+ sql , message = special .open_external_editor (sql = existing or "" , editor = get_editor () )
335346 if message :
336347 return [(None , None , None , message )]
337348
@@ -832,7 +843,7 @@ def handle_editor_command(self, text):
832843 query = self .pgexecute .view_definition (spec )
833844 elif editor_command == "\\ ef" :
834845 query = self .pgexecute .function_definition (spec )
835- sql , message = special .open_external_editor (filename , sql = query )
846+ sql , message = special .open_external_editor (filename , sql = query , editor = get_editor () )
836847 if message :
837848 # Something went wrong. Raise an exception and bail.
838849 raise RuntimeError (message )
0 commit comments