Question

I am not sure about the exact purpose of the following Rampion's code. It should apparently execute command(s) at the cursor position.

# man-word.screen

# prevent messages from slowing this down
msgminwait 0

# copy word starting at cursor
copy                         # I am not sure why we need this
stuff " e "

# open a new window that waits for a word to run man on
# (and uses 'read' to pause on error)
screen -t man /bin/sh -c 'cat | xargs man || read'       # option -c seems to mean execute

# feed that window the copied word
# be sure to enter '^M' as 'CTRL-V ENTER' and '^D' as 'CTRL-V CTRL-D' (in vim)
paste '.'
# should display as 'stuff "^M^D"'
stuff " "

# turn message waiting back on
msgminwait 1

# vi: ft=screen

The code is bound under ^g such that

bindkey -m ^f source /Users/masi/bin/screen/edit-file-under-cursor.screen

which is same as

bind f source /Users/masi/bin/screen/edit-file-under-cursor.screen

I run the code as my cursor is at the beginning of the following line

vim ~/.zshrc

I get a new buffer such that

alt text http://files.getdropbox.com/u/175564/screen-rampion.png

What is the purpose of the command?

Was it helpful?

Solution

So the command doesn't run arbitrary code. All it does is run man <whatever> in a new screen window if your cursor was over the word <whatever>.

The reason the copy command is there is that you need to tell screen that you want to copy something. You may not always be in screen's copy mode when over a path - for example, you could be using vim, and have vim's cursor over a path. If you are already in copy mode, then it's a no-op.

screen -t man /bin/sh -c 'cat | xargs man || read'
  • screen :: open a new window
  • -t man :: give it a title of man
  • /bin/sh -c 'cat | xargs man || read' :: run this command in the new window, rather than opening the default shell in the new window.
    • /bin/sh :: a shell program
    • -c 'cat | xargs man || read' :: run the given string as a script, rather than opening in interactive mode
    • cat | :: wait for user input (ended with a newline and a CTRL-D), then pipe it as user input to the next command
    • xargs man :: call man, using whatever's read from standard input as command line arguments for man
    • || read :: if the previous commands return non-zero, wait for the user to hit enter

From your output it looks like

  1. The -c part of the command isn't getting run, since it looks like a new shell (the $ is a hint).
  2. The stuff "^M^D" part wasn't transcribed correctly. The next non-comment line after paste '.' should be entered, keystroke for keystroke, as :

    's', 't', 'u', 'f', 'f', ' ', '"', <CTRL-V>, <ENTER>, <CTRL-V>, <CTRL-D>, '"'
    

If you had downloaded the file, rather than transcribed it, you may not have those issues.

Also, bindkey -m ^f is not the same as bind f. And neither bind a command to ^g.

  • bindkey -m ^f binds a command to <CTRL-f>, but only when in copy mode.
  • bind f binds a command to <CTRL-A> f, in all modes.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top