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

VISUAL with vim shortcuts #44

Open
mscott99 opened this issue Jul 26, 2022 · 4 comments
Open

VISUAL with vim shortcuts #44

mscott99 opened this issue Jul 26, 2022 · 4 comments

Comments

@mscott99
Copy link

I like the VISUAL idea for editing previously written latex. However it is easier to select text while in vim mode. Would there be a way to use the selection performed in vim mode?

I am not sure of what exactly this would look like.

Best,

Matthew

@artisticat1
Copy link
Owner

Yep, this is definitely possible. I'll try to fix this in one of the next releases.

@rwang2022
Copy link

I think it's a great idea. Are there any plans to implement this soon?

The issue, I think comes from the fact that vim doesn't really support doing a visual selection then going into insert mode (without losing the selection), so right now you can only select normally using mouse or shift+arrow keys. Not exactly sure what a workaround would look like.

Best,
Ryan

@superle3
Copy link
Contributor

its possible using the vimrc plugin (https://github.com/esm7/obsidian-vimrc-support/tree/master) in a kinda hacky way.
by adding

exmap test jscommand {const sel = selection; editor.cm.cm.state.vim.insertMode = true; editor.setSelection(sel.anchor, sel.head);}
vmap <C-o> :test<CR>

to your vimrc you should be able to use the visual snippets.
This does hide the cursor, but I couldn't figure out from the docs where the actual vim functions are in the editor object.
it would probably be easier to add 2 commands/shortcuts to switch between insert select mode and visual select mode in the obsidian plugin itself

@superle3
Copy link
Contributor

adding

this.addcommand({
	// this adds the select mode from vim so visual snippets can be triggered with vim visual selection
	id: "switch-to-select-mode",
	name: "Switch from visual mode to select mode",
	editorCheckCallback: (checking: boolean, editor: Editor) => {
		let vimObject = (window as any).CodeMirrorAdapter?.Vim;
		if (
			!(this.app as any).isVimEnabled() ||
			!editor.cm.cm.state.vim?.visualMode
		) {
			return false;
		} else if (!checking) {
			let selection: EditorSelection = editor.listSelections();
			vimObject.handleKey(editor.cm.cm, "<Esc>");
			vimObject.handleKey(editor.cm.cm, "i");
			editor.setSelections(selection);
		} else {
			return true;
		}
	},
});

would add select mode as an command. you can go back to visual with (vim mapping).
This does work with multicursor. this doesn't give an indirect mapping sadly, cause : in visual -> :'<,'>
or for only vimrc

const sel = selection;
const vimObj = window.CodeMirrorAdapter?.Vim;
const cm = editor.cm.cm;
if (vimObj) {
    vimObj.handleKey(cm, "<Esc>");
    vimObj.handleKey(cm, "i");
    editor.setSelections(sel);
}

with the vimrc, but I couldn't get editor.listSelections() to work so this doesn't work for multicursor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants