Shell command in screenrc

2.1k Views Asked by At

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

2

There are 2 best solutions below

4
On BEST ANSWER

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.

8
On

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?