Let's say we list /usr/bin with ls – this may look like:

CC                file2c            man               sscop
Mail              find              mandoc            ssh
addftinfo         finger            manpath           ssh-add
addr2line         flex              merge             ssh-agent

but we could also use ls -1, and we get:


A list with all filenames, each of them in a single line. The structure of the output is: filename, newline (\n), …

This we can pipe to less: ls -1 | less.

Now, is it possible to easily apply a command to the string contained in the current line?

How exactly this is done is irrelevant, just the number of steps should be small. It could be by using ! in less (this doesn't seem possible?) or by somehow getting the string contained in the current line into a shell variable etc.

Under Xorg this is easy of course, by just using the middle mouse button paste. But in text mode, can one do it in a not too complicated way?

  • What does pasting have to with that? Don't you rather want to copy the top line? – Arkadiusz Drabczyk Sep 27 '17 at 8:45
  • OK, I see. You can easily do what you want with GNU screen or tmux but you'd have to have it started before opening less. – Arkadiusz Drabczyk Sep 27 '17 at 8:53
  • @don_crissti It's in all details described in the question and it seems the two answering posters understood it, too. What's still unclear about the question? – viuser Sep 27 '17 at 10:58
  • @don_crissti what pick can do (or mc): Interactively searching in a directory for a filename and then applying a command to the found file. But obviously, doing it with basic unix tools. To a certain degree it can be done with vim, which is nearly always installed, yet only in a cumbersome way. – viuser Sep 28 '17 at 0:05

You can use the | command to pipe part of the file through another program, but that part is always at least two lines. You can hack it together with marks and tail, though:

  1. With the line you want at the top, press ma (or any other letter for the second one).
  2. Scroll up one line with k or Up.
  3. Press | and then the letter you chose in place of a above.
  4. Type tail -n 1 | ..., where ... is any command that will be given that one line as its standard input.

tail -n 1 will give us only the second line, which is the one we care about. ... gets that line to do with as it wishes.

If you want to use the line as an argument to a command instead of standard input, you can use xargs or another wrapper:

tail -n 1 | xargs ls -l

If that command displays anything, it won't remain visible on the screen, but you can pipe it to less again:

tail -n 1 | xargs ls -l | less

This will be a nested less: you quit it, and then you're back in the original less, and can repeat the process.

If the command is interactive somehow, like a text editor, and doesn't like having its standard input be a pipe or null, you can use a sh process in the middle to run the command and reopen the tty:

xargs sh -c 'vim "$@" < /dev/tty' sh

BSD xargs also has a -o option to do the same thing.


One way to achieve this would be:

  1. Move to the desired start of selection (for example, by searching for it).
  2. m followed by some letter, say X to set mark.
  3. Move to the end of the selection.
  4. |X (this will switch to ! prompt, where you are expected to write a command which will be given the the selection on the stdin.
  5. Type the command and press RET

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.