文章摘要
这篇文章分享了关于Linux命令行工具less的使用技巧,包括如何同时查看多个文件内容、在管道中使用less等实用方法,旨在帮助用户更高效地使用这个工具。
文章总结
《less实用技巧集锦 | Tim的博客》
文章地址:https://blog.thechases.com/posts/assorted-less-tips/ 发布时间:2025年11月15日
本文汇总了Unix/Linux下常用分页工具less的高级使用技巧,主要包含以下核心内容:
一、多文件操作
1. 支持直接打开多个文件:less file1 file2 *.txt
2. 运行时添加文件::e newfile
3. 文件导航命令:
- :n 下一文件
- :p 上一文件
- :x 返回第一个文件
4. 删除当前文件::d
二、高效导航
1. 跳转行号:[行号]G(如3141G)
2. 跳转百分比:[百分比]%(如75%)
3. 增强搜索功能:
- /!pattern 反向匹配
- /@*pattern 跨文件搜索
三、实用功能
1. 行过滤:&pattern(类似grep)
2. 书签系统:
- 设置:m[字母]
- 跳转:'[字母]
3. 括号匹配:支持自动跳转配对的括号
四、运行时配置
1. 动态切换选项(无需退出):
- -S 切换自动换行
- -N 显示行号
- -R ANSI颜色支持
2. 环境变量预设:通过$LESS设置默认参数
五、其他功能
1. 执行外部命令:!command
2. 编辑当前文件:v(调用$VISUAL编辑器)
3. 输出内容:o保存文件
作者特别推荐在查看日志文件、代码和手册页时应用这些技巧,可以显著提升工作效率。文章最后提到虽然less支持标签(tags)等进阶功能,但实际使用频率较低。
(注:原文中的导航菜单、社交媒体链接等非核心内容已精简,保留了所有技术细节和实用建议)
评论总结
以下是评论内容的总结:
替代工具推荐
- 推荐使用
most和glow作为less的替代工具 - "There's also most as an alternative pager, and also glow..." (评论1)
- "I like less and found that...ov can be a good modern alternative to it." (评论9)
- 推荐使用
实用过滤技巧
- 使用
&和&!进行日志过滤 - "using
&to filter what I want to read and&!to filter-out..." (评论2) - "you could use
grep -vthen pipe which is way faster..." (评论2)
- 使用
显示设置优化
- 推荐使用
-X/--no-init避免清屏 - "I prefer to not clear the screen. I usually want to continue to refer to something..." (评论3)
- "the deinitialization string does something unnecessary, like clearing the screen." (评论3)
- 推荐使用
文件追踪功能
- 推荐使用
less +F实现类似tail的追踪功能 - "
less +Fstarts less following stdin..." (评论4) - "Hitting
Fstarts following again." (评论4)
- 推荐使用
Vim用户习惯
- Vim用户会自然期待某些功能
- "I expect to be able to search when pressing / in terminal programs..." (评论5)
数据保存技巧
- 使用
s保存管道数据 - "press
sto save data from a pipe to a file..." (评论6)
- 使用
配置自定义
- 通过
.lesskey文件自定义快捷键 - "Less can be configured with a ~/.lesskey file..." (评论7)
- 通过
性能优化技巧
- 使用
-L跳过预处理,Ctrl-R进行字面搜索 - "Using -L skips all that." (评论8)
- "Ctrl-R as the first character of a search string will search for that literal string..." (评论8)
- 使用
安全警告
!命令可能被用于权限提升- "just do '!bash' and you have a root shell." (评论11)
幽默评论
- 关于技巧数量的玩笑
- "You're going to need more less tips to match that." (评论12)