var MODES = {
    normal: Symbol('normal'),
    insert: Symbol('insert'),
};
var mode = MODES.normal;

let emit = x => self.port.emit('fvim:event', x);

var CMDS = {
    scrollTop: () => window.scrollTo(0, 0),
    scrollBottom: () => window.scrollTo(0, window.scrollMaxY),
    scrollDown: () => window.scrollByLines(1),
    scrollUp: () => window.scrollByLines(-1),
    scrollLeft: () => window.scollBy(-10, 0),
    scrollRight: () => window.scollBy(10, 0),
    yankUrl: () => emit({ cmd: 'yankUrl', param: window.location.href }),
    newTab: () => emit({ cmd: 'newTab' }),
    openInNewTab: () => emit({ cmd: 'openInNewTab' }),
    setInsertMode: () => mode = MODES.insert,
};
let fG = (ev) => {
    if (mode === MODES.insert) {
        if (ev.key === "Escape") {
            mode = MODES.normal;
        } else {
            return;
        }
    }
    ev.preventDefault();
    ev.stopPropagation();
    switch (ev.key) {
    case 'g': CMDS.scrollTop();
        break;
    case 'G': CMDS.scrollBottom();
        break;
    case 'j': CMDS.scrollDown();
        break;
    case 'k': CMDS.scrollUp();
        break;
    case 'h': CMDS.scrollLeft();
        break;
    case 'l': CMDS.scrollRight();
        break;
    case 'y': CMDS.yankUrl();
        break;
    case 't': CMDS.newTab();
        break;
    case 'P': CMDS.openInNewTab();
        break;
    case 'i': CMDS.setInsertMode();
        break;
    }
};
addEventListener('keydown', fG, true);

@@ -67,16 +67,42 @@
 sert'),%0A
+    auto: Symbol('auto'),%0A
 %7D;%0Avar m
@@ -113,22 +113,20 @@
 = MODES.
-normal
+auto
 ;%0A%0Alet e
@@ -170,16 +170,143 @@
 ', x);%0A%0A
+let editable = x =%3E%0A    x.tagName === 'INPUT' %7C%7C%0A    x.tagName === 'TEXTAREA' %7C%7C%0A    x.getAttribute('g_editable') === 'true';%0A%0A
 var CMDS
@@ -846,45 +846,8 @@
 %3E %7B%0A
-    if (mode === MODES.insert) %7B%0A    
     
@@ -885,79 +885,176 @@
     
-   
+mode =
  mode =
- MODES.normal
+== MODES.insert ? MODES.normal : MODES.auto
 ;%0A    
-    %7D else %7B%0A            return;%0A        %7D
+%7D%0A    switch (mode) %7B%0A    case MODES.insert: return;%0A    case MODES.auto: if (editable(ev.target)) return;
 %0A   

add atuo mode with some insert detection