1
#!/bin/sh

action() {

        screen -X stuff "xdotool key Ctrl+a $1"$(echo '\015')

}

action bar
screen
action 0
action Tab
action 1
action S
screen
action Tab
action 2
action Tab
vim
#action Tab
#vim $2
#action Tab
#vim $3
#action Tab

when vim line is commented out the following output as intended is displayed one vertical partition in between and one horizontal partition on the right side of vertical partition

when vim line is included only two partitions are displayed separated by a horizontal line. the first partition shows screen #1 and second partition shows screen #2.

vim is opened in screen #0 and


l key Ctrl+a bar

is written

enter image description here

Could not get how vim may be causing the possible error? Please help.

1 Answer 1

0

What's happening here is that screen's stuff command is pushing the characters into the input buffer of the pane, but they're just sitting there until there's a program to consume them.

Since you're running this script on pane #0 and issuing the xdotool key Ctrl+a bar command on that pane #0, then when the script ends and you're back at the shell, the shell will take that and interpret it as a normal shell command.

You can actually see that in your first screenshot, you'll see the characters there on a line by itself (same as you typed some characters), which then become pending input. When the script ends and you're back in the shell, it consumes those characters and ends up repeating the command. The result looks like this:

xdotool key Ctrl+a bar
devanshu@...:~/env/bin/cp_c++$ xdotool key Ctrl+a bar
devanshu@...:~/env/bin/cp_c++$

If you're running Vim instead of exiting the script, then Vim will consume that same input and interpret it as Vim commands. It then sees this sequence of commands:

  • x: Delete character under the cursor. The buffer is empty, so that just beeps and does nothing.
  • do: Get diff from the other buffer (useful in vimdiff mode.) This is not useful here, but again it just does nothing in this context.
  • to: Move to the next character o in this line. The line is empty, so again this does nothing.
  • o: Enter insert mode on the line below the current line.

So the last command goes into insert mode, which means the rest of the line (l key Ctrl+a bar) and the "Enter" are entered into the buffer at the second line. Vim is left at the start of the third line, still in insert mode.

So that explains this perfectly.

A simpler reproducer to this is to run a command such as:

$ sleep 30; vim

Then while the sleep is running, type that line above:

xdotool key Ctrl+a bar

Since sleep won't consume its output, that input will be available to Vim whenever it starts, after the sleep is over. You'll see it behaves the same as described above, including the "beep" if it's enabled in your terminal.

4
  • 1
    Oh, thanks! Now I get it. Commented Apr 4, 2020 at 19:22
  • I'm sorry, it was my first question and I don't know how to use it that's why I got so late to answer. I think I will then start terminal in vim which will execute the commands or do you have some better suggestions? Commented Apr 4, 2020 at 19:23
  • @DevanshuSingla Hard for me to say... Your question was more of "why does this happen" rather than "how do I get this result", so I don't think I know what you're really going for here... Yeah using the Vim terminal is often a good alternative to screen/tmux panes. But there are trade-offs. (One way you pilot Vim from outside, the other you pilot terminal applications from Vim...) Anyways, feel free to ask more questions on "how" to do something, if you have them. You should also check out Vi & Vim Stack Exchange for questions on Vim!
    – filbranden
    Commented Apr 4, 2020 at 19:27
  • 1
    yes definitely! I am just a newbie trying to get a hand in bash so wanted to try implementing an automatic setup for cp. But screen maybe not so practical as would be to open three terminals in the desired setup. Commented Apr 4, 2020 at 19:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .