Linux has gradually become the most popular operating system for developers. In this article, I’ll show you four extremely useful tips that will improve your productivity and fluency in Linux, as well as make your work more comfortable and elegant.

The software installation commands in Ubuntu are used as an example, and are similar for other distributions.

1. The jump directory is elegant and smooth

1.1 bd command

Quickly go back to a specific parent directory in Bash, rather than typing “CD.. /.. /..” .

If you are in this path

/ home/radia/work/python/tkinter/one/two, and want to quickly turn to python directory, simply type:

bd python

Copy the code

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

bd p

Copy the code

Example:

Bd command installation:

sudo wget --no-check-certificate -O /usr/bin/bd https://raw.githubusercontent.com/vigneshwaranr/bd/master/bd
sudo chmod +rx /usr/bin/bd
echo 'alias bd=". bd -si"' >> ~/.bashrc
source ~/.bashrc

Copy the code

To enable case-sensitive directory name matching, use -s instead of -si in the alias:

To learn more, visit GitHub:

Github.com/vigneshwara…

1.2 Common techniques of the CD command

CD does not follow any parameters, the user home directory, the same as: CD ~.

cd
cd ~

Copy the code

CD – Rollback, return to previous directory:

cd -

Copy the code

1.3 Customizing Commands to Switch to common Directories

For frequently used directories, you can add a custom command to achieve one-click direct access. This will be explained in detail below, and the effect will be shown first.

If you are in this path

/ home/radia/work/python/tkinter/one/two, and want to quickly go to the directory/home/radia/work/Linux/Linux – 3.16.6, then simply type:

cl

Copy the code

Example:

2. Multi-terminal operations

In Linux terminal operation, arbitrarily split the screen into multiple Windows, reduce mouse operation, is a good way to improve efficiency.

Terminator is recommended. Installation method:

sudo apt-get install terminator

Copy the code

After the installation is complete, CTRL + ALT + T opens the software, or search for Terminator in all programs.

The figure below shows an example of using shortcut keys to split three small Windows and rename the bottom left window to log.

So divided into three parts:

  • The lower left corner can be used to display real-time serial port logs.

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

  • On the right side you can edit code, browse files, etc.

Of course, the size of each split screen can be flexibly adjusted, such as dragging the mouse and shortcut keys CTRL + SHIFT + ↑ ↓ ← →.

If a TAB page cannot meet requirements, you can use the shortcut keys CTRL + SHIFT + T to open multiple tabs. You can also edit the title of the tabs to distinguish them.

Common shortcut keys are as follows:

  • CRTL + SHIFT + T, open a new TAB

  • CRTL + SHIFT + E, vertical split screen

  • CRTL + SHIFT + O, split screen horizontally

  • ‘ALT + ↑ ↓ ← →’ Switch between split screens in the same TAB

  • CTRL + PAGEUP/PAGEDOWN Switch between different tabs

The shortcut keys can also be configured according to their own habits. For example, the shortcut keys for switching TAB pages are CRTL + PAGEUP/PAGEDOWN. For such a common operation, the distance between finger moving to PAGEUP/PAGEDOWN is still too long. I like to use ALT + H and ALT + L, because ALT and arrow keys can switch between different tabs on the same TAB, so I just let ALT switch between different tabs. I chose H and T because I was used to moving H and T left and right in Vim.

Reserve a terminal window for common functions and edit the terminal title to improve efficiency. Split the window as you like, tweak the window, and with the powerful Vim you’ll see in the next section, it’s easy to create your own and efficient IDE.

3. File editing

3.1 Markdown editor recommendation

Markdown is the most popular markup language at present, which can give regular text files with practical functions such as formatting and typesetting, inserting pictures, inserting charts, inserting codes, etc. Typora editor is recommended here, with artistic style and powerful functions.

Tpyora installation:

wget -qO - https://typora.io/linux/public-key.asc | sudo apt-key add - # add Typora's repository sudo add-apt-repository  'deb https://typora.io/linux ./' sudo apt-get update # install typora sudo apt-get install typoraCopy the code

Markdown and Tpyora are easy to use, easy to read and easy to use.

You can use the Markdown format as a common format for taking notes and organizing your knowledge tree.

Typora website: www.typora.io

3.2 Code editing and viewing

Arguably the most useful code editor is Vim, but many features are a bit cumbersome to configure. It takes a lot of time to compare and study various plug-ins, a process that is not friendly to beginners.

In this article, I will not compare any Vim plug-in to avoid being dazzled and falling into the fear of choice. Instead, I will directly attach the Vim configuration compression package that I have accumulated for many years and have been using. It can be used directly after decompression. Readers who have not formed their own habits are advised to use the version PROVIDED by me. They should first use it and gradually understand it in practice. After a while, they can get used to Vim, and then adjust or reconfigure it according to their own needs.

3.2.1 Vim Installation and Quick configuration

Vim installation method:

sudo apt-get install vim

Copy the code

Vim configuration:

Pan.baidu.com/s/1opcy7owS…

Extraction code: B82K

Download vim.tar.gz and decompress vim-config.tar to the user root directory:

tar -xvf vim-config.tar  -C ~/

Copy the code

Once installed, it’s as simple as that. All the plug-ins are included in the zip package. There’s no need to download anything else.

Re-open a terminal window and use Vim to open the code to see what it looks like:

As can be seen from the above interface, the left interface is the file list (not displayed by default, F3 on/off), and the right interface is the function list (displayed by default, F9 on/off). You can use CTRL + W to jump between various areas, and move the cursor in the three interfaces like editing files by h, J, K and L.

Press enter on the function list item to jump to the specified function.

Press enter on the file list item to open the corresponding file; Press I or S to open the file in horizontal or vertical split window, the Vim split window function, view the code is very practical.

3.2.2 Searching for a File

Using the list of files called out by F3, it is still convenient to open files under the same directory. However, if you need to open files in other locations, it is still not convenient. You can press F5 to call out file search window and enter the file name in the project to quickly open files.

3.2.3 Go to Function Definition

A necessary function of viewing code is to quickly reach the definition of a function or variable. To do this, you first need to create an index.

Execute in project root:

Ctags -r * // Or ctags --languages= C, C ++, Java -rCopy the code

The execution time depends on the amount of source code in the project, with more files taking longer to create the index. After execution, a tags file is generated under the project and directory. Once again in this directory, open Vim and use CTRL +] to jump to the function or variable definition. Use CTRL + T to return to the original location.

3.2.4 other

The basic operations given above can basically meet all the daily needs. If you’re a bit more adventurous, you can explore more features once you’re familiar with them and speed up your search. The readme in vim.tar.gz also has a simple explanation for this.

I hope that through the above introduction, you can get used to and like Vim.

By default, the mouse is enabled for this configuration. You can use the mouse to locate the focus of the cursor and use the scroll wheel to turn the page code. This is for the convenience of beginners.

Disable mouse function in Vim:

Vim ~/. Vimrc // Put quotes before line 303 and comment out set mouse=a: "Set mouse=aCopy the code

4. Customize shell commands

The essence of Linux is scripting, the ability to add logic to common operations, complete a series of operations, and free our hands. This is one of the main reasons programmers like to use Linux.

4.1 Implementation of CL command

Let’s take a look at the cl command, which can quickly jump to a common Linux source directory.

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

Try it out with the following command

/ home/radia/work/Linux/Linux – 3.16.6 / replace common path:

Echo 'alias cl = "CD/home/radia/work/Linux/Linux - 3.16.6 /"' > > ~ /. Bashrc source ~ /. BashrcCopy the code

As you can see in the figure above, the cl command is not available before the addition. After the addition, the function can be implemented and the specified directory can be successfully jumped.

Note: The above operation only needs to be performed once, and all terminals opened after addition contain cl commands.

4.2 To try a more complex command

More complex commands cannot be written directly to the ~/.bashrc file as cl did above.

Assume the following scenario:

In scenario A, enable software A1, A2, and A3 required by scenario A. In scenario B, software B1, B2, and B3 are required. You can add a command env_switch to switch the working environment

A1, A2, A3 env_switch B start # Enable the software B1, B2, B3 env_switch B stop # Enable the software B1, B2, B3 in the working environment of BCopy the code

The implementation is as follows:

In any directory, for example, /home/radia/cmd, create the env_switch.sh script file.

Add the following content, in which the start and close software are replaced by echo, and can be replaced by the start command of the software in actual use:

#! /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 the code

Add execution permission:

chmod +x env_switch.sh

Copy the code

In the ~/.bashrc file, this is to automatically load our custom commands when starting each terminal:

echo 'source /home/radia/cmd/env_switch.sh' >> ~/.bashrc
source ~/.bashrc

Copy the code

You can see that the script envswitch.sh has only one function and is not called. This is the key technique in this section. Use the source command to import the functions in the script into the current shell, so that the functions in the script can be used just like any other shell commands. If you need anything else, you can add a function to the envswitch.sh file. A new command will be added.

This approach is a good one for a series of actions that you do every day, and can be distilled into a common command.

In addition, for some test scenarios that require multiple steps and are frequently used in a period of time, you are advised to write scripts instead of adding commands.

4.3 Talk about the BD command again

Careful students may have found that, in fact, the BD command we used before is only a small script of more than 50 lines, we can also spend time to write some of our own small scripts in ordinary use, so that the accumulation of work efficiency will continue to improve, the so-called grinding knife is not wrong to cut wood workers.

The end of the 5.

If these small skills can bring convenience to your work and study, I will be most willing to see. Thanks for reading!