Hacker News new | past | comments | ask | show | jobs | submit login

A couple others have mentioned shell tricks. One of my favorites is using Alt-. (in Bash and Zsh) to insert the last argument of the previous command. Press it multiple times and it will cycle through the last argument of all previous commands. It's great for when you want to, say, `ls thing` and then `vim thing`.

Yesterday I went looking for a similar key that would insert a copy of the last argument on the current line, like for when you want to copy or rename a file to a similar name in the same directory, as in `cp some/long/path/to/foo.txt some/long/path/to/foo2.txt`.

I couldn't find a command for this, so I made my own Zsh "widget" and bound it to Alt-/ so now I can type `cp some/long/path/to/foo.txt` and hit Alt-/ to prefill the second arg. I put the code for it up here: https://gist.github.com/dceddia/0a45a88dd61cbdf1be023d0a8f15...




One of my favorite bash tricks is that I have this alias in my bashrc file:

   function goto {
      cd -P ~/.links/"$1";
   }

And then in the `.links` folder I have symbolic links to directories I need to go to often.

Then getting to a frequently used directory is just a `goto X` away.


Ooh, that's cool. You might be interested at z[1]. It kinda fills the same purpose as your trick, except it automatically figures out what are your most used directories by tracking your shell navigation or something.

[1] https://github.com/rupa/z/wiki


Cool. How does that compare with setting CDPATH?


I just looked up CDPATH and my understanding is that the difference between the two is that with CDPATH you are setting directories that you can quickly navigate to subdirectories of.

This is more of a quick link to directories themselves.

Maybe you could do something similar to the alias if you had my `~/.links` directory in your CDPATH, but then you'd still need to add the `-P`every time to get it to follow the symbolic link.

Not something I had ever thought of before honestly.


For `cp some/long/path/to/foo.txt some/long/path/to/foo2.txt` I always liked brace expansion, for example

`cp some/long/path/to/{foo,foo2}.txt`

(Though foo{,2}.txt is even shorter)


Gonna have to try doing this, I usually cd to the source/target directory in these situations, then copy it and use cd - to get back quickly.


I think !$ will do similar, at least in bash.

(I'm on a phone and normally rely on muscle memory so it might be the wrong symbols).


Maybe I misunderstood, or it does something different in zsh, but it seems like it's inserting the command itself:

    ls foo.txt !$

    ls: ls: No such file or directory
    foo.txt


`!$` is right, though it isn't interactive like `Alt+.` is.


Yes it is, you just need to press Alt-^ to do history expansion on it.


That doesn't cycle through the previous arguments.


Correct.


Yes, I love this one! I'll check out the widget. Sounds cool! I have the same problem (it's usually changing filenames, so I just use fzf twice, then modify the second filename).

There was one bash feature along the same lines of this. It was like bringing up the previous command in a shell script, so you could modify it freely, then run it. It was useful for long command, I think. Haven't tried learning it though. Just saw a couple wizard colleagues using it, and it seemed cool.


Thanks! I didn't know about Alt-. I'm using powershell and it works there too.


I assume you're talking about only on linux? On a mac `alt .` and `alt /` are mapped to characters ≥ and ÷


Esc-. is more portable. Works on mac too.


Yea, this is what I use.


I'm on a Mac using iTerm, but I think I've tweaked some setting about meta keys somewhere in preferences.


$_ will do the same thing in bash as in "ls thing" and then "vim $_" (but only works for arguments from the last command)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: