Taught you how to build a Linux development environment (VMware + Ubuntu) (c) - Under Ubuntu vim editor of installation, configuration and use

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_34258344/article/details/97525312

Foreword

This paper describes the installation under Ubuntu vim, configuration and use, vim is a powerful full-screen text editor, is on Linux / UNIX most commonly used only command no menu text editor, its role is to establish, edit, display text files. Installation is relatively simple, focusing on the configuration and use of lower vim.
PS:
no students Ubuntu install, we recommend the venue:
Taught you how to build a Linux development environment (VMware + Ubuntu) (a)
this blog will use some of the Linux bash command. For more command, the proposed venue :
Linux beginners caught --Linux common commands

1, vim installation

Enter the command in the Ubuntu system:

sudo apt-get install vim

Screen appears as shown below, enter y press ENTER:
Here Insert Picture Description
waiting for an input command as installed, if the version information appears vim, the installation is complete vim

vim -v

2, vim configuration

When the installation is complete vim just edit text in the terminal, by default, it is generally not displayed on the editing interface line numbers, syntax highlighting, smart indenting and other functions. In order to better carry out work in vim, you need to manually set up a profile: .vimrc. When you start vim, .vimrc file in the current user's home directory will be automatically read, the document may contain some settings even a script, so, in general, to create a more convenient in the root directory of the current user .vimrc file is created the command

vim ~/.vimrc

Only .vimrc modification under a single user's current directory, then modify the content is only for the user, if it is valid for all users, you can modify the superuser privileges to modify / etc / vim / vimrc, set up after use: wq to save and exit can, if difficult for beginners to operate the configuration, the configuration may look of vim.

(1) Basic Configuration

Configuration items are generally "open" and "closed" two settings, "close" is in the "open" preceded by the prefix "no", can also be commented out in the configuration file keyword to indicate close the setup, configuration file "" "indicates the current line a comment.
common configuration example

Keyword description
not set Open line number
set nonu Close line number
set nocompatible Is not compatible with vi
syntax on Open the syntax highlighting. Automatic identification code, using a variety of display colors
set showcmd The command mode, shown at the bottom, the current instruction type
set mouse=a It supports the use of mouse
set encoding=utf-8 Utf-8 encoded using
set autoindent After pressing Enter, the next line will be automatically indented to keep up with consistent line indent
set relativenumber Line number of the current line of the cursor, relative to the other row is the row number of the row
set cursorline Cursor to highlight the current line
set textwidth=80 Set line width, that is, how many characters a line display
set wrap Automatic off-line, i.e., long line into several display lines
set nowrap Close automatic folding line
set ruler In the status bar displays the current position of the cursor (which column about which line)
set showmatch 光标遇到圆括号、方括号、大括号时,自动高亮对应的另一个圆括号、方括号和大括号
set hlsearch 搜索时,高亮显示匹配结果

(2)自定义配置

我们也可以根据自己的需要,在vim的配置文件中加入自己的配置。例如我们在编写shell脚本的时候,需要添加注释,往往会先把光标移动到那一行,按0光标定位到行首,再进入插入模式,再按下#,再按按键Esc,这个过程太麻烦,我们可以自定义快捷键,格式如下:

:map 快捷键 命令

举个栗子

  • shell脚本添加注释
:map ^P I#<esc>

注^P是同时按下Ctrl+v+p,有颜色变化,如下图简单的shell脚本所示,按回车后只需要在当前行任意位置按下Ctrl+p就可以在命令行下快速注释
Here Insert Picture Description

  • shell脚本连续行注释
:n1,n2s/^/#/g
  • shell脚本取消注释
:map ^B 0x
  • shell脚本连续行取消注释
:n1,n2s/^#//g
  • 还有我们常用的C语言连续行注释
:n1,n2s/^/\/\//g

注:其中^为行首,g为无需确认,<按键>表示按键,\为转义字符

  • 快捷键Ctrl+M插入个人邮箱
:map ^M [email protected]<esc>

替换功能也很有用,比如我们在编辑文档时需要经常键入我们的邮箱,就可以在配置文件中写入以下ab命令,这样在编辑文档时,只需要写mymail,它会自动变成我们那一长串邮箱。

ab mymail [email protected]

永久生效需要修改配置文件,在vimrc中直接加入上述命令即可,此时无需加“ : ”。

3、vim的使用

先观此图
Here Insert Picture Description
此图展示了vim 的三种模式,分别是命令模式(Command mode),输入模式(Insert mode)和底线命令模式(Last line mode),以及切换各模式的方法。
下面介绍一下vim的常用命令

  • 插入命令
命令 描述
a 在光标所在字符后插入
A 在光标所在行尾插入
i 在光标所在字符前插入
I 在光标所在行行首插入
o 在光标下插入新行
O 在光标下插入新行
  • 定位命令
命令 描述
:set nu 设置行号
:set nonu 取消行号
gg 到第一行
G 到最后一行
nG 到第nhang
:n 到第n行
$ 到行尾
0 到行首
  • 删除命令
命令 描述
x 删除光标处字符
nx 删除光标所在处后n个字符
dd 删除光标所在行
ndd 删除光标所在行后n行
dG Delete the line the cursor to the end of the file content
D Delete at the cursor to the end of the line content
:n1,n2d Delete the specified range of row
  • Cut and Copy commands
command description
yy Copy the current line
nyy Copy the current row after row n
dd Cut the current line
ndd After the current line n line
p Paste in the current row
P Pasted on the current line
  • Replace and cancel the command
command description
r Replace the character at the cursor location
R To begin replacing characters from under the cursor, press Esc to end
in Undo
  • Search Search and Replace command
command description
/ String Search string (ignore case: set ic)
n Position in the search for a specified string appears
:%s/old/new/g Replace text specified string
:n1,n2/old/new/g Alternatively specified string in the specified range
  • Save and Exit command
command description
:w Save changes
: W path / filename Save for the specified file
:wq Save and exit
ZZ Save and exit (shortcuts)
:q! Exit without saving
:wq! Read-only file save and exit (root and owner of the file is available)

Reference links:
http://vimdoc.sourceforge.net/htmldoc/options.html
https://dougblack.io/words/a-good-vimrc.html

Since the use of vim, Let's learn together using gcc compiler, see next blog:
install and use gcc compiler

Guess you like

Origin blog.csdn.net/qq_34258344/article/details/97525312