5

Say I am in dired and the pointer is on a file called test which contains:

1. 
2. 
3. 
4. 
5. 
6. 
7. 
8. 
9. 
10. 
11. 
12. 
13. 
14. 
15. 
16. 
17. 
18. 
19. 
20. 

I type ! to run a shell command on this file. I type tac | head -n 2

I get :

1.
2.

But I expected:

20.
19.

As if I has run tac test | head -n 2 in the shell.

What am I misunderstanding and what is the correct way to get the behaviour I wish?

user27815
  • 239
  • 1
  • 6

1 Answers1

6

By default, dired-do-shell-command inserts the file name at the end of the command you enter. In effect, you are doing this:

tac | head -n 2 test

Without an argument, tac doesn't produce any output for your pipe, so head returns the first two lines of test and then you're done.

You can use the ? symbol to tell dired to put your filename at that location in the command. i.e.,

tac ? | head -n 2
Tyler
  • 21,719
  • 1
  • 52
  • 92
  • 2
    The docstring of `dired-do-shell-command` explains how to interpolate the relevant file name into the resulting command. – Basil Aug 15 '18 at 14:34