Live data from Hacker News

Assorted less(1) tips

blog.thechases.com

41–50 of 56 posts

Re: Assorted less(1) tips

#41
post #23

> The ! lets you invoke an external command. Also useful for privilege escalation... If a script running as root uses less (or vi), just do "!bash" and you have a root shell. Note that systems that let you do this are usually pretty weak, and there are often many other ways to get root access, but this is a particularly simple one that I used a few times in the past.

You can disable things like this by setting the environment variable `LESSSECURE` to `1`. You can also compile `less` without these features [0], but I don't think most distros provide a restricted `less` by default.

[0]: https://github.com/gwsw/less/blob/master/README#L67-L70

Re: Assorted less(1) tips

#42
One thing I find myself wanting is an emacs-lite alternative to less/more. Something that would let me page though a file, but use my emacs bindings to search, filter, browse etc. I've already built up my muscle memory, so it would be great if I could reuse it for this purpose.

Thought I'd tag along to this submission and see if anyone has a recommendation?

Re: Assorted less(1) tips

#43
post #42

One thing I find myself wanting is an emacs-lite alternative to less/more. Something that would let me page though a file, but use my emacs bindings to search, filter, browse etc. I've already built up my muscle memory, so it would be great if I could reuse it for this purpose. Thought I'd tag along to this submission and see if anyone has a recommendation?

Answering my own question after a brief wiki walk from most (referenced in another comment) -> pagers -> Terminal Pager.

`emacs -nw -e "(view-mode)"`

Re: Assorted less(1) tips

#44
Maybe I missed it, is there no love for the piping to an external command?

I set a mark, move to somewhere else, then save the area between where I am and the mark: ma(assign mark "a" to position), jjj(move three lines away), |a(pipe from current-position to the "a" mark then a ! prompt appears so enter...) cat >somefile (which dumps the selected text, cur-pos to mark "a", into somefile).

That was great for saving snippets of news or emails.

Also, the -j setting. Sets the line position for searches so context is available, eg using -j8 means the search is 8 lines from the top of the screen.

Re: Assorted less(1) tips

#45
There's a way to make `^q` quit `less` and not clear the screen (like `less -X`), while `q` quits `less` and clears the screen (like normal `less`).

1. Do `echo '^q toggle-option -redraw-screen\nq' >> ~/.config/lesskey`

2. Make sure `less` is invoked without `-X` (or with `-+X` if you want to be sure).

This `^q` command is particularly useful for `git log` output and other things where you might need to refer back to them in the next terminal command you do. (In fact, `git` uses `less -FRX` by default, so you'd need to override its config to use `less -FR` instead for the above to work as intended). The `q` command is useful when you don't want to lose what you had on the screen before invoking `less`.

Re: Assorted less(1) tips

#46

Maybe I missed it, is there no love for the piping to an external command? I set a mark, move to somewhere else, then save the area between where I am and the mark: ma(assign mark "a" to position), jjj(move three lines away), |a(pipe from current-position to the "a" mark then a ! prompt appears so enter...) cat >somefile (which dumps the selected text, cur-pos to mark "a", into somefile). That was great for saving sn…

I use the piping feature of less to add some interactivity to git-log.

When a commit is "selected" (at the top line of the screen), usually after a series of n/N, I can press a shortcut that invokes an action on this commit.

Currently, I use it for two things:

1. Running git-show on a commit I'm interested in. The cool thing is that once I quit the git-show's less, I'm back to where I was in git-log's less. They stack.

2. fixup-ing a commit, after verifying with the command from 1. that it really is the one I want. I've had enough problems with git-absorb and git-fixup that I prefer to do it myself.

I detect when a particular command is running[1] and set up keyboard shortcuts that send key sequences to less and ultimately lead to the top line of the screen being piped to a short script of mine that extracts the commit hash and does something with it.

[1]: via a debug trap in bash, which sets the terminal title, which, in turn, is detected by keyd-application-mapper; other setups are possible, I used to use tmux for that.

Re: Assorted less(1) tips

#47
post #35

Earlier quoted context omitted.

I can while less is running to background that process using the shell, so the shell is clearly not completely gone. I might be misremembering, but I think I just had to rebind in zsh to get less working.

> I can while less is running to background that process using the shell, so the shell is clearly not completely gone. The shell isn’t gone , but it isn’t active either from what I understand. The function of converting the user’s typing ^Z on a terminal (or a ^Z arriving on the master end of a pseudoterminal) into a SIGTSTP signal to the terminal’s foreground process group is[1] a built-in function of the kernel , m…

Whew! Advanced Unix system programming level stuff. I've dabbled a bit in that field, in C, on Unix, some older versions on PCs. It was fun. Any recommendation for a tutorial style book or site or blog on the subject, other than man pages and the Kerrisk book (TLPI, which is more of a reference), for Linux?

Re: Assorted less(1) tips

#48
My biggest problem with less(1) is that the regex engine is unreasonably slow. When processing large files, I frequently need to search with grep (or more recently, rip-grep) with large -A/-B buffers, and then pipe that through less, because the regex engine in less won't find what I want on any reasonable time scale.

Re: Assorted less(1) tips

#50

Also -X or --no-init " ... desirable if the deinitialization string does something unnecessary, like clearing the screen." I prefer to not clear the screen. I usually want to continue to refer to something or even copy/paste from the content to my current command line.

If you want the `-X` behavior only some of the time, see https://news.ycombinator.com/item?id=46472859. (Maybe I should've posted it in this thread)
Post reply on HN