mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
Merge pull request #10434 from z52527/master
20180828 A Cat Clone With Syntax Highlighting And Git Integration.md
This commit is contained in:
commit
67d984ae2a
@ -1,170 +0,0 @@
|
|||||||
Translating by z52527
|
|
||||||
|
|
||||||
|
|
||||||
A Cat Clone With Syntax Highlighting And Git Integration
|
|
||||||
======
|
|
||||||
|
|
||||||
![](https://www.ostechnix.com/wp-content/uploads/2018/08/Bat-command-720x340.png)
|
|
||||||
|
|
||||||
In Unix-like systems, we use **‘cat’** command to print and concatenate files. Using cat command, we can print the contents of a file to the standard output, concatenate several files into the target file, and append several files into the target file. Today, I stumbled upon a similar utility named **“Bat”** , a clone to the cat command, with some additional cool features such as syntax highlighting, git integration and automatic paging etc. In this brief guide, we will how to install and use Bat command in Linux.
|
|
||||||
|
|
||||||
### Installation
|
|
||||||
|
|
||||||
Bat is available in the default repositories of Arch Linux. So, you can install it using pacman on any arch-based systems.
|
|
||||||
```
|
|
||||||
$ sudo pacman -S bat
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
On Debian, Ubuntu, Linux Mint systems, download the **.deb** file from the [**Releases page**][1] and install it as shown below.
|
|
||||||
```
|
|
||||||
$ sudo apt install gdebi
|
|
||||||
|
|
||||||
$ sudo gdebi bat_0.5.0_amd64.deb
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
For other systems, you may need to compile and install from source. Make sure you have installed Rust 1.26 or higher.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Then, run the following command to install Bat:
|
|
||||||
```
|
|
||||||
$ cargo install bat
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
Alternatively, you can install it using [**Linuxbrew**][2] package manager.
|
|
||||||
```
|
|
||||||
$ brew install bat
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
### Bat command Usage
|
|
||||||
|
|
||||||
The Bat command’s usage is very similar to cat command.
|
|
||||||
|
|
||||||
To create a new file using bat command, do:
|
|
||||||
```
|
|
||||||
$ bat > file.txt
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
To view the contents of a file using bat command, just do:
|
|
||||||
```
|
|
||||||
$ bat file.txt
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
You can also view multiple files at once:
|
|
||||||
```
|
|
||||||
$ bat file1.txt file2.txt
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
To append the contents of the multiple files in a single file:
|
|
||||||
```
|
|
||||||
$ bat file1.txt file2.txt file3.txt > document.txt
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
Like I already mentioned, apart from viewing and editing files, the Bat command has some additional cool features though.
|
|
||||||
|
|
||||||
The bat command supports **syntax highlighting** for large number of programming and markup languages. For instance, look at the following example. I am going to display the contents of the **reverse.py** file using both cat and bat commands.
|
|
||||||
|
|
||||||
![](https://www.ostechnix.com/wp-content/uploads/2018/08/bat-and-cat-command-output-comparison.png)
|
|
||||||
|
|
||||||
Did you notice the difference? Cat command shows the contents of the file in plain text format, whereas bat command shows output with syntax highlighting, order number in a neat tabular column format. Much better, isn’t it?
|
|
||||||
|
|
||||||
If you want to display only the line numbers (not the tabular column), use **-n** flag.
|
|
||||||
```
|
|
||||||
$ bat -n reverse.py
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
**Sample output:**
|
|
||||||
![](https://www.ostechnix.com/wp-content/uploads/2018/08/bat-command-output-3.png)
|
|
||||||
|
|
||||||
Another notable feature of Bat command is it supports **automatic paging**. That means if output of a file is too large for one screen, the bat command automatically pipes its own output to **less** command, so you can view the output page by page.
|
|
||||||
|
|
||||||
Let me show you an example. When you view the contents of a file which spans multiple pages using cat command, the prompt quickly jumps to the last page of the file, and you do not see the content in the beginning or in the middle.
|
|
||||||
|
|
||||||
Have a look at the following output:
|
|
||||||
|
|
||||||
![](https://www.ostechnix.com/wp-content/uploads/2018/08/cat-command-output.png)
|
|
||||||
|
|
||||||
As you can see, the cat command displays last page of the file.
|
|
||||||
|
|
||||||
So, you may need to pipe the output of the cat command to **less** command to view it’s contents page by page from the beginning.
|
|
||||||
```
|
|
||||||
$ cat reverse.py | less
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
Now, you can view output page by page by hitting the ENTER key. However, it is not necessary if you use bat command. The bat command will automatically pipe the output of a file which spans multiple pages.
|
|
||||||
```
|
|
||||||
$ bat reverse.py
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
**Sample output:**
|
|
||||||
|
|
||||||
![](https://www.ostechnix.com/wp-content/uploads/2018/08/bat-command-output-1.png)
|
|
||||||
|
|
||||||
Now hit the ENTER key to go to the next page.
|
|
||||||
|
|
||||||
The bat command also supports **GIT integration** , so you can view/edit the files in your Git repository without much hassle. It communicates with git to show modifications with respect to the index (see left side bar).
|
|
||||||
|
|
||||||
![](https://www.ostechnix.com/wp-content/uploads/2018/08/bat-command-output-2.png)
|
|
||||||
|
|
||||||
**Customizing Bat**
|
|
||||||
|
|
||||||
If you don’t like the default themes, you can change it too. Bat has option for that too.
|
|
||||||
|
|
||||||
To list the available themes, just run:
|
|
||||||
```
|
|
||||||
$ bat --list-themes
|
|
||||||
1337
|
|
||||||
DarkNeon
|
|
||||||
Default
|
|
||||||
GitHub
|
|
||||||
Monokai Extended
|
|
||||||
Monokai Extended Bright
|
|
||||||
Monokai Extended Light
|
|
||||||
Monokai Extended Origin
|
|
||||||
TwoDark
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
To use a different theme, for example TwoDark, run:
|
|
||||||
```
|
|
||||||
$ bat --theme=TwoDark file.txt
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
If you want to make the theme permanent, use `export BAT_THEME="TwoDark"` in your shells startup file.
|
|
||||||
|
|
||||||
Bat also have the option to control the appearance of the output. To do so, use the `--style` option. To show only Git changes and line numbers but no grid and no file header, use `--style=numbers,changes`.
|
|
||||||
|
|
||||||
For more details, refer the Bat project GitHub Repository (Link at the end).
|
|
||||||
|
|
||||||
And, that’s all for now. Hope this was useful. More good stuffs to come. Stay tuned!
|
|
||||||
|
|
||||||
Cheers!
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://www.ostechnix.com/bat-a-cat-clone-with-syntax-highlighting-and-git-integration/
|
|
||||||
|
|
||||||
作者:[SK][a]
|
|
||||||
选题:[lujun9972](https://github.com/lujun9972)
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]:https://www.ostechnix.com/author/sk/
|
|
||||||
[1]:https://github.com/sharkdp/bat/releases
|
|
||||||
[2]:https://www.ostechnix.com/linuxbrew-common-package-manager-linux-mac-os-x/
|
|
@ -0,0 +1,173 @@
|
|||||||
|
一种具有语法高亮和 Git 集成的 Cat 克隆命令——Bat
|
||||||
|
======
|
||||||
|
|
||||||
|
![](https://www.ostechnix.com/wp-content/uploads/2018/08/Bat-command-720x340.png)
|
||||||
|
|
||||||
|
在类UNIX系统中,我们使用 **‘cat’** 命令去打印和连接文件。使用cat命令, 我们能将文件目录打印到到标准输出,合成几个文件为一个目标文件,还有追加几个文件到目标文件中。今天,我偶然发现一个具有相似作用的命令叫做 **“Bat”** ,一个 cat 命令的克隆版,具有一些例如语法高亮、 git 集成和自动分页等非常酷的特性。在这个简略指南中,我们将讲述如何在 linux 中安装和使用 Bat 命令。
|
||||||
|
|
||||||
|
### 安装
|
||||||
|
|
||||||
|
Bat 可以在 Arch Linux 的默认软件源中获取。 所以你可以使用 pacman 命令在任何 arch-based 的系统上来安装它。
|
||||||
|
```
|
||||||
|
$ sudo pacman -S bat
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
在 Debian,Ubuntu, Linux Mint 等系统中,从[**发布页面**][1] 下载 **.deb** 文件,然后用下面的命令来安装。
|
||||||
|
```
|
||||||
|
$ sudo apt install gdebi
|
||||||
|
|
||||||
|
$ sudo gdebi bat_0.5.0_amd64.deb
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
对于其他系统,你也许需要从软件源编译并安装 确保你已经安装了 Rust 1.26 或者更高版本。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
然后运行以下命令来安装 Bat
|
||||||
|
```
|
||||||
|
$ cargo install bat
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
或者,你可以从 [**Linuxbrew**][2] 软件包管理中来安装它。
|
||||||
|
```
|
||||||
|
$ brew install bat
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Bat 命令的使用
|
||||||
|
|
||||||
|
Bat 命令的使用与 cat 命令的使用非常相似。
|
||||||
|
|
||||||
|
使用 Bat 命令创建一个新的文件:
|
||||||
|
```
|
||||||
|
$ bat > file.txt
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
使用 Bat 命令来查看文件内容,只需要:
|
||||||
|
```
|
||||||
|
$ bat file.txt
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
你能同时查看多个文件,通过:
|
||||||
|
```
|
||||||
|
$ bat file1.txt file2.txt
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
将多个文件的内容合并至一个单独文件中:
|
||||||
|
```
|
||||||
|
$ bat file1.txt file2.txt file3.txt > document.txt
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
就像我之前提到的那样,除了浏览和编辑文件以外, Bat 命令有一些非常酷的特性。
|
||||||
|
|
||||||
|
Bat 命令支持大多数编程和标记语言的<ruby>语法高亮<rt>syntax highlighting</rt></ruby>。比如,下面这个例子。我将使用 cat 和 bat 命令来展示 **reverse.py** 的内容。
|
||||||
|
|
||||||
|
![](https://www.ostechnix.com/wp-content/uploads/2018/08/bat-and-cat-command-output-comparison.png)
|
||||||
|
|
||||||
|
你注意到区别了吗? cat 命令以纯文本格式显示文件的内容,而 bat 命令显示了语法高亮和整齐的文本对齐格式。更好了不是吗?
|
||||||
|
|
||||||
|
如果你只想显示行号(而不是文本对齐)使用
|
||||||
|
**-n** 标记。
|
||||||
|
```
|
||||||
|
$ bat -n reverse.py
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
**Sample output:**
|
||||||
|
![](https://www.ostechnix.com/wp-content/uploads/2018/08/bat-command-output-3.png)
|
||||||
|
|
||||||
|
另一个 Bat 命令中值得注意的特性是它支持<ruby>自动分页<rt>automatic paging</rt></ruby>。 它的意思是当文件的输出对于屏幕来说太大的时候,bat 命令自动将自己的输出内容传输到 **less** 命令中,所以你可以一页一页的查看输出内容。
|
||||||
|
|
||||||
|
让我给你看一个例子,使用cat命令查看跨多个页面的文件的内容时,提示快速跳至文件的最后一页,你看不到内容的开头和中间部分。
|
||||||
|
|
||||||
|
看一下下面的输出:
|
||||||
|
|
||||||
|
![](https://www.ostechnix.com/wp-content/uploads/2018/08/cat-command-output.png)
|
||||||
|
|
||||||
|
正如你所看到的,cat 命令显示了文章的最后一页。
|
||||||
|
|
||||||
|
所以你也许需要去将使用 cat 命令的输出传输到 **less** 命令中去从开头一页一页的查看内容。
|
||||||
|
```
|
||||||
|
$ cat reverse.py | less
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
现在你可以使用 ENTER 键去一页一页的查看输出。然而当你使用 bat 命令时这些都是不必要的。bat命令将自动传输跨越多个页面的文件的输出。
|
||||||
|
|
||||||
|
```
|
||||||
|
$ bat reverse.py
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
**Sample output:**
|
||||||
|
|
||||||
|
![](https://www.ostechnix.com/wp-content/uploads/2018/08/bat-command-output-1.png)
|
||||||
|
|
||||||
|
现在按下 ENTER 键去往下一页。
|
||||||
|
|
||||||
|
bat 命令也支持 <ruby>Git 集成<rt>**GIT integration**</rt></ruby>,
|
||||||
|
这样您就可以轻松查看/编辑Git存储库中的文件。 它与 Git 连接可以显示关于索引的修改。(看左栏)
|
||||||
|
|
||||||
|
![](https://www.ostechnix.com/wp-content/uploads/2018/08/bat-command-output-2.png)
|
||||||
|
|
||||||
|
**定制 Bat**
|
||||||
|
|
||||||
|
如果你不喜欢默认主题,你也可以修改它。Bat 同样有修改它的选项。
|
||||||
|
|
||||||
|
若要显示可用主题,只需运行:
|
||||||
|
```
|
||||||
|
$ bat --list-themes
|
||||||
|
1337
|
||||||
|
DarkNeon
|
||||||
|
Default
|
||||||
|
GitHub
|
||||||
|
Monokai Extended
|
||||||
|
Monokai Extended Bright
|
||||||
|
Monokai Extended Light
|
||||||
|
Monokai Extended Origin
|
||||||
|
TwoDark
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
要使用其他主题,例如 TwoDark,请运行:
|
||||||
|
```
|
||||||
|
$ bat --theme=TwoDark file.txt
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
如果你想永久改变主题,在你的 shells startup 文件中加入 `export BAT_THEME="TwoDark"`。
|
||||||
|
|
||||||
|
|
||||||
|
Bat还可以选择修改输出的外观。使用 `--style` 选项来修改输出外观。仅显示 Git 的更改和行号但不显示网格和文件头,请使用 `--style=numbers,changes`.
|
||||||
|
|
||||||
|
|
||||||
|
更多详细信息,请参阅 Bat 项目的 GitHub 库(链接在文末)
|
||||||
|
|
||||||
|
最好,这就是目前的全部内容了。希望这篇文章会帮到你。更多精彩文章即将到来,敬请关注!
|
||||||
|
|
||||||
|
干杯!
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://www.ostechnix.com/bat-a-cat-clone-with-syntax-highlighting-and-git-integration/
|
||||||
|
|
||||||
|
作者:[SK][a]
|
||||||
|
选题:[lujun9972](https://github.com/lujun9972)
|
||||||
|
译者:[z52527](https://github.com/z52527)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]:https://www.ostechnix.com/author/sk/
|
||||||
|
[1]:https://github.com/sharkdp/bat/releases
|
||||||
|
[2]:https://www.ostechnix.com/linuxbrew-common-package-manager-linux-mac-os-x/
|
Loading…
Reference in New Issue
Block a user