Best AutoHotKey macros? [closed] - automation

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I use AutoHotKey for Windows macros. Most commonly I use it to define hotkeys that start/focus particular apps, and one to send an instant email message into my ToDo list. I also have an emergency one that kills all of my big memory-hogging apps (Outlook, Firefox, etc).
So, does anyone have any good AHK macros to share?

Very simple and useful snippet:
SetTitleMatchMode RegEx ;
; Stuff to do when Windows Explorer is open
;
#IfWinActive ahk_class ExploreWClass|CabinetWClass
; create new folder
;
^!n::Send !fwf
; create new text file
;
^!t::Send !fwt
; open 'cmd' in the current directory
;
^!c::
OpenCmdInCurrent()
return
#IfWinActive
; Opens the command shell 'cmd' in the directory browsed in Explorer.
; Note: expecting to be run when the active window is Explorer.
;
OpenCmdInCurrent()
{
WinGetText, full_path, A ; This is required to get the full path of the file from the address bar
; Split on newline (`n)
StringSplit, word_array, full_path, `n
full_path = %word_array1% ; Take the first element from the array
; Just in case - remove all carriage returns (`r)
StringReplace, full_path, full_path, `r, , all
full_path := RegExReplace(full_path, "^Address: ", "") ;
IfInString full_path, \
{
Run, cmd /K cd /D "%full_path%"
}
else
{
Run, cmd /K cd /D "C:\ "
}
}

Here is so simple but useful script:
^SPACE:: Winset, Alwaysontop, , A
Use CTRL + Space to set any window always on top.

Add surrounding quotes on selected text/word
Useful when writing emails or during coding...
Doubleclick word, hit Win+X, have quotes around
; Win + X
#x:: ; Attention: Strips formatting from the clipboard too!
Send ^c
clipboard = "%clipboard%"
; Remove space introduced by WORD
StringReplace, clipboard, clipboard,%A_SPACE%",", All
Send ^v
return

; I have this in my start menu so that I won't ruin my ears when I put on my headphones after rebooting my computer
sleep, 5000
SoundSet, 1.5 ; really low volume

I create new Outlook objects with AutoHotKey
; Win+Shift+M = new email
#+m:: Run "mailto:"
; Outlook
#^M:: Run "%ProgramFiles%\Microsoft Office\Office12\OUTLOOK.EXE" /recycle
; Win+Shift+A = create new calendar appointment
#+A:: Run "%ProgramFiles%\Microsoft Office\Office12\OUTLOOK.EXE"/c ipm.appointment
; Win+Shift+T = create new Task
; Win+Shift+K = New task
#+T:: Run "%ProgramFiles%\Microsoft Office\Office12\OUTLOOK.EXE"/c ipm.task
#+K:: Run "%ProgramFiles%\Microsoft Office\Office12\OUTLOOK.EXE"/c ipm.task

Here's a dead-simple snippet to quickly close the current window using a mouse button.
It's one of the actions you perform most often in Windows, and you'll be surprised at how much time you save by no longer having to shoot for that little X. With a 5-button mouse, I find this a very useful reassignment of the "Forward" button.
#IfWinActive ;Close active window when mouse button 5 is pressed
XButton2::
SendInput {Alt Down}{F4}{Alt Up}
Return
#IfWinActive
To take into account programs that use tabbed documents (like web browsers), here's a more comprehensive version:
;-----------------------------------------------------------------------------
; Bind Mouse Button 5 to Close Tab / Close Window command
;-----------------------------------------------------------------------------
; Create a group to hold windows which will use Ctrl+F4 instead of Alt+F4
GroupAdd, CtrlCloseGroup, ahk_class IEFrame ; Internet Explorer
GroupAdd, CtrlCloseGroup, ahk_class Chrome_WidgetWin_0 ; Google Chrome
; (Add more programs that use tabbed documents here)
Return
; For windows in above group, bind mouse button to Ctrl+F4
#IfWinActive, ahk_group CtrlCloseGroup
XButton2::
SendInput {Ctrl Down}{F4}{Ctrl Up}
Return
#IfWinActive
; For everything else, bind mouse button to Alt+F4
#IfWinActive
XButton2::
SendInput {Alt Down}{F4}{Alt Up}
Return
#IfWinActive
; In FireFox, bind to Ctrl+W instead, so that the close command also works
; on the Downloads window.
#IfWinActive, ahk_class MozillaUIWindowClass
XButton2::
SendInput {Ctrl Down}w{Ctrl Up}
Return
#IfWinActive
Visual Studio 2010 can't easily be added to the CtrlCloseGroup above, as it's window class / title aren't easily predictable (I think). Here's the snippet I use to handle it, including a couple additional helpful bindings:
SetTitleMatchMode, 2 ; Move this line to the top of your script
;-----------------------------------------------------------------------------
; Visual Studio 2010
;-----------------------------------------------------------------------------
#IfWinActive, Microsoft Visual Studio
; Make the middle mouse button jump to the definition of any token
MButton::
Click Left ; put the cursor where you clicked
Send {Shift Down}{F2}{Shift Up}
Return
; Make the Back button on the mouse jump you back to the previous area
; of code you were working on.
XButton1::
Send {Ctrl Down}{Shift Down}{F2}{Shift Up}{Ctrl Up}
Return
; Bind the Forward button to close the current tab
XButton2::
SendInput {Ctrl Down}{F4}{Ctrl Up}
Return
#IfWinActive
I also find it useful in Outlook to map ALT+1, ALT+2, etc. to macros I wrote which move the currently selected message(s) to specific folders (eg. "Personal Filed", "Work Filed", etc) but that's a bit more complicated.

There are tons of good ones in the AutoHotKey Forum:
http://www.autohotkey.com/forum/forum-2.html&sid=8149586e9d533532ea76e71e8c9e5b7b
How good? really depends on what you want/need.

I use this one all the time, usually for quick access to the MySQL command line.
http://lifehacker.com/software/featured-windows-download/make-a-quake+style-command-prompt-with-autohotkey-297607.php

Fix an issue when copying file to FTP server when the "Copying" dialog appears on top of the "Confirm File Replace" dialog (very annoying):
SetTimer, FocusOnWindow, 500
return
FocusOnWindow:
IfWinExist, Confirm File Replace
WinActivate
return
One to deactivate the useless Caps-lock key:
Capslock::
return
CTRL + shift + c will copy colour below cursor to the clipboard (in hexadecimal)
^+c::
MouseGetPos,x,y
PixelGetColor,rgb,x,y,RGB
StringTrimLeft,rgb,rgb,2
Clipboard=%rgb%
Return
Write your email address in the active field (Win key + m)
#m::
Send, my#email.com{LWINUP}
Sleep, 100
Send, {TAB}
return

Related

autohotkey variables not updating on keypresses

I have a number of hotkeys which go through various processes and sleep for certain periods of time to allow animations to be shown. I want to setup a variable to allow only 1 to activate at a time...so if I hit the first and then the second....the second doesn't do anything until the first completes. Similarly I don't want to double activate any of them by mashing the same key right after I first press it.
F4:ExitApp
#IfWinActive ahk_class notepad
a::
if (work:=true){
work:=false
soundbeep
;do something
sleep 1000
work:=true
}
return
b::
if (work:=true){
work:=false
soundbeep
;do something else
sleep 2000
work:=true
}
return
i.e. if I hit 'a'....'b' cannot activate until the sleep of 'a' ends. Nor should 'a' be able to activate a second time...at least not until its sleep ends and work=true again.
This leaves 2 problems. I need to first somehow specify the initial value of 'work=true'. I do not know how to do this in the traditional sense. Simply putting it at the top of the code doesn't work. Second problem, putting this in another key...like enter:: work:=true return....and pressing that key initially...this doesn't work as it allows b to beep while a is still in its sleep phase. So maybe the code above is flawed to begin with as well. Individual keys seem to respect themselves and not re-initialize until after the first instance has completed...so the beeps work if I just mash 1 key. Also I don't want to have to press enter to get the code to work after loading the script.
Is there an easy cheat here? Like in lua undeclared variables are automatically false... so I could just swap to if (work=false){...., I can't find any such behaviour with autohotkey though.
As requested, here is a different solution that uses variable logic to allow or prevent hotkey commands from executing:
#IfWinActive ahk_class Notepad
work:=true
a::
if(work){
work:=false
soundbeep
;do something
sleep 1000
work:=true
}
return
b::
if(work){
work:=false
soundbeep
;do something else
sleep 2000
work:=true
}
return
Update: Breakdown of how it works
First, the script begins in the auto-execute section. As summarized elegently by maul-esel
When an AutoHotkey script launches, it just starts execution from the
first line of your script. It continues from there line by line until
it hits an end point. This is called the auto-execute section.
As such, by including work:=true in this section of the script, not only are we initializing the value of the variable before any hotkeys are triggered, we set the scope of work to be a global variable, accessible across different hotkeys.
The rest of the script is a bit more straightforward, where each hotkey is essentially (in pseudocode):
When the hotkey is triggered
if work is true
set work to false
Beep and etc.
Wait for some amount of time
Then set work back to true so that another command can be run
End the hotkey
One simple way to do it would be to declare #MaxThreads 1 at the top of your script.
As a side note, there appear to be a few other syntax errors in the script. For example, when comparing values (like in an if statement), you still use the regular, single = and not the assignment :=. Also, the ahk_class is case sensitive, and as such #IfWinActive ahk_class notepad should be replaced with #IfWinActive ahk_class Notepad.
Updated Code (Work Variable logic was removed as it is no longer needed):
#MaxThreads 1
#IfWinActive ahk_class Notepad
a::
soundbeep
;do something
sleep 1000
return
b::
soundbeep
;do something else
sleep 2000
return

Autohotkey: Restart a "screensaver" script/part of a script (Starting VLC)

I wanted to create a "VLC-screensaver" script:
When the user/system is idle for a certain time VLC should start and play a video from a specified folder. I can start the script and VLC is being executed by it after the set time. Now I exit it with "Esc" and VLC closes.
After I closed it the AHK-tray is visible but VLC/the script is not starting again after the set time...
Where is the mistake? Thank you in advance!
#Persistent
SetTimer, Check, 1000
return
Check:
If (A_TimeIdle>=10000)
{
run C:\Program Files\VideoLAN\VLC\vlc.exe --repeat --fullscreen "D:\video"
SetTimer, Check, Off
}
return
#IfWinActive ahk_exe vlc.exe
Escape::Send !{F4}
#IfWinActive
return
Got it by myself:
#IfWinActive ahk_exe vlc.exe
Escape::
Send !{F4}
Reload
#IfWinActive
Return
Forgot that you can assign more than one action/paramter/value to one
key...

How do I stop SAPI.SpVoice reading "is." as "island"?

Can you configure the way SAPI.spVoice reads text?
In my situation I am reading the current clipboard using an AutoHotKey script.
The script makes a COM call to SAPI.spVoice passing the text from the clipboard.
;;;;;;;;;;;;;;;;;;;;TTS;;;;;;;;;;;;;;;;;;;;;;
#^!D:: ; Win + Ctrl + D + Alt
ClipSaved := ClipboardAll
clipboard = ; Start off empty to allow ClipWait to detect when the text has arrived.
Send ^c
ClipWait ; Wait for the clipboard to contain text.
ComObjCreate("SAPI.SpVoice").Speak(clipboard)
Clipboard := ClipSaved
ClipSaved = ; Free the memory
return
The problem is.. that SAPI reads some text incorrectly..
For Example:
"Yes it is. Ours is complex." reads "is." as island,
"Yes it is. This is complex." is read correctly.
You can experiment with this by doing the following:
If you are running windows 7.
Press the windows key and type "Change text to speech settings" and pick the option.
In this dialog enter "Yes it is. Ours is complex." in the "Use the following text to preview the voice:" field.
Press "Preview Voice"
Hear it read the "is." as island.
So... My question is...
Is it possible to change/configure the way "Microsoft Anna" reads text so it doesn't make these mistakes?
Is this a bug in the Anna voice only or all voices?
How can I make it read the text the way I want it read?
This is done by SAPI's text normalization code. Unfortunately, this is quite difficult to modify without building a custom voice, which is likely far more work than you probably want to implement.
"Every problem (except the problem of too many levels of indirection) can be solved with another level of indirection."
The SAPI.spVoice object can be passed text (as I was doing) or SSML.
By taking the text to be spoken, then converting it to SSML you gain control over how words are spoken. You have a chance to pre-process the text and replace miss-read words with the specific pronunciation you wish.
For example: "Yes it is. Ours is complex." becomes "Yes it <sub alias="is">is</sub>. Ours is complex."
sub and say_as seem to work. phoneme seem to be ignored.. but I may have something configured wrongly.
Note: If you want XML read aloud, XML escape the text before converting it to SSML, otherwise it will be assumed to be part of the SSML.
So.. in code:
;;;;;;;;;;;;;;;;;;;;TTS;;;;;;;;;;;;;;;;;;;;;;
#^D:: ; Win + Ctrl + D
ClipSaved := ClipboardAll
Clipboard = ; Start off empty to allow ClipWait to detect when the text has arrived.
Send ^c
ClipWait ; Wait for the clipboard to contain text.
FileDelete , c:\tmp\tmp_ahk_tts_clip.txt
FileAppend , %Clipboard% , c:\tmp\tmp_ahk_tts_clip.txt
RunWait, %comspec% /c ""F:\bin\tools\speakit.rb" c:\tmp\tmp_ahk_tts_clip.txt > c:\tmp\tmp_ahk_clip_tts_out.txt" ,,Hide
FileRead, Clipboard, c:\tmp\tmp_ahk_clip_tts_out.txt
ComObjCreate("SAPI.SpVoice").Speak(Clipboard)
Clipboard := ClipSaved
ClipSaved = ; Free the memory
return
and F:\bin\tools\speakit.rb is sometihng like this:
#!/usr/bin/env ruby
substitutions = [
[/[A-Z][A-Z][A-Z][A-Z]+((?=[^A-Za-z])|(?!.))/, lambda{|x|x.downcase}], #All caps becomes word
[/\.exe(?=[^a-z])/i, " executable "],
[/\.txt(?=[^a-z])/i, " text file "],
[/rebranded/, "re-branded"],
[/App(?=[\s\.])/, " application "],
['GUI' , " gooee "],
[/localhost/, "local host"],
[/(?<word>[A-Z][a-z]*)(?=[A-Z ,\.;:\t\/])/, "'\\k<word>' "], # CamelCaseWords should be split by spaces
['\\', '<sub alias="slash">\\</sub>'],
]
require 'cgi'
puts <<-eos
<?xml version="1.0"?>
<speak xmlns="http://www.w3.org/2001/10/synthesis" version="1.0" xml:lang="en-UK">
<voice xml:lang="en-UK">
#{substitutions.reduce(CGI::escapeHTML(ARGF.read)){|o, (r,s)| s.is_a?(Proc) ? o.gsub(r, &s) : o.gsub(r,s) }}
</voice>
</speak>
eos

How can I set tmux hotkey as Ctrl-, that is, Ctrl+comma

I've got some trouble when setting my preferred tmux hotkey on Mac OS X.
The most common hotkeys that invokes tmux's magics are CTRL+A and CTRL+B. But I would rather select other keystrokes for the following reasons: C-a is the global hot key for "jumping to the beginning of a line"; C-b is for "moving backward on a line" and "Page UP in Vim". I don't want to break these nice rules in tmux.
So, I try to set some non-so-frequently-used keystrokes for tmux hotkey. What I choose is CTRL-, , CTRL-., or CTRL-;
I write this statement in my .tmux.conf file:
set-option -g prefix C-,
I start tmux. Oh, it says "bad key". I replace C-, with C-. or C-;. It doesn't work either.
So, how can I set tmux hotkey to CTRL-,?
Had to dig into the source code for this one. The control key can only be applied to letters, #, space and ? (and C-# seems to be equivalent to C-space). Sorry, doesn't look like C-, is possible, at least in tmux-1.6.
None of comma, period, or semicolon are standard control characters. Your terminal emulator probably just sends the plain character, or nothing at all when you type them; you can type them at into cat -v to see what it is sending.
See Wikipedia’s “ASCII control characters” and “How control characters map to keyboards”.
If you can reconfigure your terminal emulator to send some other character/sequence, you should be able to use it in tmux (e.g. have your terminal emulator send the same character as C-\ (hex 1C), and set your prefix to that).
Using xterm terminal
1) put these in your ~/.Xresources to generate escape sequence of F13 when CTRL-,, CTRL-. or CTRL-; is pressed
XTerm.VT100.translations: #override \
Ctrl<Key>period: string(0x1b) string("[1;2P") \n\
Ctrl<Key>comma: string(0x1b) string("[1;2P") \n\
Ctrl<Key>semicolon: string(0x1b) string("[1;2P")
run xrdb ~/.Xresources to load these conf.
2) change prefix in your ~/.tmux.conf
set -g prefix F13
3) fire xterm and tmux
$ tmux

What must I know to use GNU Screen properly? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I've just introduced a friend to GNU Screen and they're having a hard time getting used to it. That makes me think about the essential things he needs to know about the excellent Screen utility, the same things that you'd think worthwhile to teach someone, a beginner, from the ground up. What are some analogies and handy tips for remembering binds, etc.?
It would be awesome.
I've been using Screen for over 10 years and probably use less than half the features. So it's definitely not necessary to learn all its features right away (and I wouldn't recommend trying). My day-to-day commands are:
^A ^W - window list, where am I
^A ^C - create new window
^A space - next window
^A p - previous window
^A ^A - switch to previous screen (toggle)
^A [0-9] - go to window [0-9]
^A esc - copy mode, which I use for scrollback
I think that's it. I sometimes use the split screen features, but certainly not daily. The other tip is if screen seems to have locked up because you hit some random key combination by accident, do both ^Q and ^A ^Q to try to unlock it.
I couldn't get used to screen until I found a way to set a 'status bar' at the bottom of the screen that shows what 'tab' or 'virtual screen' you're on and which other ones there are. Here is my setup:
[roel#roel ~]$ cat .screenrc
# Here comes the pain...
caption always "%{=b dw}:%{-b dw}:%{=b dk}[ %{-b dw}%{-b dg}$USER%{-b dw}#%{-b dg}%H %{=b dk}] [ %= %?%{-b dg}%-Lw%?%{+b dk}(%{+b dw}%n:%t%{+b dk})%?(%u)%?%{-b dw}%?%{-b dg}%+Lw%? %{=b dk}]%{-b dw}:%{+b dw}:"
backtick 2 5 5 $HOME/scripts/meminfo
hardstatus alwayslastline "%{+b dw}:%{-b dw}:%{+b dk}[%{-b dg} %0C:%s%a %{=b dk}]-[ %{-b dw}Load%{+b dk}:%{-b dg}%l %{+b dk}] [%{-b dg}%2`%{+b dk}] %=[ %{-b dg}%1`%{=b dk} ]%{-b dw}:%{+b dw}:%<"
sorendition "-b dw"
[roel#roel ~]$ cat ~/scripts/meminfo
#!/bin/sh
RAM=`cat /proc/meminfo | grep "MemFree" | awk -F" " '{print $2}'`
SWAP=`cat /proc/meminfo | grep "SwapFree" | awk -F" " '{print $2}'`
echo -n "${RAM}kb/ram ${SWAP}kb/swap"
[roel#roel ~]$
Ctrl+A ? - show the help screen!
If your friend is in the habit of pressing ^A to get to the beginning of the line in Bash, he/she is in for some surprises, since ^A is the screen command key binding. Usually I end up with a frozen screen, possibly because of some random key I pressed after ^A :-)
In those cases I try
^A s and ^A q block/unblock terminal scrolling
to fix that. To go to the beginning of a line inside screen, the key sequence is ^A a.
You can remap the escape key from Ctrl + A to be another key of your choice, so if you do use it for something else, e.g. to go to the beginning of the line in bash, you just need to add a line to your ~/.screenrc file. To make it ^b or ^B, use:
escape ^bB
From the command line, use names sessions to keep multiple sessions under control. I use one session per task, each with multiple tabs:
screen -ls # Lists your current screen sessions
screen -S <name> # Creates a new screen session called name
screen -r <name> # Connects to the named screen sessions
When using screen you only need a few commands:
^A c Create a new shell
^A [0-9] Switch shell
^A k Kill the current shell
^A d Disconnect from screen
^A ? Show the help
An excellent quick reference can be found here. It is worth bookmarking.
Some tips for those sorta familiar with screen, but who tend to not remember things they read in the man page:
To change the name of a screen window is very easy: ctrl+A shift+A.
Did you miss the last message from screen? ctrl+a ctrl+m will show it again for you.
If you want to run something (like tailing a file) and have screen tell you when there's a change, use ctrl+A shift+m on the target window. Warning: it will let you know if anything changes.
Want to select window 15 directly? Try these in your .screenrc file:
bind ! select 11
bind # select 12
bind \# select 13
bind $ select 14
bind % select 15
bind \^ select 16
bind & select 17
bind * select 18
bind ( select 19
bind ) select 10
That assigns ctrl+a shift+0 through 9 for windows 10 through 19.
Ctrl+A is the base command
Ctrl+A N = go to the ***N***ext screen
Ctrl+A P = go to the ***P***revious screen
Ctrl+A C = ***C***reate new screen
Ctrl+A D = ***D***etach your screen
http://www.debian-administration.org/articles/34
I wrote that a couple of years ago, but it is still a good introduction that gets a lot of positive feedback.
I "must" add this: add
bind s
to your .screenrc, if You - like me - used to use split windows, as C-a S splits the actual window, but C-a s freezes it. So I just disabled the freeze shortcut.
Ctrl+a is a special key.
Ctrl+a d - [d]etach, leave programs (irssi?) in background, go home.
Ctrl+a c [c]reate a new window
Ctrl+a 0-9 switch between windows by number
screen -r - get back to detached session
That covers 90% of use cases. Do not try to show all the functionality at the single time.
Not really essential not solely related to screen, but enabling 256 colors in my terminal, GNU Screen and Vim improved my screen experience big time (especially since I code in Vim about 8h a day - there are some great eye-friendly colorschemes).
The first modification I make to .screenrc is to change the escape command. Not unlike many of you, I do not like the default Ctrl-A sequence because of its interference with that fundamental functionality in almost every other context. In my .screenrc file, I add:
escape `e
That's backtick-e.
This enables me to use the backtick as the escape key (e.g. to create a new screen, I press backtick-c, detach is backtick-d, backtick-? is help, backtick-backtick is previous screen, etc.). The only way it interferes (and I had to break myself of the habit) is using backtick on the command line to capture execution output, or pasting anything that contains a backtick. For the former, I've modified my habit by using the BASH $(command) convention. For the latter, I usually just pop open another xterm or detach from screen then paste the content containing the backtick. Finally, if I wish to insert a literal backtick, I simply press backtick-e.
There is some interesting work being done on getting a good GNU screen setup happening by default in the next version of Ubuntu Server, which includes using the bottom of the screen to show all the windows as well as other useful machine details (like number of updates available and whether the machine needs a reboot). You can probably grab their .screenrc and customise it to your needs.
The most useful commands I have in my .screenrc are the following:
shelltitle "$ |bash" # Make screen assign window titles automatically
hardstatus alwayslastline "%w" # Show all window titles at bottom line of term
This way I always know what windows are open, and what is running in them at the moment, too.
I use the following for ssh:
#!/bin/sh
# scr - Runs a command in a fresh screen
#
# Get the current directory and the name of command
wd=`pwd`
cmd=$1
shift
# We can tell if we are running inside screen by looking
# for the STY environment variable. If it is not set we
# only need to run the command, but if it is set then
# we need to use screen.
if [ -z "$STY" ]; then
$cmd $*
else
# Screen needs to change directory so that
# relative file names are resolved correctly.
screen -X chdir $wd
# Ask screen to run the command
if [ $cmd == "ssh" ]; then
screen -X screen -t ""${1##*#}"" $cmd $*
else
screen -X screen -t "$cmd $*" $cmd $*
fi
fi
Then I set the following bash aliases:
vim() {
scr vim $*
}
man() {
scr man $*
}
info() {
scr info $*
}
watch() {
scr watch $*
}
ssh() {
scr ssh $*
}
It opens a new screen for the above aliases and iff using ssh, it renames the screen title with the ssh hostname.
I like to set up a screen session with descriptive names for the windows. ^a A will let you give a name to the current window and ^a " will give you a list of your windows.
When done, detach the screen with ^a d and re-attach with screen -R
I like to use screen -d -RR to automatically create/attach to a given screen. I created bash functions to make it easier...
function mkscreen
{
local add=n
if [ "$1" == '-a' ]; then
add=y
shift;
fi
local name=$1;
shift;
local command="$*";
if [ -z "$name" -o -z "$command" ]; then
echo 'Usage: mkscreen [ -a ] name command
-a Add to .bashrc.' 1>&2;
return 1;
fi
if [ $add == y ]; then
echo "mkscreen $name $command" >> $HOME/.bashrc;
fi
alias $name="/usr/bin/screen -d -RR -S $name $command";
return 0;
}
function rmscreen
{
local delete=n
if [ "$1" == '-d' ]; then
delete=y
shift;
fi
local name=$1;
if [ -z "$name" ]; then
echo 'Usage: rmscreen [ -d ] name
-d Delete from .bashrc.' 1>&2;
return 1;
fi
if [ $delete == y ]; then
sed -i -r "/^mkscreen $name .*/d" $HOME/.bashrc;
fi
unalias $name;
return 0;
}
They create an alias to /usr/bin/screen -d -RR -S $name $command. For example, I like to use irssi in a screen session, so in my .bashrc (beneath those functions), I have:
mkscreen irc /usr/bin/irssi
Then I can just type irc in a terminal to get into irssi. If the screen 'irc' doesn't exist yet then it is created and /usr/bin/irssi is run from it (which connects automatically, of course). If it's already running then I just reattach to it, forcibly detaching any other instance that is already attached to it. It's quite nice.
Another example is creating temporary screen aliases for perldocs as I come across them:
mkscreen perlipc perldoc perlipc
perlipc # Start reading the perldoc, ^A d to detach.
...
# Later, when I'm done reading it, or at least finished
# with the alias, I remove it.
rmscreen perlipc
The -a option (must be first argument) appends the screen alias to .bashrc (so it's persistent) and -d removes it (these can potentially be destructive, so use at own risk). xD
Append:
Another bash-ism that I find convenient when working a lot with screen:
alias sls='/usr/bin/screen -ls'
That way you can list your screens with a lot fewer keystrokes. I don't know if sls collides with any existing utilities, but it didn't at the time on my system so I went for it.
^A A switches back to the screen you just came from.
Ctrl + A is a great special character for Unix people, but if you're using screen to talk to OpenVMS, then not being able to ^A is going to make you bald prematurely.
In VMS, if you're editing a DCL command prior to execution from the history buffer, Insert mode is off (it has to be for a few reasons I won't get into here) ... to turn it on so you don't over-type your command rather than space things out, you have to hit `^A.