jueves, febrero 28, 2013

bash tip: get first and last arguments of last executed command


Let's say you execute the following command on bash:


# cp /long/source/path/file /long/destination/path/directory/


If in the next command you need to edit the file or change to the destination directory, then you can use:


 a.- "ctrl+alt+y" (simultaneously press ctrl and alt and y) which recovers the first argument of the previously executed command.

 b.- "ESC+." (simultaneously press ESC and dot), which recovers the last argument of the previously executed command.


So, you can do either:

# vim <ctrl+alt+y>

(becomes automatically "vim /long/source/path/file")

or:

# cd <ESC+.>


(becomes automatically "cd /long/destination/path/directory/")


Remember this and save lots of keystrokes!


EDIT: David Muriel shares with as the way of doing it without key combinations, just using the following direct arguments:

First argument of the previous command -->   !!^
Last argument of the previous command  -->   !!$
Nth argument of the previous command ---->   !!:n

So you can directly write:

# vim !!$

It's quite easy to remember, !! for "the argument" followed by a identifier similar to regular expressions (^ = start, $ = end) or :n = Nth ...

Thanks, David!

No hay comentarios: