Ubuntu Shell 实践指南
- 内容介绍
- 文章标签
- 相关推荐
一、Shell 环境配置
安装 Zsh + Oh My Zsh
# 安装 Zsh
sudo apt install zsh
# 安装 Oh My Zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
必装插件
# zsh-autosuggestions:历史命令实时提示
git clone https://github.com/zsh-users/zsh-autosuggestions \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# zsh-syntax-highlighting:命令实时语法高亮
git clone https://github.com/zsh-users/zsh-syntax-highlighting \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
在 ~/.zshrc 启用:
plugins=(git zsh-autosuggestions zsh-syntax-highlighting z fzf)
安装 fzf(模糊搜索)
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
在 ~/.zshrc 加入(需在 plugins 之前):
export FZF_BASE="$HOME/.fzf"
启动后按 Ctrl+R 即可模糊搜索历史命令。
二、ls 最佳实践
推荐用 eza 替代 ls
sudo apt install eza
常用别名配置(写入 ~/.zshrc)
alias ll='eza -lha --group-directories-first --time-style="+%Y-%m-%d %H:%M:%S"'
alias lt='eza -lh --sort=modified --reverse' # 按时间排序
alias lS='eza -lh --sort=size' # 按大小排序
cd 后自动列出目录内容
cd() {
builtin cd "$@" && eza -lh --group-directories-first --time-style="+%Y-%m-%d %H:%M:%S"
}
区分文件与文件夹
| 方式 | 命令 |
|---|---|
| 目录置顶 | --group-directories-first |
| 符号标记 | eza -lhF(目录加 /,可执行加 *) |
| 只看目录 | eza -lh | grep "^d" |
| 只看文件 | eza -lh | grep "^-" |
常用 ls 参数对照(ls vs eza)
| ls | eza | 说明 |
|---|---|---|
--color=auto |
默认开启 | 不需要写 |
--group-directories-first |
--group-directories-first |
相同 |
--full-time |
--time-style="+%Y-%m-%d %H:%M:%S" |
自定义时间格式 |
-t |
--sort=modified |
按时间排序 |
-S |
--sort=size |
按大小排序 |
-a |
-a |
显示隐藏文件 |
生效:
source ~/.zshrc
三、系统监控工具
推荐组合
| 场景 | 工具 | 安装 |
|---|---|---|
| 日常总览 | btop / htop | sudo apt install htop |
| 磁盘占用分析 | ncdu | sudo apt install ncdu |
| 网络流量监控 | nethogs | sudo apt install nethogs |
| 轻量替代 | bottom (btm) | 见下方 |
安装 bottom(轻量监控,Rust 编写)
wget https://github.com/ClementTsang/bottom/releases/download/0.9.6/bottom_0.9.6_amd64.deb
sudo dpkg -i bottom_0.9.6_amd64.deb
btm
原生命令快速诊断
free -h # 内存使用
df -h # 磁盘空间
ps aux --sort=-%cpu | head -10 # CPU 占用前 10
ps aux --sort=-%mem | head -10 # 内存占用前 10
ss -tunlp # 查看监听端口
推荐日常工作流
btop # 开机总览
ncdu / # 磁盘快满时找元凶
sudo nethogs # 排查网络流量异常进程
四、Vim 最佳实践
安装插件管理器 vim-plug
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
推荐 ~/.vimrc 配置
" ===== 基础设置 =====
set number
set relativenumber
set cursorline
set clipboard=unnamed
" ===== 缩进 =====
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set smartindent
" ===== 搜索 =====
set hlsearch
set incsearch
set ignorecase
set smartcase
" ===== 插件 =====
call plug#begin()
Plug 'preservim/nerdtree'
Plug 'vim-airline/vim-airline'
Plug 'tpope/vim-fugitive'
Plug 'junegunn/fzf.vim'
Plug 'tpope/vim-surround'
Plug 'morhetz/gruvbox'
call plug#end()
colorscheme gruvbox
" ===== Leader 键 =====
let mapleader = " "
nnoremap <leader>w :w<CR>
nnoremap <leader>q :q<CR>
nnoremap <leader>n :nohlsearch<CR>
nnoremap <leader>e :NERDTreeToggle<CR>
nnoremap <leader>f :Files<CR>
nnoremap <leader>v :vsp<CR>
安装插件:
vim +PlugInstall +qall
常用快捷键速查
移动
| 按键 | 功能 |
|---|---|
h j k l |
左下上右 |
w / b |
下/上一个单词 |
gg / G |
文件头/尾 |
Ctrl+d / Ctrl+u |
半页下/上 |
编辑
| 按键 | 功能 |
|---|---|
i / a |
当前位置/后面插入 |
o / O |
下方/上方新行 |
dd |
删除行 |
yy / p |
复制/粘贴行 |
u / Ctrl+r |
撤销/重做 |
ciw |
修改当前单词 |
搜索替换
/keyword " 搜索
:%s/old/new/g " 全文替换
:%s/old/new/gc " 逐个确认替换
分屏
:sp " 水平分屏
:vsp " 垂直分屏
Ctrl+w + hjkl " 分屏间移动
进阶:Neovim + LazyVim
sudo apt install neovim
git clone https://github.com/LazyVim/starter ~/.config/nvim
nvim # 自动安装所有插件
五、快速参考:判断当前 Shell
echo $SHELL # /bin/zsh 或 /bin/bash
| Shell | 配置文件 |
|---|---|
| Zsh | ~/.zshrc |
| Bash | ~/.bashrc |
配置修改后记得执行 source ~/.zshrc 使其生效。
--【壹】--:
感谢佬友分享
--【贰】--:
第一步:alias sl=‘ls’
因为总是手快打错,哈哈
--【叁】--:
感谢大佬分享
--【肆】--:
感谢大佬
一、Shell 环境配置
安装 Zsh + Oh My Zsh
# 安装 Zsh
sudo apt install zsh
# 安装 Oh My Zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
必装插件
# zsh-autosuggestions:历史命令实时提示
git clone https://github.com/zsh-users/zsh-autosuggestions \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# zsh-syntax-highlighting:命令实时语法高亮
git clone https://github.com/zsh-users/zsh-syntax-highlighting \
${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
在 ~/.zshrc 启用:
plugins=(git zsh-autosuggestions zsh-syntax-highlighting z fzf)
安装 fzf(模糊搜索)
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
在 ~/.zshrc 加入(需在 plugins 之前):
export FZF_BASE="$HOME/.fzf"
启动后按 Ctrl+R 即可模糊搜索历史命令。
二、ls 最佳实践
推荐用 eza 替代 ls
sudo apt install eza
常用别名配置(写入 ~/.zshrc)
alias ll='eza -lha --group-directories-first --time-style="+%Y-%m-%d %H:%M:%S"'
alias lt='eza -lh --sort=modified --reverse' # 按时间排序
alias lS='eza -lh --sort=size' # 按大小排序
cd 后自动列出目录内容
cd() {
builtin cd "$@" && eza -lh --group-directories-first --time-style="+%Y-%m-%d %H:%M:%S"
}
区分文件与文件夹
| 方式 | 命令 |
|---|---|
| 目录置顶 | --group-directories-first |
| 符号标记 | eza -lhF(目录加 /,可执行加 *) |
| 只看目录 | eza -lh | grep "^d" |
| 只看文件 | eza -lh | grep "^-" |
常用 ls 参数对照(ls vs eza)
| ls | eza | 说明 |
|---|---|---|
--color=auto |
默认开启 | 不需要写 |
--group-directories-first |
--group-directories-first |
相同 |
--full-time |
--time-style="+%Y-%m-%d %H:%M:%S" |
自定义时间格式 |
-t |
--sort=modified |
按时间排序 |
-S |
--sort=size |
按大小排序 |
-a |
-a |
显示隐藏文件 |
生效:
source ~/.zshrc
三、系统监控工具
推荐组合
| 场景 | 工具 | 安装 |
|---|---|---|
| 日常总览 | btop / htop | sudo apt install htop |
| 磁盘占用分析 | ncdu | sudo apt install ncdu |
| 网络流量监控 | nethogs | sudo apt install nethogs |
| 轻量替代 | bottom (btm) | 见下方 |
安装 bottom(轻量监控,Rust 编写)
wget https://github.com/ClementTsang/bottom/releases/download/0.9.6/bottom_0.9.6_amd64.deb
sudo dpkg -i bottom_0.9.6_amd64.deb
btm
原生命令快速诊断
free -h # 内存使用
df -h # 磁盘空间
ps aux --sort=-%cpu | head -10 # CPU 占用前 10
ps aux --sort=-%mem | head -10 # 内存占用前 10
ss -tunlp # 查看监听端口
推荐日常工作流
btop # 开机总览
ncdu / # 磁盘快满时找元凶
sudo nethogs # 排查网络流量异常进程
四、Vim 最佳实践
安装插件管理器 vim-plug
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
推荐 ~/.vimrc 配置
" ===== 基础设置 =====
set number
set relativenumber
set cursorline
set clipboard=unnamed
" ===== 缩进 =====
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set smartindent
" ===== 搜索 =====
set hlsearch
set incsearch
set ignorecase
set smartcase
" ===== 插件 =====
call plug#begin()
Plug 'preservim/nerdtree'
Plug 'vim-airline/vim-airline'
Plug 'tpope/vim-fugitive'
Plug 'junegunn/fzf.vim'
Plug 'tpope/vim-surround'
Plug 'morhetz/gruvbox'
call plug#end()
colorscheme gruvbox
" ===== Leader 键 =====
let mapleader = " "
nnoremap <leader>w :w<CR>
nnoremap <leader>q :q<CR>
nnoremap <leader>n :nohlsearch<CR>
nnoremap <leader>e :NERDTreeToggle<CR>
nnoremap <leader>f :Files<CR>
nnoremap <leader>v :vsp<CR>
安装插件:
vim +PlugInstall +qall
常用快捷键速查
移动
| 按键 | 功能 |
|---|---|
h j k l |
左下上右 |
w / b |
下/上一个单词 |
gg / G |
文件头/尾 |
Ctrl+d / Ctrl+u |
半页下/上 |
编辑
| 按键 | 功能 |
|---|---|
i / a |
当前位置/后面插入 |
o / O |
下方/上方新行 |
dd |
删除行 |
yy / p |
复制/粘贴行 |
u / Ctrl+r |
撤销/重做 |
ciw |
修改当前单词 |
搜索替换
/keyword " 搜索
:%s/old/new/g " 全文替换
:%s/old/new/gc " 逐个确认替换
分屏
:sp " 水平分屏
:vsp " 垂直分屏
Ctrl+w + hjkl " 分屏间移动
进阶:Neovim + LazyVim
sudo apt install neovim
git clone https://github.com/LazyVim/starter ~/.config/nvim
nvim # 自动安装所有插件
五、快速参考:判断当前 Shell
echo $SHELL # /bin/zsh 或 /bin/bash
| Shell | 配置文件 |
|---|---|
| Zsh | ~/.zshrc |
| Bash | ~/.bashrc |
配置修改后记得执行 source ~/.zshrc 使其生效。
--【壹】--:
感谢佬友分享
--【贰】--:
第一步:alias sl=‘ls’
因为总是手快打错,哈哈
--【叁】--:
感谢大佬分享
--【肆】--:
感谢大佬

