8 ms·
An example of some of the vi commands that less(1) has adopted. You can also: * page forward and backward, including using numeric prefixes to indicate a lin
by bch 12y ago
An example of some of the vi commands that less(1) has adopted. You can also:
* page forward and backward, including using numeric prefixes to indicate a lineno or indicate "repeat 'n' times".
* launch vi if less(1) is working on a file (versus (eg) stdout of some process)
* search fwd/backward
* start examining a new file w/o leaving less(1)
* ...
- pjungwir 12y agoJust the other day I was looking for a way to do this in less: yank 10 lines, open a new file, paste the 10 lines, save the file. Is that possible? My Googling last week didn't find a good solution.
- bch 12y agoIf you wanted to grab 10 lines from /var/log/messages, starting at the first line that has "pjungwlr" in it, you could: $ less /var/log/msgs /pjungwlr^M ^G (note line informational line numbers) v (launch vi) [double check your line #s, etc] :d1,. (delete from 1st line, to current line) 10j (go down 10 lines) :d,$ (delete to EOF) :w my_newfile :q
- technofiend 12y agoThis is such a common problem there's a stackoverflow article on it. http://stackoverflow.com/questions/17908555/printing-with-sed-or-awk-a-line-following-a-matching-pattern http://stackoverflow.com/questions/17908555/printing-with-se... (Actually, there's probably more than one despite their really good duplication grooming, but this was the first hit.)
- pjungwir 12y ago`v` was the missing key for me. Looks like it even preserves my position in the file. Thanks!
- kragen 12y agoUsually I `ma|acat > newfile` in less, which saves by default a whole screenfull. There must be a better way to do this in less.
- pjungwir 12y agoAnother less-vs-vi question: As a junior programmer I got chewed out by my IT department because I was examining a (production) log file in vi. The sysadmin told me I should use less (actually more---this was on Solaris in ~2001). To this day I only read log files with less, but I've never figured out his objection. Negatives to using vi I can imagine are: - I might write to the log file. That seems like a real worry, although I could also say `vi -R` to prevent it. - Starving the production system of memory. I'm pretty sure he expressed this concern. Any insight into whether it is legit? Is less actually any better? Obviously you really should have a log shipping & aggregation service so you can read logs offline, etc, but not every project is large enough for that, nor every org organized enough. So for the sake of argument my premise is, "Assuming you want to read a log on production . . ."
- vbezhenar 12y agoOn large files less fires up immediately and vim is quite slow. I think that vim is loading an entire file in the memory while less just loads visible chunk. May be other vi implementations are more efficient with large files. Besides — vim really shouldn't be used on log files. It's editor, not viewer.
- EdiX 12y ago> I think that vim is loading an entire file in the memory while less just loads visible chunk No, vim also only keeps part of the file loaded, however it will try to count how many lines the file has.
- pjungwir 12y ago> Besides — vim really shouldn't be used on log files. It's editor, not viewer. I think the two concerns I stated are probably legit, but this one is boring to me. Vim is nicer than less for reading files. I have more navigation commands. I can yank part of the file and save it somewhere else. I can pipe a range of lines through awk/etc. I can switch between files. I can split my screen. Etc. Some of these are probably available in less too (more than more(1) supported in 2001), but I doubt all, and I already know the commands in vim. I'm interested in not clobbering my logs and not crashing the server, but if you tell me vim is an editor not a viewer, I'll ask Why? Btw re-reading my words I don't mean to sound combative. But the point of my original question was to understand. I've been cargo-culting "use less for logs" for 14 years already.
- dredmorbius 12y ago'&pattern' filtering is huge for me. Display only lines which match the pattern; lines which do not match the pattern are not displayed. If pattern is empty (if you type & immediately followed by ENTER), any filtering is turned off, and all lines are displayed. While filtering is in effect, an ampersand is displayed at the beginning of the prompt, as a reminder that some lines in the file may be hidden.