In vimscript, how can I chack if the last character before the cursor is a letter or a…
- User
In vimscript, how can I chack if the last character before the cursor is a letter or a number?
- Assistant
In Vimscript, you can check if the last character before the cursor is a letter or a number using a combination of Vim's built-in functions. The idea is to get the character right before the cursor position and then check its properties.
Here’s a simple way to do this:
" Get the current cursor position let cursor_pos = getcurpos()[1:2] " Get the character before the cursor let char_before_cursor = getline(cursor_pos[1])[cursor_pos[2] - 1] " Check if the character is a letter or a number if char_before_cursor =~# '\v\w' " It is a letter or number echo "The last character before the cursor is a letter or a number." else " It is not a letter or number echo "The last character before the cursor is not a letter or a number." endifExplanation:
- : Retrieves the current cursor position. The returned list contains line number and column number, among other details.
- User
Sorry I meant the character under the cursor
- Assistant
No problem! To check if the character under the cursor is a letter or a number in Vimscript, you can use a similar approach. You just need to retrieve the character at the cursor position instead of the one before it.
Here's how you can do that:
" Get the current cursor position let cursor_pos = getcurpos()[1:2] " Get the character under the cursor let char_under_cursor = getline(cursor_pos[1])[cursor_pos[2] - 1] " Check if the character is a letter or a number if char_under_cursor =~# '\v\w' " It is a letter or number echo "The character under the cursor is a letter or a number." else " It is not a letter or number echo "The character under the cursor is not a letter or a number." endifExplanation:
getcurpos(): This function gets the current line and column position of the cursor.
- User
It throws an "list Index out of range: 2" error after the second line
- Assistant
The "list index out of range" error likely occurs because the cursor's column position might be at the very start of the line, making
cursor_pos[2] - 1an invalid index. To avoid this issue, you can add a check to ensure that the cursor is not at the beginning of the line.Here's a modified version of the script that includes that check:
" Get the current cursor position let cursor_pos = getcurpos()[1:2] " Get the line content at the current cursor's line number let line_content = getline(cursor_pos[1]) " Check if the cursor is at the beginning of the line if cursor_pos[2] > 1 " Get the character under the cursor let char_under_cursor = line_content[cursor_pos[2] - 1] " Check if the character is a letter or a number if char_under_cursor =~# '\v\w' " It is a letter or number echo "The character under the cursor is a letter or a number." else " It is not a letter or number echo "The character under the cursor is not a letter or a number." endif else echo "The cursor is at the beginning of the line." endif