[vim/vim] Can't automatically close the command line window via CmdwinEnter event (#4980)

59 views
Skip to first unread message

George Brown

unread,
Sep 25, 2019, 5:26:21 AM9/25/19
to vim/vim, Subscribed

Describe the bug

I was trying to prevent the command line window from opening without the use of <NOP> mappings. It seems I should simply be able to quit it upon CmdwinEnter, but this does not appear to be the case.

To Reproduce

The following leaves the command line window open.

$ cat test.vim 
autocmd CmdwinEnter * quit
call feedkeys('q:')
$ vim -u NONE -S test.vim

Expected behavior

For the command line window to be closed by the autocmd.

Environment (please complete the following information):

  • Vim version: v8.1.2072
  • OS: macOS 10.14
  • Terminal: Apple Terminal


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub

cs09736

unread,
Sep 30, 2019, 3:07:42 AM9/30/19
to vim/vim, Subscribed

If you're trying to quit vim right after it is entered, you will have to get the script to type ":q" and then press the "Enter" key.

If I understand what you are trying to do correctly then in the feedkeys call, have the argument as ":q\<CR>" using double quotes instead of single quotes.

Also note the change from q: to :q which I assumed was just a typo.
The \<CR> part is for Carraige Return, which simulates the Enter key.

autocmd CmdwinEnter * quit
call feedkeys(":q\<CR>")

This is what I changed the "test.vim" file to and it seemed to do what you were trying to achieve.

George Brown

unread,
Sep 30, 2019, 4:00:14 AM9/30/19
to vim/vim, Subscribed

I'm trying to quit the command line window, not Vim.

Also note the change from q: to :q which I assumed was just a typo.

No this is just automating the opening of the command line window in my example.

Christian Brabandt

unread,
Sep 30, 2019, 4:27:01 AM9/30/19
to vim/vim, Subscribed

Yes, I see the problem. Try this patch please:

diff --git a/src/ex_getln.c b/src/ex_getln.c
index 2a601e1ea..9adb2de7c 100644
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -4163,11 +4163,13 @@ open_cmdwin(void)
     invalidate_botline();
     redraw_later(SOME_VALID);

-    /* No Ex mode here! */
+    // No Ex mode here!
     exmode_active = 0;

     State = NORMAL;
     setmouse();
+    // could be set by CmdWinEnter autocommand
+    cmdwin_result = 0;

     // Trigger CmdwinEnter autocommands.
     trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
@@ -4177,10 +4179,7 @@ open_cmdwin(void)
     i = RedrawingDisabled;
     RedrawingDisabled = 0;

-    /*
-     * Call the main loop until <CR> or CTRL-C is typed.
-     */
-    cmdwin_result = 0;
+    // Call the main loop until <CR> or CTRL-C is typed.
     main_loop(TRUE, FALSE);

     RedrawingDisabled = i;

Christian Brabandt

unread,
Sep 30, 2019, 4:30:55 AM9/30/19
to vim/vim, Subscribed

BTW: as a workaround you can use :au CmdWinEnter * :call feedkeys(":q\<cr>")

George Brown

unread,
Sep 30, 2019, 4:55:44 AM9/30/19
to vim/vim, Subscribed

Thanks @chrisbra! I can confirm that patch gives me the expected behavior, as does your workaround example.

Bram Moolenaar

unread,
Sep 30, 2019, 3:41:01 PM9/30/19
to vim/vim, Subscribed

Thanks for the fix. Can this be tested?

Christian Brabandt

unread,
Oct 1, 2019, 4:47:29 AM10/1/19
to vim/vim, Subscribed

Can this be tested?

Yes, I wondered about this and wasn't quite sure. I don't want to write a screentest, since this takes to long. Perhaps something simple, like setting a buffer local variable and testing whether that one exists? Also the buftype option could be used. I'll go with this approach.

Christian Brabandt

unread,
Oct 1, 2019, 9:14:33 AM10/1/19
to vim/vim, Subscribed

Okay, getting the test right was harder than expected. How about this patch:

diff --git a/src/testdir/test_autocmd.vim b/src/testdir/test_autocmd.vim
index f2c9af50f..f25f65de4 100644
--- a/src/testdir/test_autocmd.vim
+++ b/src/testdir/test_autocmd.vim
@@ -2259,3 +2259,32 @@ func Test_autocmd_SafeState()
   call StopVimInTerminal(buf)
   call delete('XSafeState')
 endfunc
+
+func! Test_autocmd_CmdWinEnter()
+  CheckRunVimInTerminal
+  " There is not cmdwin switch, so
+  " test for cmdline_hist
+  " (both are available with small builds)
+  CheckFeature cmdline_hist
+  let lines =<< trim END
+    let b:dummy_var = 'This is a dummy'
+    autocmd CmdWinEnter * quit
+    let winnr = winnr('$')
+  END
+  let filename='XCmdWinEnter'
+  call writefile(lines, filename)
+  let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
+
+  call term_sendkeys(buf, "q:")
+  call term_wait(buf)
+  call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
+  call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 1000)
+  call term_sendkeys(buf, ":echo &buftype\<cr>")
+  call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
+  call term_sendkeys(buf, ":echo winnr\<cr>")
+  call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
+
+  " clean up
+  call StopVimInTerminal(buf)
+  call delete(filename)
+endfunc

Bram Moolenaar

unread,
Oct 1, 2019, 11:39:38 AM10/1/19
to vim/vim, Subscribed

Closed #4980 via 23324a0.

Bram Moolenaar

unread,
Oct 1, 2019, 11:41:00 AM10/1/19
to vim/vim, Subscribed

Christian wrote:

> Okay, getting the test right was harder than expected. How about this patch:

Thanks, I checked the test fails before the fix.

--
FATHER: You only killed the bride's father - that's all -
LAUNCELOT: Oh dear, I didn't really mean to...
FATHER: Didn't mean to? You put your sword right through his head!
LAUNCELOT: Gosh - Is he all right?
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

MarkthePro

unread,
Sep 12, 2023, 8:43:29 AM9/12/23
to vim/vim, Subscribed

Your code appears to be a combination of a test script for Vim called test_autocmd.vim and a section of the source code for Vim called ex_getln.c. It appears to be associated with testing and autocmd (autocommand) capability in Vim. Its pretty helpful though. :)


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/4980/1715652217@github.com>

Reply all
Reply to author
Forward
0 new messages