bashunixvimlsunix-head

Passing the most recent file in a directory to a program


What would be the correct way to do the following:

$ ls -t | head -n1 | vim -

Currently, this will read the 'filename' into vim, but I'm looking at actual open that filename that's passed to it as a string. How would this be done?

Without opening the file we get:

$ ls -t | head -n1
2020-11-05.txt

Solution

  • You can pass it like this:

    $ vim "$(ls -t | head -n1)"