Archive for June 2009
How can you copy a file from host1 through host2, and end up on host3? Here is my quick hack solution for a scp / ssh copy.
cat file.txt | ssh -A user1@host2 ‘cat | ssh -A user@host3 “cat > file.txt”‘
or, you can just set up a tunnel from host1 to host3 like this
ssh -L 9999:host3:22 host2
this will open up the local port 9999 which gets tunnelled through host2 and ends up at host3. Now you can do commands like this.
scp -P 9999 file.txt user2@localhost:file.txt
and there you have it!
How do you change directories in a makefile, run a command and then change back? First off, all the commands that you want to run for that situation must be on the same line, because make executes each line with a new shell (weird huh?). Here is an example…
foo:
pushd /etc; pwd; popd
cd /bin; stat ls; cd -
Remember if you cut and paste the above code, you must change the 4 spaces preceding the line to a real tab, or make will freak out.
Either cd or pushd will work, just remember to link all your commands that belong in that same shell with a semicolon, you can also use &&, or ||, but then you need to make certain that you want the logic that implies.
Happy making!
So, how do you do a search and replace in vim? There are two ways that I know of, the first is a general search and replace in the whole file, and the other is a search and replace in just a selected area.
First the general search and replace. Press escape to make sure that you are in command mode. Then type :%s/FOO/BAR/ and hit enter, to replace all FOOs with BARs in the whole file. Note, this will only modify the first occurrence in each line. To modify every occurrence in each line, simply follow the ending slash with a g, like this :%s/FOO/BAR/g . Now, what if the search or replacement has a ‘/’ in it? You can use another character as your search delimiter, the key is that you must be consistent, like this :%s#FOO#BAR#g is equivalent to the above search and replace command.
To do a search and replace in selected text, first, select some text. For our example, lets grab 3 lines, so press escape just to be sure that you are in command mode, then type V, that will select the current line, then go down (press j or down arrow) and now type : . Lets just pause here and look at the vim command prompt, it now looks weird, like this ‘<,'> . But that is ok, now you can just do the s/FOO/BAR/g command and hit enter. You should have changed all FOOs to BARs, but only in your selected area. YAY! It works!

