1

In terminal mode, commands like d or dd are not applicable for deleting texts.

I think this would be useful since it is common to edit a command line.

For example, if I open a terminal vim :term and type some command someCommand someArg1 someArg2 and then realize I messed up the order of arguments to someCommand, I would like to visually select and delete someArg1 and yank it right after someArg2.

Is it possible to enable this?

0

2 Answers 2

0

This is only partly possible. I provide the explanation below after giving some examples, first.

When you open a terminal inside Vim using :term[inal], the terminal is opened in a mode called Terminal-Job mode. This means that the job, i.e. the terminal process is in control of the contents of the buffer containing the terminal. You can use termwinkey, which defaults to Ctrl-W if the option termwinkey is unset, and then type N (capital N, i.e. Shift-N) to switch to Terminal-Normal mode. This enables you to visually select and yank text. It does not allow you to delete text, though, as (out of necessity, see below) the buffer is not modifiable. To go back to Terminal-Job mode, i.e. the mode where you can type and execute commands, press anything that normally would lead you to enter insert mode such as i or a. Here you can insert previously yanked text by again pressing termwinkey and then ", followed by a letter indicating the register whose contents you would like to paste. E.g. if you simply copied text with a command such as y (without explicitly specifying a register) to the default register, you need to type termwinkey " " (i.e. " twice) to paste the text you copied to the terminal.

Hence, to follow your example someCommand someArg1 someArg2: You can enter Terminal-Normal mode by pressing Ctrl-W and than (capital) N (resp. termwinkey N), and then visually select someArg2 and yank it using y. Then, you can switch back to Terminal-Job by pressing i, delete someArg2 by pressing backspace (or Ctrl-Alt-H if your shell allows for this), move to before someArg1 using arrow keys (or using Ctrl-B and Alt-B if your shell allows for this) and insert someArg2 before someArg1 using <C-W>"".

There is no way to delete any text in the terminal, though. To understand why this is impossible, we must discuss roughly what happens when you press a key in the terminal resp. terminal-emulator. A usual misconception is to think that if you type someCommand in a terminal resp. terminal emulator, the characters corresponding to the key strokes are only sent to the shell (i.e. the process running in your terminal resp. terminal emulator that executes your comments) after pressing enter. Usually, this is not what happens. The misconception arises because shells typically echo every character back to the terminal resp. terminal emulator whenever you type it, such that you can see what characters you have just typed. Additionally, they typically implement features to edit the command you have typed before executing it (a typical choice to implement such features is the readline library).

Consequently, whenever you open a terminal in Vim - as in a regular terminal emulator - Vim immediately sends a key stroke to the shell instead of displaying the corresponding character in the buffer by itself. Then, typically, the shell echoes the character back. Vim receives the output from the terminal and displays it in the buffer, i.e. you are able to see the character on the screen corresponding to the key you just have pressed.

Hence, Vim is not really in charge of the contents you see in the buffer in which a terminal has been opened within Vim. It is the shell that decides over the contents of this buffer, and for this shell Vim acts as a terminal emulator where the buffer is the display. Keystrokes are send immediately to the shell and Vim displays whatever the shell instructs Vim to display, including the characters the shell echoes back to you whenever you press a key. In particular, Vim does not allow you to edit anything you have typed before sending it to the shell.

This behaviour is intended. Otherwise, within Vim, you would not be able you use the command line editing features of your shell, such as (if your shell is capable and configured to do so) going back through your command line history by pressing Ctrl-P (resp. arrow key up).

1

You are running a shell in that terminal window, and that shell already has command-line editing capabilities so there is no need to involve Vim at all.

In bash, you would simply need to do M-t (which Vim doesn't have the equivalent of anyway):

term

See $ man readline if you use bash or the relevant manual if you use something else.

2
  • 1
    Or set -o vi for bash and bindkey -v for Zsh
    – D. Ben Knoble
    Commented Mar 10 at 17:19
  • Well, I don't like that one bit but yeah, use your shell's features.
    – romainl
    Commented Mar 10 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.