Skip to content

Cursor shape is stuck in vim when opening vim from a nushell #10169

Closed

Description

@mb21

Describe the bug

When opening vim (or zsh or bash) from nushell, the cursor stays stuck to whatever it was in the nushell session (block or line cursor). Inside vim, switching between edit- and insert-mode doesn't change the cursor either, which is very disorienting.

I even temporarily removed my ~/.vimrc file to make sure it's not related to that.

How to reproduce

  1. Open Apple Terminal app (I'm on macOS 13.5.1)
  2. nu (if you're not already in it)
  3. vim (I used the macOS-supplied one at /usr/bin/vim)

Observe that cursor is a vertical line (or whatever it was you had nu's cursor configured to). Pressing i puts me into insert mode but the cursor doesn't change.

Expected behavior

I expect a block curstor in vim edit mode. More generally, I expect a program (like zsh or bash) when its opened from inside nu to be able to set its own cursor.

Screenshots

image

Configuration

key value
version 0.84.0
branch
commit_hash
build_os macos-aarch64
build_target aarch64-apple-darwin
rust_version rustc 1.71.1 (eb26296b5 2023-08-03) (built from a source tarball)
cargo_version cargo 1.71.2 (1a737af0c 2023-08-07)
build_time 2023-08-22 21:09:23 +00:00
build_rust_channel release
allocator standard
features dataframe, default, sqlite, trash, which, zip
installed_plugins

Additional context

Interestingly if I enter zsh from inside a nu shell, and then enter vim, the cursor is still broken. But if I enter vim directly from zsh (without going through nu) the cursor is correctly a block cursor.

This seems related to #8452, as I can get it fixed inside a zsh shell inside nu by resetting the cursor with echo -e "\033[2 q"

> nu
> zsh # cursor is still a line cursor here although usually it's a block cursor for me in zsh
> vim # line cursor
> echo -e "\033[2 q"
>
> vim # cursor is block now (both in insert mode and edit mode though)

Finally, If I put the following into my config.nu, the cursor is always a block cursor inside vim as well (in both insert and edit mode):

  edit_mode: vi,
  cursor_shape: {
    vi_insert: block,
    vi_normal: block,
  },

So definitely seems to have something to do with how nu shell sets the cursor. Seems whatever it's doing should be undone before entering vim and before exiting nushell?

Activity

  1. added
    status:needs-triage
    on Aug 31, 2023
  2. fdncred commented on Aug 31, 2023

    @fdncred
    Contributor

    Have you tried changing the cursor_shape in the config file to see if that makes any difference?

      cursor_shape: {
        emacs: underscore # block, underscore, line, blink_block, blink_underscore, blink_line (line is the default)
        vi_insert: block # block, underscore, line , blink_block, blink_underscore, blink_line (block is the default)
        vi_normal: line # block, underscore, line, blink_block, blink_underscore, blink_line (underscore is the default)
      }
  3. mb21 commented on Aug 31, 2023

    @mb21
    Author

    @fdncred thanks for the quick reply! I just updated the post above with something that does fix it. With your snippet, the cursor stays an underline in vim (in both insert and edit mode).

  4. amtoine commented on Aug 31, 2023

    @amtoine
    Member

    @mb21
    is there any remaining issues here? 😋

  5. added
    A:line-editor
    A:configuration
    on Sep 1, 2023
  6. mb21 commented on Sep 4, 2023

    @mb21
    Author

    is there any remaining issues here?

    yes, very much.

    Both configuration options result in the cursor in vim always being the same in insert mode and edit mode, which makes the experience very disorienting.

    It seems whatever nushell sets the cursor to before entering vim, that's the cursor you'll be stuck with in vim. So effectively changing the config doesn't fix anything. Am I making sense?

  7. amtoine commented on Sep 4, 2023

    @amtoine
    Member

    Am I making sense?

    yup i misunderstood your last message, my bad 😌

    i cannot reproduce this in Rio nor Alacritty, even if i use

        cursor_shape: {
            vi_insert: block
            vi_normal: block
        }

    in my config, i see the cursor change when changing mode 😕

    maybe you could try with the latest main branch of Nushell? with the terminal emulator, that's the only other obvious difference i see between our two setups 😋

  8. mb21 commented on Sep 5, 2023

    @mb21
    Author

    I just built from source from latest main and could reproduce there as well. Also, I tried iTerm2 and could reproduce there as well. 🤷🏽‍♂️

  9. mb21 commented on Sep 7, 2023

    @mb21
    Author

    I just tried with Apple Terminal on an older laptop with macOS 12.6.8 and it's the same.

    Pretty sure nushell should "just" restore the cursor before entering vim and before exiting itself...

    @nibon7 did something like that in reedline and that works for me (I added a screen-recording there). That commit seems to have made it into crossterm (crossterm-rs/crossterm#646) but not nushell? Or perhaps it doesn't apply in the case of entering vim?

  10. nibon7 commented on Sep 7, 2023

    @nibon7
    Contributor
    cursor_shape: {
          vi_insert: block
          vi_normal: block
    }
    

    This setting has nothing to do with vim editor, but it does affect the behavior of reedline command line input.

  11. mb21 commented on Sep 7, 2023

    @mb21
    Author

    @nibon7 yes I know. Sorry, didn't mean to @-mention you if you're not a nushell dev. Seems to me currently that both reedline and crossterm do the right thing...

  12. changed the title [-]Line- instead of block cursor in edit-mode in vim[/-] [+]Cursor shape is stuck in vim when opening it from a nushell[/+] on Sep 7, 2023
  13. changed the title [-]Cursor shape is stuck in vim when opening it from a nushell[/-] [+]Cursor shape is stuck in vim when opening vim from a nushell[/+] on Sep 7, 2023
  14. 4 remaining items

  15. nibon7 commented on Sep 8, 2023

    @nibon7
    Contributor

    It seems that vim doesn't set the cursor shape. You can use :echo &t_SI, :echo &t_SR and :echo &t_EI to check whether the values of t_SI, t_SR and t_EI are empty.

    Nushell sets cursor shape by default (line in emacs mode, block in vi insert mode and underline in vi normal mode) and restores previous cursor shape after nushell session ends. This behavior can't be canceled even if you comment out cursor_shape block in $HOME/.config/nushell/config.nu.

  16. mb21 commented on Sep 9, 2023

    @mb21
    Author

    @nibon7 Okay... so how does vim render the cursor then and why does it break when opened in nu? You can reproduce the problem on your machine? also macOS, or?

  17. nibon7 commented on Sep 9, 2023

    @nibon7
    Contributor

    I guess the cursor is rendered in the follow code:
    https://github.com/vim/vim/blob/7bcd25cad3e9d5c9e25c7ae2bde67285c26f73cd/src/term.c#L4309-L4355

    In my gentoo system, whether in nushell or zsh or even bash, the cursor shape in vim editor doesn't change when entering or leaving insert mode with the default config.

  18. mb21 commented on Sep 9, 2023

    @mb21
    Author

    whether in nushell or zsh or even bash, the cursor shape in vim editor doesn't change

    even if you open zsh or bash directly from your teminal emulator, not opening them from inside nushell?!

  19. mb21 commented on Sep 9, 2023

    @mb21
    Author

    It seems that vim doesn't set the cursor shape.

    Are you sure? From the vim code you linked:

    	    if (*T_CSR != NUL)
    		p = T_CSR;	// Replace mode cursor
    	    else
    		p = T_CSI;	// fall back to Insert mode cursor
    	    if (*p != NUL)
    	    {
    		out_str(p);

    With e.g. T_CSR defined here.

    And out_str says:

    Put a character string a byte at a time into the output buffer.

    • If HAVE_TGETENT is defined use tputs(), the termcap parser. (jw)
    • This should only be used for writing terminal codes

    Also, for me this isn't isolated to vim. If I open bash or zsh from inside nushell, the cursor is wrong there as well (wrong meaning it's a line cursor although these shells normally have block cursors for me). In zsh/bash, I can fix it with echo -e "\033[2 q". Actually I can fix it inside vim as well by running :! echo -e "\033[2 q"... but then it's just stuck as a block cursor which doesn't really help me either.

  20. nibon7 commented on Sep 9, 2023

    @nibon7
    Contributor

    even if you open zsh or bash directly from your teminal emulator, not opening them from inside nushell?!

    Yes

  21. mb21 commented on Sep 9, 2023

    @mb21
    Author

    the cursor shape in vim editor doesn't change when entering or leaving insert mode

    Ah, interestingly I have the same behaviour if I change the cursor to 'Vertical Bar' (default is 'Block') in my terminal emulator's preferences (Apple Terminal).

  22. nibon7 commented on Sep 9, 2023

    @nibon7
    Contributor

    Would you mind applying this patch to build nushell and test again?

    diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs
    index 415778448..ab6d788bb 100644
    --- a/crates/nu-cli/src/repl.rs
    +++ b/crates/nu-cli/src/repl.rs
    @@ -268,8 +268,7 @@ pub fn evaluate_repl(
                 )))
                 .with_quick_completions(config.quick_completions)
                 .with_partial_completions(config.partial_completions)
    -            .with_ansi_colors(config.use_ansi_coloring)
    -            .with_cursor_config(cursor_config);
    +            .with_ansi_colors(config.use_ansi_coloring);
             perf(
                 "reedline builder",
                 start_time,
  23. mb21 commented on Sep 9, 2023

    @mb21
    Author

    Sure, I just tried the diff, and now the cursor is always a block cursor (in both nushell edit and insert mode and in vim edit and insert mode) 😅

    Edit: actually it's always the cursor set in the terminal emulator's preferences... if I change that to 'line', it's line everywhere (and doesn't change in insert/edit mode).

  24. mb21 commented on Sep 9, 2023

    @mb21
    Author

    Oh, I just realized that when I open vim in zsh/bash, the cursor is always a block cursor there as well, also in insert mode! Sorry, I was sure I had tested that before.

    So it seems vim (and bash and zsh) just don't alter the terminal cursor on startup... but vim assumes it to be a block cursor? Using vi_insert: block in nushell config indeed gives me the same behaviour I have in bash/zsh... I'll investigate whether I can tweak my vimrc file...

  25. mb21 commented on Sep 9, 2023

    @mb21
    Author

    Okay, adding this to my vimrc actually works:

    :set timeout ttimeoutlen=100 "see https://vi.stackexchange.com/questions/15633
    let &t_ti.="\<Esc>[1 q"
    let &t_SI.="\<Esc>[5 q"
    let &t_EI.="\<Esc>[1 q"
    let &t_te.="\<Esc>[0 q"
    

    So actually there is no issue in nushell, sorry for the stir! I just assumed vim, bash and zsh must be managing their cursors actively, whereas all of them actually don't do anything at all and just carry on working with the cursor the shell has in which they were opened in. That's why changing the cursor shape in my nushell config affected them...

    Not sure whether there should be a warning note somewhere in nushell docs, seeing that nushell is the only one that even in the default config changes the terminal's cursor actively to a line cursor, or whether that default should be reconsidered. But yeah, maybe I'm the only one who is so stupid :P

  26. amtoine commented on Sep 9, 2023

    @amtoine
    Member

    @mb21
    maybe this could be added to the cookbook? 😋

  27. added a commit that references this issue on Sep 9, 2023
  28. added this to the v0.85.0 milestone on Sep 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:configurationA:line-editorstatus:needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions