4 Linux tips you deserve

Release: 2023-08-02 15:57:59
forward
1191 people have browsed it

4 Linux tips you deserve


Linux has gradually become the most popular operating system among developers. This article will introduce four extremely practical tips, which I believe will improve your work efficiency and fluency under Linux, and also make your work more comfortable and elegant.

1. Jumping directories is elegant and smooth

1.1 bd command

Quickly return to a specific parent directory in Bash , instead of typing <span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;letter-spacing: 1px;visibility: visible;">cd ../../..</span> redundantly.

If you are in this path<span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;letter-spacing: 1px;visibility: visible;">/home/radia/work/python/tkinter/one/two</span> and want to quickly go to the directory python, just type:

bd python
Copy after login

or just enter the first few letters of the directory. If multiple directories are matched, return to the most recent one:

bd p
Copy after login

Example:

4 Linux tips you deserve

bd 命令安装:

sudo wget --no-check-certificate -O /usr/bin/bd https://raw.githubusercontent.com/vigneshwaranr/bd/master/bd
sudo chmod +rx /usr/bin/bd
echo &#39;alias bd=". bd -si"&#39; >> ~/.bashrc
source ~/.bashrc
Copy after login

要启用区分大小写的目录名称匹配,请在别名中使用 -s 代替 -si:

如希望了解更多可以访问开发者 GitHub:<span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;letter-spacing: 1px;">https://github.com/vigneshwaranr/bd</span>

1.2 cd 命令的一些常用技巧

cd 后面不跟任何参数,回用户主目录,等同:cd ~。

cd
cd ~
Copy after login

cd - 回退,返回之前目录:

cd -
Copy after login

1.3 自定义命令,跳转到常用目录

对于经常使用的目录,可以增加一条自定义命令,实现一键直达。这点在下文详解,先看个效果。

如果您在此路径中<span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;letter-spacing: 1px;">/home/radia/work/python/tkinter/one/two</span>,并且想快速转到目录 <span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;letter-spacing: 1px;">/home/radia/work/linux/linux-3.16.6</span>,然后只需键入:

cl
Copy after login

示例:

4 Linux tips you deserve

2. 多终端操作

在 Linux 终端操作时,任意分割屏幕为多个窗口,减少鼠标操作,都是提高效率的好办法。

在此,推荐 Terminator。安装方法:

sudo apt-get install terminator
Copy after login

安装完成后 CTRL + ALT + T 打开软件,或者在所有程序中搜索 terminator。

The following figure shows an example of using shortcut keys to split the screen into three small windows, and rename the lower left window to log.

Three parts divided like this:

  • The lower left corner can be specially used to display the real-time serial port log;

  • The upper left side can be used to compile code, view compilation errors, etc.;

  • The right side can be used to edit Code, browse files, etc.

4 Linux tips you deserve

Of course, The size of each split screen can also be flexibly adjusted by dragging the mouse and pressing the shortcut CTRL SHIFT ↑ ↓ ← → can be realised.

If one tab page cannot meet the needs, you can use the shortcut key CTRL SHIFT T to open multiple tab pages. The title of the tab page can also be edited for easy differentiation. In addition, search the top technology background of the public account and reply "API interface" to get a surprise gift package.

Commonly used shortcut keys are as follows:

  • CRTL SHIFT T, open a new tab

  • ##CRTL SHIFT E, split the screen vertically

  • CRTL SHIFT O, split screen horizontally

  • ##ALT ↑ ↓ ← → In the same tab Switch between various split screens in

  • ##CTRL PAGEUP / PAGEDOWN Switch between different tabs left and right

Shortcut keys can also be configured according to your own habits. For example, the shortcut key for switching tabs is CRTL PAGEUP/PAGEDOWN. For such a common operation, move your finger to the PAGEUP/PAGEDOWN button. The distance is still too long, so I am used to configuring it as ALT H and ALT L. Because the ALT key and the direction keys can switch different split screens in the same tab, I simply let ALT switch different tabs. I chose H and T because I am used to the left and right movement of H and T in Vim.

Reserve a separate terminal window for commonly used functions and edit the terminal title to improve efficiency. Split the screen window according to your own habits, adjust the window, and combine it with the powerful Vim prepared in the next section, you can easily create your own exclusive and efficient IDE.

3. File editing

3.1 Markdown editor recommendation

Markdown is currently the most popular markup language, which can Conventional text files are endowed with practical functions such as formatting, inserting pictures, inserting charts, inserting codes, etc. The Typora editor is recommended here, with its literary style and powerful functions.

Tpyora installation:

wget -qO - https://typora.io/linux/public-key.asc | sudo apt-key add -
# add Typora&#39;s repository
sudo add-apt-repository &#39;deb https://typora.io/linux ./&#39;
sudo apt-get update
# install typora
sudo apt-get install typora
Copy after login

Markdown 和 Tpyora 使用简单,一看就会,不用再多做介绍。

可以将 Markdown 格式作为记录笔记,整理自己知识树的常用格式。

Typora 官网:https://www.typora.io

3.2 Code editing and viewing

The most widely recognized code editor is of course Vim, but many functions are a bit cumbersome to configure. It takes a lot of time to compare and study various plug-ins. This process is not friendly to beginners.

This article will not compare any Vim plug-ins first, so as to avoid being dazzled and falling into phobia of choice. Instead, I will directly attach the Vim configuration compressed package that I have accumulated and been using for many years and unzip it directly. can use. It is recommended that readers who have not formed their own usage habits use the version I provide. Use it first and slowly understand it in actual use. I believe it will not take long to get used to Vim, and then adjust or reconfigure it according to your own needs.

3.2.1 Vim 安装与快速配置

Vim 安装方法:

sudo apt-get install vim
Copy after login

Vim 配置:

下载 vim.tar.gz 解压后,将 vim-config.tar 解压至用户根目录:

tar -xvf vim-config.tar  -C ~/
Copy after login

安装完毕,就这么简单,所有的插件已经包含到压缩包里了,不需要再下载其它东西。

重新开启一个终端窗口就可以使用 Vim 打开代码看看效果了:

4 Linux tips you deserve

上述界面可以看到,左侧界面是文件列表(默认不显示,F3 开 / 关),右侧函数列表(默认显示,F9 开 / 关),可以使用 CTRL + W 在各个区域之间跳转,在三个界面上都可以像编辑文件一样,通过 h、j、k、l 移动光标定位。

Press the Enter key on the function list item to jump to the specified function.

Press the Enter key on the file list item to open the corresponding file; Press i or s to open the file in a horizontally split or vertically split window. The split window of Vim Function, it is very practical to view the code.

3.2.2 Search for a file

Using the file list called out by F3, it is more convenient to open the file in the same directory, but if It is still inconvenient to open files in other locations. You can press F5 to call out the file search window, enter the file name in the project, and quickly open the file.

3.2.3 跳转到函数定义

查看代码必备的功能就是快速到达函数或变量的定义位置。要实现这个功能,首先需要创建索引。

牛逼啊!接私活必备的 N 个开源项目!赶快收藏
Copy after login

在项目根目录下执行:

ctags -R *
//或者指定需要建立索引的语言
ctags --languages=c,c++,java -R
Copy after login

执行时间根据项目中源码数量而定,文件多则创建索引的时间会久一些。执行完成之后,在项目跟目录下会生成一个 tags 文件。这样再次在该目录下,打开 Vim 就可以使用 CTRL + ] 快捷键,跳转到函数或变量的定义位置了,使用 CTRL + T 返回原来的位置。

3.2.4 其它

上述给出的这些基本操作,已经基本能够满足日常所有的需求。如果喜欢折腾,熟悉之后可以探索更多功能,还可以加快查找速度,这些在 vim.tar.gz 中的 readme 也有简单的说明,算是抛砖引玉。

希望通过上述介绍,你能习惯并喜欢上 Vim。

这套配置默认开启了鼠标的支持,可以通过鼠标定位光标的焦点,使用滚轮翻页代码,这是为了便于初学者上手,后续熟悉了之后建议关闭此功能,因为开启鼠标功能后会损失鼠标的右键菜单,鼠标也不如键盘操作的效率高。

关闭 Vim 中鼠标功能:

vim ~/.vimrc
//在303行前加引号,将set mouse=a注释掉即可
" set mouse=a
Copy after login

4. 自定义 shell 命令

Linux 的精髓就是脚本,可以对常用操作加入逻辑关系,完成一系列操作,解放我们的双手,这也是程序员喜欢使用 Linux 的一个主要原因。

4.1 Implementation of cl command

Let’s first take a look at the cl command mentioned above, which can quickly jump to commonly used Linux source code directories. This is How is it achieved?

It’s actually incredibly simple: just add the command alias to the .bashrc file in the current user’s root directory.

按照如下命令尝试一下吧,将其中<span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;letter-spacing: 1px;">/home/radia/work/linux/linux-3.16.6/</span> 替换成自己的常用路径:

echo &#39;alias cl="cd /home/radia/work/linux/linux-3.16.6/"&#39; >> ~/.bashrc
source ~/.bashrc
Copy after login
4 Linux tips you deserve

上图可以看到,在添加之前 cl 命令不可用,添加之后就能够实现功能,成功跳转到指定目录了。

注:上述操作仅需操作一次,在添加后开启的所有终端就都包含了 cl 命令。

4.2 Let’s try a command with more complex functions

Commands with more complex logic cannot be written directly in the ~/.bashrc file like the above cl. .

Assume the following scenario:

#When working in scenario A, you need to open the software A1 and A2 required for scenario A. , A3; B scenario, the support of software B1, B2 and B3 is required. Then you can add a new command env_switch to complete such a working environment switch

env_switch A start #开启A工作环境下的软件A1,A2,A3
env_switch A stop #关闭A工作环境下的软件A1,A2,A3
env_switch B start #开启B工作环境下的软件B1,B2,B3
env_switch B stop #开启B工作环境下的软件B1,B2,B3
Copy after login

实现如下:

在任意目录下,如 <span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;letter-spacing: 1px;">/home/radia/cmd</span>,创建脚本文件 <span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;letter-spacing: 1px;">env_switch.sh</span>

加入如下内容,其中开启、关闭软件使用 echo 替代了,实际使用时替换为软件的开启命令即可:

#!/bin/bash
function env_switch()
{
    if [ $1 = "A" ]; then
        echo "A1,A2,A3 "
        if [ $2 = "start" ];then
            echo "will be opened"
        elif [ $2 = "stop" ]; then
            echo "will be closed"
        fi
    elif [ $1 = "B" ];then
        echo "B1,B2,B3"
        if [ $2 = "start" ];then
            echo "will be opened"
        elif [ $2 = "stop" ]; then
            echo "will be closed"
        fi
    fi
}
Copy after login

增加执行权限:

chmod +x env_switch.sh
Copy after login

放到 <span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;letter-spacing: 1px;">~/.bashrc</span> 文件中,这是为了在开启每个终端时,都自动载入我们自定义的命令:

echo &#39;source /home/radia/cmd/env_switch.sh&#39; >> ~/.bashrc
source ~/.bashrc
Copy after login
4 Linux tips you deserve

可以看到上述脚本 <span style="margin: 0px;padding: 0px;outline: 0px;max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;letter-spacing: 1px;">env_switch.sh</span> 中只有一个函数,而且并未被调用。这就是本小节中的关键技巧所在,利用 source 命令将脚本中的函数导入当前 shell,这样脚本中的函数就可以和其他的 shell 命令一样使用了,如有其他需求,可以在 env_switch.sh 文件中增加函数即可,增加一个函数,就会新增一条命令。

这种方法适合那些你每天都在做的一系列操作,归纳后形成一个常用命令,是个不错的选择。

In addition, if some test scenarios during the development process require multiple steps of operation and will be used frequently over a period of time, it is recommended to write them as scripts without adding them as commands.

4.3 Let’s talk about the bd command again

Careful students may have discovered that in fact, the bd command we used before is only 50 For multi-line small scripts, we can also spend time writing some of our own small scripts during daily use. In this way, work efficiency will continue to improve after continuous accumulation.

The above is the detailed content of 4 Linux tips you deserve. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Linux中文社区
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template