Python prompt_toolkit.enums.EditingMode.VI Examples

The following are 30 code examples of prompt_toolkit.enums.EditingMode.VI(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module prompt_toolkit.enums.EditingMode , or try the search function .
Example #1
Source File: test_cli.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_vi_digraphs():
    feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI)

    # C-K o/
    result, cli = feed("hello\x0bo/\r")
    assert result.text == "helloø"

    # C-K /o  (reversed input.)
    result, cli = feed("hello\x0b/o\r")
    assert result.text == "helloø"

    # C-K e:
    result, cli = feed("hello\x0be:\r")
    assert result.text == "helloë"

    # C-K xxy (Unknown digraph.)
    result, cli = feed("hello\x0bxxy\r")
    assert result.text == "helloy" 
Example #2
Source File: test_cli.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_vi_text_objects():
    feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI)

    # Esc gUgg
    result, cli = feed("hello\x1bgUgg\r")
    assert result.text == "HELLO"

    # Esc gUU
    result, cli = feed("hello\x1bgUU\r")
    assert result.text == "HELLO"

    # Esc di(
    result, cli = feed("before(inside)after\x1b8hdi(\r")
    assert result.text == "before()after"

    # Esc di[
    result, cli = feed("before[inside]after\x1b8hdi[\r")
    assert result.text == "before[]after"

    # Esc da(
    result, cli = feed("before(inside)after\x1b8hda(\r")
    assert result.text == "beforeafter" 
Example #3
Source File: test_cli.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_vi_character_delete_after_cursor():
    " Test 'x' keypress. "
    feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI, multiline=True)

    # Delete one character.
    result, cli = feed("abcd\x1bHx\r")
    assert result.text == "bcd"

    # Delete multiple character.s
    result, cli = feed("abcd\x1bH3x\r")
    assert result.text == "d"

    # Delete on empty line.
    result, cli = feed("\x1bo\x1bo\x1bggx\r")
    assert result.text == "\n\n"

    # Delete multiple on empty line.
    result, cli = feed("\x1bo\x1bo\x1bgg10x\r")
    assert result.text == "\n\n"

    # Delete multiple on empty line.
    result, cli = feed("hello\x1bo\x1bo\x1bgg3x\r")
    assert result.text == "lo\n\n" 
Example #4
Source File: test_cli.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_vi_character_delete_before_cursor():
    " Test 'X' keypress. "
    feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI, multiline=True)

    # Delete one character.
    result, cli = feed("abcd\x1bX\r")
    assert result.text == "abd"

    # Delete multiple character.
    result, cli = feed("hello world\x1b3X\r")
    assert result.text == "hello wd"

    # Delete multiple character on multiple lines.
    result, cli = feed("hello\x1boworld\x1bgg$3X\r")
    assert result.text == "ho\nworld"

    result, cli = feed("hello\x1boworld\x1b100X\r")
    assert result.text == "hello\nd"

    # Delete on empty line.
    result, cli = feed("\x1bo\x1bo\x1b10X\r")
    assert result.text == "\n\n" 
Example #5
Source File: switch-between-vi-emacs.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run():
    # Create a `KeyBindings` that contains the default key bindings.
    bindings = KeyBindings()

    # Add an additional key binding for toggling this flag.
    @bindings.add("f4")
    def _(event):
        " Toggle between Emacs and Vi mode. "
        if event.app.editing_mode == EditingMode.VI:
            event.app.editing_mode = EditingMode.EMACS
        else:
            event.app.editing_mode = EditingMode.VI

    def bottom_toolbar():
        " Display the current input mode. "
        if get_app().editing_mode == EditingMode.VI:
            return " [F4] Vi "
        else:
            return " [F4] Emacs "

    prompt("> ", key_bindings=bindings, bottom_toolbar=bottom_toolbar) 
Example #6
Source File: app.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def vi_navigation_mode() -> bool:
    """
    Active when the set for Vi navigation key bindings are active.
    """
    from prompt_toolkit.key_binding.vi_state import InputMode

    app = get_app()

    if (
        app.editing_mode != EditingMode.VI
        or app.vi_state.operator_func
        or app.vi_state.waiting_for_digraph
        or app.current_buffer.selection_state
    ):
        return False

    return (
        app.vi_state.input_mode == InputMode.NAVIGATION
        or app.vi_state.temporary_navigation_mode
        or app.current_buffer.read_only()
    ) 
