各种小技巧 (1)
Assorted less(1) tips

原始链接: https://blog.thechases.com/posts/assorted-less-tips/

## 少即是多:`less` 工具实用技巧 本摘要汇集了使用 `less` 命令行工具的实用技巧,超越基本的文件查看。`less` 可以直接处理多个文件(例如,`less file1.txt file2.c`),或在会话中途添加文件,使用 `:e filename`。使用 `:n`(下一个)、`:p`(上一个)或 `:x`(倒回第一个)在打开的文件之间导航。可以使用 `:d` 从列表中删除文件。 为了在文件*内部*导航,使用 `[count]G` 跳转到特定行,或使用 `[count]%` 进行百分比偏移。强大的搜索功能包括 `/pattern`(向前)、`?pattern`(向后),以及修饰符,例如 `/!pattern`(不匹配的行)和 `/@*pattern`(从头开始在所有文件中搜索)。 使用 `&pattern`(匹配)或 `&!pattern`(不匹配)过滤显示的行。书签(`m[letter]` 标记,`'[letter]` 跳转)对于导航长文档(如 man 页面)很有用。括号匹配(键入 `(`、`[` 或 `{`)跳转到相应的闭合字符,反之亦然。 最后,通过键入连字符后跟选项,在 `less` *内部* 切换选项,例如单词换行 (`-S`) 或行号 (`-N`)。通过 `$LESS` 环境变量自定义默认行为。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 工作 | 提交 登录 杂项 小技巧 (thechases.com) 10 分,by todsacerdoti 2 小时前 | 隐藏 | 过去 | 收藏 | 2 评论 etra0 4 分钟前 | 下一个 [–] 最近我调试长日志文件时经常使用的一个技巧是使用 `&` 过滤我想读取的内容,使用 `&!` 过滤掉无用的内容(它们支持正则表达式)。承认吧,有时它们有点慢,而且当然可以使用 `grep -v` 然后管道传输,那会快很多,但它们有时可以帮助我从日志文件中去除噪音,尤其是在你事先不知道该过滤什么的时候。编辑:这在原文中。回复 fragmede 7 分钟前 | 上一个 [–] 使用 -R 退出如果文件小于屏幕大小。还有 most 作为替代分页器,以及 glow(当然,我的分支更好)在终端中渲染 md 文件。回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

In a recent discussion on Reddit I shared a number of tips about the common utility less(1) that others found helpful so I figured I'd aggregate some of those tips here.

Operating on multiple files

While most folks invoke less at the tail of a pipeline like

 command | less
Invoking less in a pipeline
you can directly provide one or more files to open
 less README.txt file.c *.md
Invoking less directly

Adding files after starting

When reading a document, sometimes you want to view another file, adding it to the file list. Perhaps while reading some C source code you want to also look over the corresponding header-file. You can add that header-file to the argument list with :e file.h

You can navigate between multiple files using :n to go to the next file in the argument-list, and :p for the previous file. You can also use :x to rewind to the first file in the argument-list similar to how :rewind behaves in vi/vim.

Removing files after starting

While I rarely feel the need to, if you have finished with a file and want to keep your argument list clean, you can use :d to delete the current file from the argument-list.

Jumping to a particular line-number

Use «count»G to jump to a particular line-number. So using 3141G will jump to line 3141. It helps to display line numbers.

Jumping to a particular percentage-offset

Similarly, using «count»% jumps to that percentage-offset of the file. So if you want to go to ¾ of the way through the file, you can type 75% to jump right there.

Searching

While many folks know you can search forward with /«pattern» and some people know you can use ?«pattern» to search backwards, or use n/N to search again for the next/previous match, less provides modifiers you can specify before the pattern to modify its behavior:

!
Find the next line that doesn't match the pattern
*
search across multiple files, starting from the current location in the current file
@
rewind to the first file and search from there
@*
rewind to the first file and search from there across multiple files

Thus you would use /@*«pattern» to search for "pattern" starting with the first file.

Filtering lines

Using & lets you specify a pattern and filter the displayed lines to only those matching the pattern, much like an internal grep command. If you modify it with !, so it will display only those lines that do not match the pattern, like &!«pattern». I find this particularly helpful for browsing log-files.

Bookmarking

You can bookmark points in a file with m followed by a letter, then jump back to that bookmark with ' followed by the same letter. These apply globally across all open files, so if you ma in the third file, then navigate away to other files, using 'a will take you back to the marked location in that third file. I use marks most when reading man-pages, dropping one mark at the OPTIONS section such as mo, and another at the EXAMPLES section, such as me, then bounce back and forth between them with 'o and 'e. While you can use any of the 26 lowercase or uppercase letters (for a total of 52 marks), I rarely use more than two or three either in alphabetical order ("a", "b", "c"), or assigning mnemonics like in the man-page example above.

Bracket matching

If the first line on the screen contains a (, [, or {, typing that character will jump to the matching/closing character, putting it on the bottom line of the screen. Similarly, if a closing ), ], or }, character appears on the last line, typing that closing character will jump to the matching/opening character, putting it at the top of the screen. I find it a little disorienting if they fall less than a screen-height apart because what feels like a forward motion to find the next matching close-bracket might actually result in shifting the screen down rather than up which feels backwards.

While I don't use it much, you can also specify match-pairs using alt+ctrl+f or alt+ctrl+b followed by the opening/closing pair of characters such as alt+ctrl+f<> to define a "<"…">" pair and jump between them in a manner similar to the (/), [/], and {/) motions.

Toggling options without restarting

While the man-page documents many flags you can pass on the command-line, you can also toggle boolean options from inside less. I find this particularly helpful when I've fed the output of a long-running process to less and don't want to re-run it because it will take a long time. Instead of quitting, you can type a literal - followed by the option you want to change. I most commonly want to toggle word-wrap for long lines, so instead of quitting and adding -S at the end of my pipeline, I can type -S directly in less. Options I commonly toggle:

-S
word-wrap (mnemonic "splitting long lines")
-G
search-highlighting
-i/-I
smart-case/case-sensitivity for searches
-R
ANSI-color escaping
-N/-n
show/hide line-numbers

Running external commands

The ! lets you invoke an external command. I don't do this often, but occasionally I want some simple reference like the current date (!date) or to do some simple math (!bc).

Default options with $LESS

You might find yourself regularly setting a common group of options so you can put those in your environment (usually in your shell startup file like ~/.bashrc) like LESS="-RNe" if you want to show ANSI colors, show line-numbers, and exit automatically when you reach the end of the file.

Other misc

less has a few other corners that I've never really used, but figured I'd document here:

Tags

While I've used tags in vi/vim to easily jump between definitions. However, even though less provides support for tags generated by ctags. I've never found cause to use them.

Editing the current document

The v command will open your $VISUAL editor on the current document.

"Log" output

less lets you redirect the output it has gathered from stdin to a file using the o command (or the O command to overwrite an existing file). This might come in handy because less won't let you edit stdin in an external editor but you can write it directly to a file.

联系我们 contact @ memedata.com