WSL安装及基本配置指南
前言
这里安装的是WSL,而不是WSL2,如果想升级,参考其余资料即可。对于一般用户来说,WSL完全够用。
安装
勾选适用于Linux的Windows子系统
控制面板-程序-启用或关闭Windows功能-适用于Linux的Windows子系统
,然后按照提示重启电脑
wsl --install进行安装
我安装的是20.04 LTS版本的(-d指明版本号)
wsl --install -d Ubuntu-20.04
输入用户名和密码
安装完成后,系统会提示输入用户名和密码,没有必要和Windows下的用户名保持一致
查看版本号
查看WSL版本号,嗯,是1,不是2
换源
这里选择清华源,不过有几点需要注意
版本号的选择(不要无脑复制)
备份文件:cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo vim /etc/apt/sources.list
,将上述内容粘贴进去
进入网址http://archive.ubuntu.com/ubuntu/pool/main/c/ca-certificates/
找到对应的20.04文件
终端执行以下命令:
cd /tmp
wget http://archive.ubuntu.com/ubuntu/pool/main/c/ca-certificates/ca-certificates_20211016~20.04.1_all.deb
sudo dpkg -i ./ca-certificates_20211016~20.04.1_all.deb
然后执行sudo apt-get update
和 sudo apt-get upgrade
即可
vim的基本配置
终端启动wsl
在Windows Terminal
(也就是终端)中启动wsl,之后cd ~
,进入~目录
然后 touch .vimrc
,创建vim的配置文件。然后vim .vimrc
,按下i
键,然后删除内容(记得先备份一个,防止之后想要恢复)。屏幕空白后,将如下内容粘贴到文件中(参考 missing semester
)
" Comments in Vimscript start with a `"`.
" If you open this file in Vim, it'll be syntax highlighted for you.
" Vim is based on Vi. Setting `nocompatible` switches from the default
" Vi-compatibility mode and enables useful Vim functionality. This
" configuration option turns out not to be necessary for the file named
" '~/.vimrc', because Vim automatically enters nocompatible mode if that file
" is present. But we're including it here just in case this config file is
" loaded some other way (e.g. saved as `foo`, and then Vim started with
" `vim -u foo`).
set nocompatible
" Turn on syntax highlighting.
syntax on
" Disable the default Vim startup message.
set shortmess+=I
" Show line numbers.
set number
" This enables relative line numbering mode. With both number and
" relativenumber enabled, the current line shows the true line number, while
" all other lines (above and below) are numbered relative to the current line.
" This is useful because you can tell, at a glance, what count is needed to
" jump up or down to a particular line, by {count}k to go up or {count}j to go
" down.
set relativenumber
" Always show the status line at the bottom, even if you only have one window open.
set laststatus=2
" The backspace key has slightly unintuitive behavior by default. For example,
" by default, you can't backspace before the insertion point set with 'i'.
" This configuration makes backspace behave more reasonably, in that you can
" backspace over anything.
set backspace=indent,eol,start
" By default, Vim doesn't let you hide a buffer (i.e. have a buffer that isn't
" shown in any window) that has unsaved changes. This is to prevent you from "
" forgetting about unsaved changes and then quitting e.g. via `:qa!`. We find
" hidden buffers helpful enough to disable this protection. See `:help hidden`
" for more information on this.
set hidden
" This setting makes search case-insensitive when all characters in the string
" being searched are lowercase. However, the search becomes case-sensitive if
" it contains any capital letters. This makes searching more convenient.
set ignorecase
set smartcase
" Enable searching as you type, rather than waiting till you press enter.
set incsearch
" Unbind some useless/annoying default key bindings.
nmap Q <Nop> " 'Q' in normal mode enters Ex mode. You almost never want this.
" Disable audible bell because it's annoying.
set noerrorbells visualbell t_vb=
" Enable mouse support. You should avoid relying on this too much, but it can
" sometimes be convenient.
set mouse+=a
" Try to prevent bad habits like using the arrow keys for movement. This is
" not the only possible bad habit. For example, holding down the h/j/k/l keys
" for movement, rather than using more efficient movement commands, is also a
" bad habit. The former is enforceable through a .vimrc, while we don't know
" how to prevent the latter.
" Do this in normal mode...
nnoremap <Left> :echoe "Use h"<CR>
nnoremap <Right> :echoe "Use l"<CR>
nnoremap <Up> :echoe "Use k"<CR>
nnoremap <Down> :echoe "Use j"<CR>
" ...and in insert mode
inoremap <Left> <ESC>:echoe "Use h"<CR>
inoremap <Right> <ESC>:echoe "Use l"<CR>
inoremap <Up> <ESC>:echoe "Use k"<CR>
inoremap <Down> <ESC>:echoe "Use j"<CR>
"关闭按键错误提醒
"set vb t_vb=
"au GuiEnter * set t_vb=
"解决中文乱码
set nu
set fencs=utf-8,gbk,utf-16,utf-32,ucs-bom
之后就:wq
保存修改并退出,vim的界面就变得极其美观了。
为什么要用终端启动wsl
这就要牵扯到了一个坑,如果你在开始界面输入Ubuntu
,然后点击这个图标,会进入一个黑框框
如果此时你用vim编辑一个中文文件,就会发现汉字重影的问题,具体阐释就是光标左右移动时,下方的汉字会重叠在一起。可能原因是汉字和英文的宽度不同,导致vim的移动距离出现偏差。
具体图片如下
简单来说,直接启动wsl(以及通过git bash启动wsl)会让vim出现问题,但是从终端启动wsl却不会出现这个问题,汉字不会出现重影,并且界面简洁美观。