-1

I'm trying to improve my screen software, so I'll have a lot of questions because there are many answers that I have difficulty to find on internet... Anyway, this one is about a shell command that I would like screen to execute when opening, and it doesn't...

on a new session of screen, I ask screen to open some windows and to name them and to open a file in each of them, and then to split vertically and to open to new empty windows :

screen -t name1 vim /path/to/folder/file1.txt
screen -t name2 vim /path/to/folder/file2.txt
screen -t name3 vim /path/to/folder/file3.txt
screen -t name4 vim /path/to/folder/file4.txt
split -v
focus
screen
focus
screen

but those files create .file.txt.swp and .file.txt.swo hidden files, as usual, so when my computer shut down and files didn't close properly, when I reopen screen it ask what I must do with the .sw* files... I would like to run first this command rm /path/to/folder/.*.sw* so it doesn't ask for the action when opening (".file.txt.swp already exist ! [O]open, [E]dit anyway, [R]ecover, [Q]uit, [A]bandoned")

In the shell rm path/to/folder/.*.sw* deletes every swap files as intended, but I can't make it works in .screenrc

This doesn't work :

rm /path/to/folder/.*.sw*

screen -t name1 vim /path/to/folder/file1.txt
screen -t name2 vim /path/to/folder/file2.txt
screen -t name3 vim /path/to/folder/file3.txt
screen -t name4 vim /path/to/folder/file4.txt
split -v
focus
screen
focus
screen

neither does this :

exec rm /path/to/folder/.*.sw*

...

nor this :

eval 'rm /path/to/folder/.*.sw*'

...

or even this :

stuff rm /path/to/folder/.*.sw*

...

and a lot of other 'blind' tries...

Well I have no idea what I'm doing :p

hugogogo
  • 501
  • 4
  • 24

2 Answers2

3

In your .screenrc, the screen command takes a string which it evaluates itself rather than passing to a shell. So you can't gang multiple commands together so easily... For example, a config line like this:

screen -t test1 touch foo; touch bar

would result in three files being touched -- foo;, touch and bar. :)

Instead, you can run a shell interpreter to run multiple commands, including vim:

screen -t name1 sh -c 'rm /path/to/my/.file1.sw*; vim /path/to/my/file1.txt'

The options passed to the sh command will be interpreted correctly because within screen, they're all just one option.

If you really plan to do this, you may run in to collisions because name2 and name3 etc will generate swap files at the same time as they are being deleted in another window, so you might do this:

screen -t name1 sh -c 'rm /path/to/my/.file1.sw*; vim /path/to/my/file1.txt'
screen -t name2 sh -c 'sleep 1; vim /path/to/my/file2.txt'
screen -t name3 sh -c 'sleep 1; vim /path/to/my/file3.txt'
screen -t name4 sh -c 'sleep 1; vim /path/to/my/file4.txt'

Note that another alternative might be to tell vim not to create swap files at all. Interatively, you can do this with:

:set noswapfile

To put this on your command line (rather than making it a default for all your vim instances), you can use vim's -n option, which causes vim to open files with no swap:

screen -t name1 vim -n /path/to/my/file1.txt
screen -t name2 vim -n /path/to/my/file2.txt
screen -t name3 vim -n /path/to/my/file3.txt
screen -t name4 vim -n /path/to/my/file4.txt

The result will of course be that unsaved changes to your four files will be lost.

One more option might be to move your temporary files to a separate location. For example, if you use tmpfs/shmfs and your /tmp directory is empty as of every reboot, you might use:

screen -t name1 vim --cmd 'set dir=/tmp' /path/to/my/file1.txt
screen -t name2 vim --cmd 'set dir=/tmp' /path/to/my/file2.txt
screen -t name3 vim --cmd 'set dir=/tmp' /path/to/my/file3.txt
screen -t name4 vim --cmd 'set dir=/tmp' /path/to/my/file4.txt

Then you can still recover the file if a vim instance is killed for some other reason than than the server shutting down.

ghoti
  • 45,319
  • 8
  • 65
  • 104
  • awsome ! I'll try all of that, and tell you back if I have trouble, otherwhile thanks a lot ! – hugogogo Apr 16 '18 at 13:03
  • My pleasure. And if this solves your issue, please feel free to close the question by clicking the green checkmark to the left of my answer. :) – ghoti Apr 16 '18 at 13:55
  • I didn't try the temporary file solution, but the two others works perfectly ! I have one more question and one precision about the `sh -c` command : -the `-c` option is used to override a configuration of the .screenrc file -and I assume the `sh` command is used to enter a shell command, but it's not clear for me if it's the `shell command` which "is used to create a new shell and override the value of the environment variable $SHELL" ? It may not be important, but the meaning of that is obscure to me... – hugogogo Apr 18 '18 at 10:32
  • So .. not quite. In `sh -c`, the `-c` is an option to the `sh` command, which is your POSIX shell. `man sh` for details on that. We are not overriding anything with this command, we're just running the `sh` command with two arguments, `-c` and a string containing shell commands to run. – ghoti Apr 18 '18 at 11:11
-1

When ".file.swp already exist ! [O]open, [E]dit anyway, [R]ecover, [Q]uit, [A]bandoned", you will have more luck removing ".file.swp" then when you try to remove "file.swp", don't you think?

Didn't you notice the DOT?

Gerard H. Pille
  • 2,528
  • 1
  • 13
  • 17
  • Regardless of the filename, this does not answer the question. – ghoti Apr 16 '18 at 11:14
  • 1
    Ho yes of course, I'll edit my post, this is not the problem, the command line that I try to run in my .screenrc file work perfectly when I run it in the shell... syntax of the command itself is good, but the way to make it run by screen when opening is a mystery for me – hugogogo Apr 16 '18 at 11:16
  • Also, I don't believe "vim /path/to/my/file1.txt" would create a file called ".file.swp", but rather ".file1.txt.swp". Moreover, it would not be in /path/to, but in /path/to/my folder. Not that I'm trying to answer the question, eh @ghoti? – Gerard H. Pille Apr 16 '18 at 11:24
  • If `/path/to` is obviously an example, why would you expect that `file` is not also an example? Was it unclear to you that the OP was asking how to run extra commands in screen? – ghoti Apr 16 '18 at 11:30
  • To me, it looks as if he's trying to run commands that have no chance of succeeding, BEFORE screen. – Gerard H. Pille Apr 16 '18 at 11:37
  • I see. Well, now that he's confirmed (in his comment above) that your initial interpretation was incorrect, will you fix your answer? – ghoti Apr 16 '18 at 11:55
  • I'm too knew in informatic so I have a big lake of accuracy, and also english is still a little bit difficult for me, and everything you said, Gerard H. Pille, is true, and it's my fault that I havent been precise enough in my post. anyway, on my computer I already noticed all of that, both the .file.**txt**.swp, and the folder path (this one was obvious in the real context, I got confused in the example), and my command line works great when I run it in the shell, I can assure you, I tried it several times ! – hugogogo Apr 16 '18 at 13:01
  • In the mean time, ghoti has given you a number of solutions. The screenrc does not allow you to execute an OS command directly. That is what you overlooked (and what I didn't know before I checked myself, since I never use screen). – Gerard H. Pille Apr 16 '18 at 13:16