Example #7
Source File: app.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def vi_insert_mode() -> bool:
    from prompt_toolkit.key_binding.vi_state import InputMode

    app = get_app()

    if (
        app.editing_mode != EditingMode.VI
        or app.vi_state.operator_func
        or app.vi_state.waiting_for_digraph
        or app.current_buffer.selection_state
        or app.vi_state.temporary_navigation_mode
        or app.current_buffer.read_only()
    ):
        return False

    return app.vi_state.input_mode == InputMode.INSERT 
Example #8
Source File: app.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def vi_replace_mode() -> bool:
    from prompt_toolkit.key_binding.vi_state import InputMode

    app = get_app()

    if (
        app.editing_mode != EditingMode.VI
        or app.vi_state.operator_func
        or app.vi_state.waiting_for_digraph
        or app.current_buffer.selection_state
        or app.vi_state.temporary_navigation_mode
        or app.current_buffer.read_only()
    ):
        return False

    return app.vi_state.input_mode == InputMode.REPLACE 
Example #9
Source File: test_cli.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_vi_visual_empty_line():
    """
    Test edge case with an empty line in Visual-line mode.
    """
    feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI, multiline=True)

    # 1. Delete first two lines.
    operations = (
        # Three lines of text. The middle one is empty.
        "hello\r\rworld"
        # Go to the start.
        "\x1bgg"
        # Visual line and move down.
        "Vj"
        # Delete.
        "d\r"
    )
    result, cli = feed(operations)
    assert result.text == "world"

    # 1. Delete middle line.
    operations = (
        # Three lines of text. The middle one is empty.
        "hello\r\rworld"
        # Go to middle line.
        "\x1bggj"
        # Delete line
        "Vd\r"
    )

    result, cli = feed(operations)
    assert result.text == "hello\nworld" 
Example #10
Source File: cli.py    From android_universal with MIT License 5 votes vote down vote up
def __call__(self, cli):
        if cli.editing_mode != EditingMode.VI:
            return False

        return cli.vi_state.waiting_for_digraph 
Example #11
Source File: test_cli.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_vi_character_paste():
    feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI)

    # Test 'p' character paste.
    result, cli = feed("abcde\x1bhhxp\r")
    assert result.text == "abdce"
    assert result.cursor_position == 3

    # Test 'P' character paste.
    result, cli = feed("abcde\x1bhhxP\r")
    assert result.text == "abcde"
    assert result.cursor_position == 2 
Example #12
Source File: test_cli.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_vi_macros():
    feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI)

    # Record and execute macro.
    result, cli = feed("\x1bqcahello\x1bq@c\r")
    assert result.text == "hellohello"
    assert result.cursor_position == 9

    # Running unknown macro.
    result, cli = feed("\x1b@d\r")
    assert result.text == ""
    assert result.cursor_position == 0

    # When a macro is called within a macro.
    # It shouldn't result in eternal recursion.
    result, cli = feed("\x1bqxahello\x1b@xq@x\r")
    assert result.text == "hellohello"
    assert result.cursor_position == 9

    # Nested macros.
    result, cli = feed(
        # Define macro 'x'.
        "\x1bqxahello\x1bq"
        # Define macro 'y' which calls 'x'.
        "qya\x1b@xaworld\x1bq"
        # Delete line.
        "2dd"
        # Execute 'y'
        "@y\r"
    )

    assert result.text == "helloworld" 
Example #13
Source File: app.py    From aws-shell with Apache License 2.0 5 votes vote down vote up
def create_application(self, completer, history,
                           display_completions_in_columns):
        self.key_manager = self.create_key_manager()
        toolbar = Toolbar(
            lambda: self.model_completer.match_fuzzy,
            lambda: self.enable_vi_bindings,
            lambda: self.show_completion_columns,
            lambda: self.show_help)
        style_factory = StyleFactory(self.theme)
        buffers = {
            'clidocs': Buffer(read_only=True)
        }

        if self.enable_vi_bindings:
            editing_mode = EditingMode.VI
        else:
            editing_mode = EditingMode.EMACS

        return Application(
            editing_mode=editing_mode,
            layout=self.create_layout(display_completions_in_columns, toolbar),
            mouse_support=False,
            style=style_factory.style,
            buffers=buffers,
            buffer=self.create_buffer(completer, history),
            on_abort=AbortAction.RETRY,
            on_exit=AbortAction.RAISE_EXCEPTION,
            on_input_timeout=self.on_input_timeout,
            key_bindings_registry=self.key_manager.manager.registry,
        ) 
