1. Jump to the directory elegantly and smoothly
1.1 bd command
Quickly go back to a specific parent directory in Bash without typing " cd ../../..
" redundantly.
if you are in this path
/home/radia/work/python/tkinter/one/two, and want to quickly go to the directory python, just type:
bd python
Or just enter the first few letters of the directory, if multiple directories are matched, return to the nearest one:
bd p
Example:
bd command to install:
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
To enable case-sensitive directory name matching, use -s instead of -si in the alias:
If you want to know more, you can visit the developer GitHub: https://github.com/vigneshwaranr/bd
1.2 Some common techniques of cd command
cd is not followed by any parameters, and returns to the user's home directory, which is equivalent to: cd ~.
cd
cd ~
cd - go back, go back to the previous directory:
cd -
1.3 Custom command, jump to common directory
For frequently used directories, you can add a custom command to achieve one-click direct access. This is explained in detail below, let's see the effect first.
if you are in this path
/home/radia/work/python/tkinter/one/two
, and want to quickly go to a directory /home/radia/work/linux/linux-3.16.6
, then just type:
cl
Example:
2. Multi-terminal operation
When operating a Linux terminal, it is a good way to improve efficiency by arbitrarily dividing the screen into multiple windows and reducing mouse operations.
Here, Terminator is recommended. installation method:
sudo apt-get install terminator
After the installation is complete, CTRL + ALT + T to open the software, or search for terminator in all programs.
The following figure shows an example of using shortcut keys to split out three small windows, and rename the lower left window to log.
This is divided into three parts:
-
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.;
-
On the right you can edit code, browse files, etc.
Of course, the size of each split screen can also be flexibly adjusted, both by mouse dragging and shortcut keys CTRL + SHIFT + ↑ ↓ ← →
.
If one tab does not meet your needs, you can use shortcut keys to CTRL + SHIFT + T
open multiple tabs, and tabs can also edit titles for easy distinction.
Commonly used shortcut keys are as follows:
-
CRTL + SHIFT + T
, open a new tab -
CRTL + SHIFT + E
, split screen vertically -
CRTL + SHIFT + O
, split screen horizontally -
ALT + ↑ ↓ ← →
Switch between split screens in the same tab -
CTRL + PAGEUP / PAGEDOWN
Switch between different tabs left and right
The shortcut keys can also be configured according to your own habits. For example, the shortcut keys for switching tabs are CRTL + PAGEUP/PAGEDOWN
. For such a common operation, the PAGEUP/PAGEDOWN
distance between the finger and the button is still too long. I am used to configuring it as ALT + H and ALT + L. , because the ALT button and the arrow keys can switch between different split screens in the same tab page, so simply let ALT switch between different tab pages. I chose H and T because I'm used to the left and right movement of H and T in Vim.
Reserving a terminal window for common functions and editing the terminal title can improve efficiency. Split-screen windows according to your own habits, adjust the windows, and combine with the powerful Vim prepared in the next section, you can easily create your own and efficient IDE.
3. File editing
3.1 Markdown editor recommendation
Markdown is the most popular markup language at present. It can give conventional text files: formatting, inserting pictures, inserting charts, inserting codes and other practical functions. Here we recommend the Typora editor, which has a literary style and powerful functions.
Typora 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 typora
Markdown and Typora are easy to use and can be referred to: Why learn Markdown? What's the use?
You can use the Markdown format as a common format for taking notes and organizing your own knowledge tree. Goodbye Typora! Love this open source Markdown editor!
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, which is not friendly to beginners.
This article will not compare any Vim plug-ins first, so as not to be dazzled and fall into the phobia of choice, but directly attach the Vim configuration compressed package that I have accumulated and used for many years, and can be used directly after decompression. It is recommended that readers who have not yet formed their own usage habits use the version provided by me, use them first, and slowly understand them in actual use.
3.2.1 Vim installation and quick configuration
How to install Vim:
sudo apt-get install vim
Vim configuration:
Link: https://pan.baidu.com/s/1PLyCV88Froe4d7sRzopVkA
Extraction code: r4vo
After downloading vim.tar.gz and extracting it, extract vim-config.tar to the user root directory:
tar -xvf vim-config.tar -C ~/
After the installation is complete, it is as simple as that, all the plug-ins have been included in the compressed package, and there is no need to download other things.
Reopen a terminal window and use Vim to open the code to see the effect:
As you can see from the above interface, the left interface is the file list (not displayed by default, F3 on/off), and the function list on the right (default displayed, F9 on/off), you can use CTRL + W to jump between various areas , on the three interfaces, you can move the cursor through h, j, k, l just like editing a file.
Press Enter 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 horizontal or vertical split window. The function of this Vim split window is very useful for viewing the code.
3.2.2 Searching a file
Using F3 to call out the file list, it is convenient to open files in the same directory, but if you need to open files in other locations, it is still inconvenient, 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 Jump to function definition
A must for viewing code is to quickly get to where a function or variable is defined. To achieve this function, you first need to create an index.
Execute in the project root directory:
ctags -R *
//或者指定需要建立索引的语言
ctags --languages=c,c++,java -R
The execution time depends on the number of source code in the project. If there are many files, it will take longer to create an index. After the execution is completed, a tags file will be generated in the project and directory. In this way, in this directory again, open Vim and use the CTRL + ] shortcut key to jump to the definition position of the function or variable, and use CTRL + T to return to the original position.
3.2.4 Others
The basic operations given above can basically meet all daily needs. If you like tossing, you can explore more functions after you are familiar with it, and you can also speed up the search. These readmes in vim.tar.gz also have simple instructions, which can be regarded as an introduction.
I hope that through the above introduction, you can get used to and like Vim.
This configuration enables mouse support by default. 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 beginners to get started. It is recommended to turn off this function after you are familiar with it later, because the mouse will be lost after turning on the mouse function. Right-click menu, mouse is not as efficient as keyboard operation.
Turn off the mouse function in Vim:
vim ~/.vimrc
//在303行前加引号,将set mouse=a注释掉即可
" set mouse=a
4. Custom shell commands
The essence of Linux is scripting, which can add logical relationships to common operations, complete a series of operations, and free our hands. This is one of the main reasons why programmers like to use Linux.
4.1 Implementation of the cl command
Let's take a look at the cl command mentioned above, which can quickly jump to the commonly used Linux source directory. How is this achieved?
It's actually unbelievably simple: just add a command alias to the .bashrc file in the current user's root directory.
Try it out by following the command
/home/radia/work/linux/linux-3.16.6/
Replace with your own common path:
echo 'alias cl="cd /home/radia/work/linux/linux-3.16.6/"' >> ~/.bashrc
source ~/.bashrc
As you can see from the above figure, the cl command is not available before the addition, and the function can be implemented after the addition, and the jump to the specified directory is successful.
Note: The above operations only need to be performed once, and all terminals opened after adding include the cl command.
4.2 Let's try a more complex command
~/.bashrc
Commands with more complex logic cannot be written directly in the file like the above cl .
Suppose the following scenario:
When working in scene A, you need to open the software A1, A2, and A3 required for scene A; in scene B, you need the support of software B1, B2, and B3. Then you can add a 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
The implementation is as follows:
In any directory, for example /home/radia/cmd
, create the script file env_switch.sh.
Add the following content, in which the opening and closing of the software is replaced by echo, and the software's opening command can be replaced 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
}
Increase execute permission:
chmod +x env_switch.sh
Put it in the ~/.bashrc file, this is to automatically load our custom command when each terminal is opened:
echo 'source /home/radia/cmd/env_switch.sh' >> ~/.bashrc
source ~/.bashrc
You can see that there is only one function in the above script envswitch.sh, and it is not called. This is the key skill 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 like other shell commands. If you have other requirements, you can put them in the envswitch.sh file You can add a function. Adding a function will add a new command.
This method is suitable for a series of operations that you do every day, and it is a good choice to form a common command after induction.
In addition, in some test scenarios during the development process, if multiple steps are required and will be used frequently for a period of time, it is recommended to write a script instead of adding a command
Source: blog.csdn.net/daocaokafei/article/details/117384034
Recommended reading
Actual combat | How to use Python to count and visualize Jira data
In 5 minutes, teach you to deploy a Python application with Docker!
The most complete summary | Talk about several ways to parameterize the Python command line!