Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mb21 opened this issue Aug 31, 2023 · 26 comments
Closed

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

mb21 opened this issue Aug 31, 2023 · 26 comments
Labels
configuration Issue related to nu's configuration line editor Issues related to reedline needs-triage An issue that hasn't had any proper look
Milestone

Comments

@mb21
Copy link

mb21 commented Aug 31, 2023

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?

@mb21 mb21 added the needs-triage An issue that hasn't had any proper look label Aug 31, 2023
@fdncred
Copy link
Collaborator

fdncred commented Aug 31, 2023

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)
  }

@mb21
Copy link
Author

mb21 commented Aug 31, 2023

@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).

@amtoine
Copy link
Member

amtoine commented Aug 31, 2023

@mb21
is there any remaining issues here? 😋

@sholderbach sholderbach added line editor Issues related to reedline configuration Issue related to nu's configuration labels Sep 1, 2023
@mb21
Copy link
Author

mb21 commented Sep 4, 2023

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?

@amtoine
Copy link
Member

amtoine commented Sep 4, 2023

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 😋

@mb21
Copy link
Author

mb21 commented Sep 5, 2023

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

@mb21
Copy link
Author

mb21 commented Sep 7, 2023

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?

@nibon7
Copy link
Contributor

nibon7 commented Sep 7, 2023

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.

@mb21
Copy link
Author

mb21 commented Sep 7, 2023

@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...

@nibon7
Copy link
Contributor

nibon7 commented Sep 7, 2023

@mb21 mb21 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 Sep 7, 2023
@mb21 mb21 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 Sep 7, 2023
@mb21
Copy link
Author

mb21 commented Sep 7, 2023

@nibon7 thanks, but that's not it, it's definitely a bug in nushell. Since amtoine above already was confused as well, I've updated this issue's title and initial description to make it clearer what the problem is (now that I understand it better). Is it clearer now?

@nibon7
Copy link
Contributor

nibon7 commented Sep 7, 2023

Thanks, I get it now :)

@fdncred
Copy link
Collaborator

fdncred commented Sep 7, 2023

What's the fix?

@nibon7
Copy link
Contributor

nibon7 commented Sep 7, 2023

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.

@nibon7
Copy link
Contributor

nibon7 commented Sep 8, 2023

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.

@mb21
Copy link
Author

mb21 commented Sep 9, 2023

@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?

@nibon7
Copy link
Contributor

nibon7 commented Sep 9, 2023

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.

@mb21
Copy link
Author

mb21 commented Sep 9, 2023

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?!

@mb21
Copy link
Author

mb21 commented Sep 9, 2023

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.

@nibon7
Copy link
Contributor

nibon7 commented Sep 9, 2023

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

Yes

@mb21
Copy link
Author

mb21 commented Sep 9, 2023

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).

@nibon7
Copy link
Contributor

nibon7 commented Sep 9, 2023

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,

@mb21
Copy link
Author

mb21 commented Sep 9, 2023

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).

@mb21
Copy link
Author

mb21 commented Sep 9, 2023

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...

@mb21
Copy link
Author

mb21 commented Sep 9, 2023

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

@amtoine
Copy link
Member

amtoine commented Sep 9, 2023

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

@mb21 mb21 closed this as completed Sep 9, 2023
fdncred pushed a commit that referenced this issue Sep 9, 2023
# Description
There are several cursor shape related issues #7151 #9243 #7271 #8452
#10169, you can't disable the cursor shape feature even if you comment
out the entire `cursor_shape` block in the config.nu, and even worse,
when nushell exits with an error, the cursor shape can't be restored,
that is annoying.

This PR provides an opportunity to disable setting the cursor shape.

# User-Facing Changes
If you use the default config.nu, nothing changes, but if you comment
out `cursor_shape` block or set them to `inherit`, related cursor shape
will not be set.

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
@hustcer hustcer added this to the v0.85.0 milestone Sep 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
configuration Issue related to nu's configuration line editor Issues related to reedline needs-triage An issue that hasn't had any proper look
Projects
None yet
Development

No branches or pull requests

6 participants