Example #14
Source File: editor.py    From pyvim with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _create_application(self):
        """
        Create CommandLineInterface instance.
        """
        # Create Application.
        application = Application(
            input=self.input,
            output=self.output,
            editing_mode=EditingMode.VI,
            layout=self.editor_layout.layout,
            key_bindings=self.key_bindings,
#            get_title=lambda: get_terminal_title(self),
            style=DynamicStyle(lambda: self.current_style),
            paste_mode=Condition(lambda: self.paste_mode),
#            ignore_case=Condition(lambda: self.ignore_case),  # TODO
            include_default_pygments_style=False,
            mouse_support=Condition(lambda: self.enable_mouse_support),
            full_screen=True,
            enable_page_navigation_bindings=True)

        # Handle command line previews.
        # (e.g. when typing ':colorscheme blue', it should already show the
        # preview before pressing enter.)
        def preview(_):
            if self.application.layout.has_focus(self.command_buffer):
                self.previewer.preview(self.command_buffer.text)
        self.command_buffer.on_text_changed += preview

        return application 
Example #15
Source File: cli.py    From android_universal with MIT License 5 votes vote down vote up
def __call__(self, cli):
        if (cli.editing_mode != EditingMode.VI
                or cli.vi_state.operator_func
                or cli.vi_state.waiting_for_digraph
                or cli.current_buffer.selection_state):
            return False

        return (cli.vi_state.input_mode == ViInputMode.NAVIGATION or
                cli.current_buffer.read_only()) 
Example #16
Source File: cli.py    From android_universal with MIT License 5 votes vote down vote up
def __call__(self, cli):
        if (cli.editing_mode != EditingMode.VI
                or cli.vi_state.operator_func
                or cli.vi_state.waiting_for_digraph
                or cli.current_buffer.selection_state
                or cli.current_buffer.read_only()):
            return False

        return cli.vi_state.input_mode == ViInputMode.INSERT 
Example #17
Source File: cli.py    From android_universal with MIT License 5 votes vote down vote up
def __call__(self, cli):
        if (cli.editing_mode != EditingMode.VI
                or cli.vi_state.operator_func
                or cli.vi_state.waiting_for_digraph
                or cli.current_buffer.selection_state
                or cli.current_buffer.read_only()):
            return False

        return cli.vi_state.input_mode == ViInputMode.INSERT_MULTIPLE 
Example #18
Source File: cli.py    From android_universal with MIT License 5 votes vote down vote up
def __call__(self, cli):
        if (cli.editing_mode != EditingMode.VI
                or cli.vi_state.operator_func
                or cli.vi_state.waiting_for_digraph
                or cli.current_buffer.selection_state
                or cli.current_buffer.read_only()):
            return False

        return cli.vi_state.input_mode == ViInputMode.REPLACE 
Example #19
Source File: cli.py    From android_universal with MIT License 5 votes vote down vote up
def __call__(self, cli):
        if cli.editing_mode != EditingMode.VI:
            return False

        return bool(cli.current_buffer.selection_state) 
Example #20
Source File: python_input.py    From ptpython with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def vi_mode(self, value: bool) -> None:
        if value:
            self.editing_mode = EditingMode.VI
        else:
            self.editing_mode = EditingMode.EMACS 
Example #21
Source File: test_cli.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_vi_visual_line_copy():
    feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI, multiline=True)

    operations = (
        # Three lines of text.
        "-line1\r-line2\r-line3\r-line4\r-line5\r-line6"
        # Go to the second character of the second line.
        "\x1bkkkkkkkj0l"
        # Enter Visual linemode.
        "V"
        # Go down one line.
        "j"
        # Go 3 characters to the right (should not do much).
        "lll"
        # Copy this block.
        "y"
        # Go down one line.
        "j"
        # Insert block twice.
        "2p"
        # Escape again.
        "\x1b\r"
    )

    result, cli = feed(operations)

    assert (
        result.text
        == "-line1\n-line2\n-line3\n-line4\n-line2\n-line3\n-line2\n-line3\n-line5\n-line6"
    ) 
Example #22
Source File: test_cli.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_vi_block_editing():
    " Test Vi Control-V style block insertion. "
    feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI, multiline=True)

    operations = (
        # Six lines of text.
        "-line1\r-line2\r-line3\r-line4\r-line5\r-line6"
        # Go to the second character of the second line.
        "\x1bkkkkkkkj0l"
        # Enter Visual block mode.
        "\x16"
        # Go down two more lines.
        "jj"
        # Go 3 characters to the right.
        "lll"
        # Go to insert mode.
        "insert"  # (Will be replaced.)
        # Insert stars.
        "***"
        # Escape again.
        "\x1b\r"
    )

    # Control-I
    result, cli = feed(operations.replace("insert", "I"))

    assert result.text == "-line1\n-***line2\n-***line3\n-***line4\n-line5\n-line6"

    # Control-A
    result, cli = feed(operations.replace("insert", "A"))

    assert result.text == "-line1\n-line***2\n-line***3\n-line***4\n-line5\n-line6" 
Example #23
Source File: test_cli.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_vi_operators():
    feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI)

    # Esc g~0
    result, cli = feed("hello\x1bg~0\r")
    assert result.text == "HELLo"

    # Esc gU0
    result, cli = feed("hello\x1bgU0\r")
    assert result.text == "HELLo"

    # Esc d0
    result, cli = feed("hello\x1bd0\r")
    assert result.text == "o" 
Example #24
Source File: app.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def vi_recording_macro() -> bool:
    " When recording a Vi macro. "
    app = get_app()
    if app.editing_mode != EditingMode.VI:
        return False

    return app.vi_state.recording_register is not None 
Example #25
Source File: app.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def vi_digraph_mode() -> bool:
    app = get_app()
    if app.editing_mode != EditingMode.VI:
        return False

    return app.vi_state.waiting_for_digraph 
Example #26
Source File: app.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def vi_waiting_for_text_object_mode() -> bool:
    app = get_app()
    if app.editing_mode != EditingMode.VI:
        return False

    return app.vi_state.operator_func is not None 
Example #27
Source File: app.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def vi_selection_mode() -> bool:
    app = get_app()
    if app.editing_mode != EditingMode.VI:
        return False

    return bool(app.current_buffer.selection_state) 
Example #28
Source File: key_processor.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _leave_vi_temp_navigation_mode(self, event: "KeyPressEvent") -> None:
        """
        If we're in Vi temporary navigation (normal) mode, return to
        insert/replace mode after executing one action.
        """
        app = event.app

        if app.editing_mode == EditingMode.VI:
            # Not waiting for a text object and no argument has been given.
            if app.vi_state.operator_func is None and self.arg is None:
                app.vi_state.temporary_navigation_mode = False 
Example #29
Source File: clitoolbar.py    From athenacli with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def create_toolbar_tokens_func(cli, show_fish_help):
    """Return a function that generates the toolbar tokens."""
    def get_toolbar_tokens():
        result = []
        result.append(('class:bottom-toolbar', ' '))

        if cli.multi_line:
            result.append(
                ('class:bottom-toolbar', ' (Semi-colon [;] will end the line) ')
            )
        if cli.multi_line:
            result.append(('class:bottom-toolbar.on', '[F3] Multiline: ON '))
        else:
            result.append(('class:bottom-toolbar.off', '[F3] Multiline: OFF'))
        
        if cli.prompt_app.editing_mode == EditingMode.VI:
            result.append((
                'class:bottom-toolbar.on',
                'Vi-mode ({})'.format(_get_vi_mode())
            ))
        
        if show_fish_help():
            result.append(
                ('class:bottom-toolbar', ' Right-arrow to complete suggestion')
            )
        
        if cli.completion_refresher.is_refreshing():
            result.append(
                ('class:bottom-toolbar', '     Refreshing completions...')
            )
        return result
    return get_toolbar_tokens 
Example #30
Source File: repl.py    From crash with Apache License 2.0 5 votes vote down vote up
def _get_editing_mode():
    files = ['/etc/inputrc', os.path.expanduser('~/.inputrc')]
    for filepath in files:
        try:
            with open(filepath, 'r') as f:
                for line in f:
                    if line.strip() == 'set editing-mode vi':
                        return EditingMode.VI
        except IOError:
            continue
    return EditingMode.EMACS