Home > Blockchain >  Vim - custom "execute selection in bash" keybinding
Vim - custom "execute selection in bash" keybinding

Time:10-17

I am just a beginner at using Vim. My aim is to create custom keybinding do run the selection in bash. For now, I must use the following sequence: Esc :!bash. This becomes a bit troublesome, especially when testing. Is it possible to create some combination instead of repeating it all the time? I have already spent a while searching, but it ended up in something appended to .vimrc, of course,not working:

let mapleader = "_"

nnoremap <Leader>=  <Esc> :!bash

Not even sure what I did here. Any hints?

CodePudding user response:

If you are in command mode (which you are, usually, when you just selected a line), then you don't need the <Esc>, do you? so

noremap _ :!bash<enter>

No need for <esc> here.

If you are in insert mode, then, yes, you need an escape. But then, you need to remap in insert mode

inoremap _ <esc>:!bash<enter>

(But then I fail to see how you could have a selection. That being said, I've been using vi for 30 years now, and in some aspect I am a power user — I've plenty of scripts —, yet, there are plenty of modern usage, that I never get used to. For example, I still use hjkl (well hnei, since I use colemak) instead of arrows. What I mean, is that it is really possible that everybody but me know how to select in insert mode)

But, nevertheless, my point is, either you are in insert mode, and you map with a imap command, and need a <esc>. Or you are in normal mode, and you map with a nmap command, but don't need a <esc>

Plus, I doubt that you want to remap _ in insert mode. I mean, sometimes you really want to type _. You wouldn't sacrifice that character, I suppose. So, my bet is that you want to bind in normal mode (as you are doing). But then, no need for <esc>

  • Related