我正在使用 Ubuntu 12.04。我没有任何 root 或 sudo 权限,因为这是一台公司机器。
在 Ubuntu 12.04 的正常安装中,是否有任何终端程序可以用来将缺乏任何缩进的丑陋畸形源代码转换为漂亮的代码?
同样,我无法安装任何软件包,所以我需要一个已经随 Ubuntu 一起提供的软件包(如果存在这样的东西)。
例如:
int main()
{
test(1);
another_function(1);
}
Run Code Online (Sandbox Code Playgroud)
然后将其转换为:
int main()
{
test(1);
another_function(1);
}
Run Code Online (Sandbox Code Playgroud)
Lek*_*eyn 12
如果您安装了 vim 编辑器,请打开文件vim file.c并键入=G从头到尾缩进文件。然后用:wq.
在默认安装中,vi(not vim) 已安装,因此它不会包含所需的ident包(如 karel 所述)。
小智 10
clang-format是你的朋友!它易于使用且有用。
这里有一些关于它的信息。
用法
$ clang-format file > formattedfile
Run Code Online (Sandbox Code Playgroud)
或者:
$ clang-format -i file
Run Code Online (Sandbox Code Playgroud)
分步指南
1. 可怕的格式化代码
#include <iostream>
using namespace std;
int main() {
cout << "Oh";
cout << "clang format rulez!";
}
Run Code Online (Sandbox Code Playgroud)
main.cc
2. 神奇的命令
$ clang-format -i main.cc
Run Code Online (Sandbox Code Playgroud)
3. 格式良好的代码
#include <iostream>
using namespace std;
int main() {
cout << "Oh";
cout << "clang format rulez!";
}
Run Code Online (Sandbox Code Playgroud)
main.cc
4. Happiness
安装
如果你喜欢它,你可以安装它,
$ sudo apt-get install clang-format
Run Code Online (Sandbox Code Playgroud)
命令。
打开终端并运行:
sudo apt-get install indent
indent -linux -l120 -i4 -nut unformatted-source-code.cpp
Run Code Online (Sandbox Code Playgroud)
...其中 unformatted-source-code.cpp 是具有未格式化的 C++ 源代码的文件,例如示例中的代码。
或者,如果您无法安装它,您可以使用以下命令下载软件包并apt-get download indent解压:dpkg-deb -x indent*.deb fs/,缩进二进制文件位于fs/usr/bin/其中 fs 是您的主目录中的任何目录。如果将 unformatted-source-code.cpp 文件复制到同一位置,fs/usr/bin/则从终端缩进代码的命令为:
cd path/to/fs/usr/bin/ # change directories to the location of "indent" executable
./indent -linux -l120 -i4 -nut unformatted-source-code.cpp
Run Code Online (Sandbox Code Playgroud)
这些命令可以作为普通用户运行。不必是 root。