From 92936d7bd13c6eb38612548047ade5844cf525cb Mon Sep 17 00:00:00 2001 From: superle3 <987654321.yo.na.than@gmail.com> Date: Sun, 20 Jul 2025 16:53:31 +0200 Subject: [PATCH] feat: implement select mode Implements #239 There are some usefull keybindings in insert mode while having something selected, for example surrounding the current selection with (), but selecting the right text is easier in visual mode than using a mouse or arrow keys. Vim also a select mode at https://vimhelp.org/visual.txt.html#Select, this only implements switching between visual and insert mode while keeping the selection and doesn't keep the selection mode. --- src/vim.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/vim.js b/src/vim.js index 237858b..84d98d7 100644 --- a/src/vim.js +++ b/src/vim.js @@ -216,6 +216,8 @@ export function initVim(CM) { { keys: 'V', type: 'action', action: 'toggleVisualMode', actionArgs: { linewise: true }}, { keys: '', type: 'action', action: 'toggleVisualMode', actionArgs: { blockwise: true }}, { keys: '', type: 'action', action: 'toggleVisualMode', actionArgs: { blockwise: true }}, + { keys: '', type: 'action', action: 'toggleSelectMode', context: 'visual'}, + { keys: '', type: 'action', action: 'toggleSelectMode', context: 'insert'}, { keys: 'gv', type: 'action', action: 'reselectLastSelection' }, { keys: 'J', type: 'action', action: 'joinLines', isEdit: true }, { keys: 'gJ', type: 'action', action: 'joinLines', actionArgs: { keepSpaces: true }, isEdit: true }, @@ -3152,6 +3154,22 @@ export function initVim(CM) { exitVisualMode(cm); } }, + toggleSelectMode: function(cm, actionArgs, vim) { + const selections = cm.listSelections(); + const has_selection = selections.some( + (s) => s.anchor.line !== s.head.line || s.anchor.ch !== s.head.ch + ); + if (!has_selection) { + return; + } + if (vim.insertMode) { + exitInsertMode(cm); + cm.setSelections(selections); + } else if (vim.visualMode) { + this.enterInsertMode(cm, {repeat: actionArgs.repeat}, vim); + cm.setSelections(selections); + } + }, reselectLastSelection: function(cm, _actionArgs, vim) { var lastSelection = vim.lastSelection; if (vim.visualMode) {