Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Simplest way to change cursor to indicate vim mode

+1
−0

Question

What is the simplest way to change the cursor to indicate vim mode when using zsh?

Notes

I want it to show as a vertical line in insert mode and a block in normal mode.

This is the current code I am using:

# change cursor shape for different vi modes
cursor_mode() {
    # See https://ttssh2.osdn.jp/manual/4/en/usage/tips/vim.html for cursor shapes
    cursor_block='\e[2 q'
    cursor_beam='\e[6 q'
    function zle-keymap-select {
        if [[ ${KEYMAP} == vicmd ]] ||
               [[ $1 = 'block' ]]; then
            echo -ne $cursor_block
        elif [[ ${KEYMAP} == main ]] ||
                 [[ ${KEYMAP} == viins ]] ||
                 [[ ${KEYMAP} = '' ]] ||
                 [[ $1 = 'beam' ]]; then
            echo -ne $cursor_beam
        fi
    }
    zle-line-init() {
        echo -ne $cursor_beam
    }
    zle -N zle-keymap-select
    zle -N zle-line-init
}
cursor_mode
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Why? (2 comments)

1 answer

+1
−0

You can modify your existing code to use the cursor shape codes for a vertical line (or "I" cursor) and a block cursor instead of the beam cursor shape. Here is an updated version of your code that will show a vertical line cursor in insert mode and a block cursor in normal mode:

# change cursor shape for different vi modes
cursor_mode() {
    # See https://ttssh2.osdn.jp/manual/4/en/usage/tips/vim.html for cursor shapes
    cursor_block='\e[2 q'
    cursor_vertical='\e[3 q'
    function zle-keymap-select {
        if [[ ${KEYMAP} == vicmd ]] ||
               [[ $1 = 'block' ]]; then
            echo -ne $cursor_block
        elif [[ ${KEYMAP} == main ]] ||
                 [[ ${KEYMAP} == viins ]] ||
                 [[ ${KEYMAP} = '' ]] ||
                 [[ $1 = 'vertical' ]]; then
            echo -ne $cursor_vertical
        fi
    }
    zle-line-init() {
        echo -ne $cursor_vertical
    }
    zle -N zle-keymap-select
    zle -N zle-line-init
}

cursor_mode

This updated code sets the cursor_block variable to the code for a block cursor, and the cursor_vertical variable to the code for a vertical line cursor. In the zle-keymap-select function, it checks the current keymap and the argument passed to the function to determine whether to display the block or vertical line cursor. In the zle-line-init function, it sets the initial cursor shape to the vertical line cursor.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »