I want to copy matched line that is too long that cannot select with the mouse in less, so does there exist another way in less? For example

File: test.txt content is:

line1: 12312sdfas23414123
liie2: aaaaaxxxxxxxxxxxxxxxxxxxxxxxfsdfsdfasdf
line3: 134sdgas1212

I will first search with 'aaaaa' and then how to copy that line to my clipboard? After search, I found this https://stackoverflow.com/questions/26626910/copy-specific-line-from-less , but I don't know how to apply this to my requirement. Thanks in advance

update: Does there have some native ways? Because I found my Centos 6 doesn't install the xclip or pbcopy.

3 Answers 3


less has a v command that will run the editor giving it the filename and current line number. You can override the command it runs with something like:

export LESSEDIT='sed -n "%lp" %g | xclip -selection clipboard'

Now when you type v, it will use sed to print (p) the current line (%l) from the current file (%g), and pipe it into xclip, for example. You can write a tiny shell script, say myless, to hold this setting and run less for you:

export LESSEDIT='sed -n "%lp" %g | xclip -selection clipboard'
exec less "$@"

Don't forget to chmod +x myless to make it executable, and put it somewhere in a directory in your PATH.


The link you send has the answer but I can include the exact sequence you are probably looking for:

The less commands to do that are:

  1. /aaaaax <enter>
  2. | <enter>
  3. head -1 | xclip -selection clipboard <enter>

And this is what those commands are saying/doing

  1. Search for the string 'aaaaax' which will put it at the top of the page/screen
  2. "pipe" a mark. Pressing enter without an explicit mark just pipes this screen's contents
  3. head -1 trims the input to just the first line. In this case the line we want
  4. xclip -selection clipboard puts that line on the system clipboard where you can paste it wherever
  • 1
    I would add that since some version less supports & command which is like / but displays only the lines which matched hiding all that did not match. This allows for saving matched lines if there are more than a single match.
    – kostix
    Commented Jun 27, 2023 at 18:22

If you don't have to rely on less I recommend using grep.

You can filter the output with grep and may find your string faster.

  • 2
    I truly need the less, because there have multiple occurrences of search keywords in my file, so I should first locate the position that I want.
    – Yang Xu
    Commented Jun 27, 2023 at 6:43
  • @YangXu are you aware that you can show the context around the match with grep? See -A, -B and -C in man grep.
    – FelixJN
    Commented Jun 27, 2023 at 7:49
  • @FelixJN Yeah, I know, but I think less is more fits my situation since I can locate what I want first, and then choose which I want as there will vary cases in that I cannot filter results with one keyword.
    – Yang Xu
    Commented Jun 27, 2023 at 12:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .