Linux学习笔记(三)------Vim编辑器

Linux学习笔记(三)------Vim编辑器

8、Vim编辑器

8.1 简介

  • Vim是从vi发展出来的一个文本编辑器。代码补完、编译及错误跳转等方便编程的功能特别丰富,在程序员中广泛被使用。尤其是Linux中,必须学会Vim

  • Vim可以通过一些插件实现和IDE一样的功能

  • Vim是程序开发者一项很好用的工具,可用于查看内容,编辑内容,保存内容

  • 所有Unix Like系统都会内建vi文书编辑器,其他的文书编辑器则不一定会存在

  • Vim就是程序开发工具而不是文字处理软件

  • Vim官网:welcome home : vim online

  • Vim键盘图

在这里插入图片描述

8.2 三种使用模式

  • vi/Vim分为三种使用模式:命令模式(Command mode)输入模式(Insert mode)底线命令模式(Last line mode)
8.2.1 如何进入Vim
  • 新建一个文件vim zstudy.txt,如果文件存在,则修改文件,文件不存在新建文件
8.2.2 命令模式
  • 用户启动vi/Vim,就进入了命令模式

在这里插入图片描述

  • 此状态下敲击键盘动作会被Vim识别为命令,而非输入字符。例如:按下i,并不会输入字符,而被当作操作了i命令

  • 常用命令:

i:切换到输入模式,可以输入字符

x:删除当前光标所在处的字符

::切换到底线命令模式,可以在最下面一行输入命令。如果在编辑模式,需要先退出编辑模式,Esc退出
  • 命令模式只有一些最基本的命令,需要依靠底线命令模式输入更多命令
8.2.3 输入模式
  • i进入输入模式,可以编辑文本

在这里插入图片描述

  • 在输入模式下,可以用以下按键
字符按键以及Shift组合:输入字符

Enter:换行

Backspace:删除光标前一个字符

Delete:删除光标后一个字符

左右方向键:在文本中移动光标

上下方向键:商家翻页

Home/End:移动光标到行首/行尾

Insert:切换光标为输入/替换模式,光标变成竖线/下划线

Esc:退出输入模式,切换到命令模式
8.2.4 底线命令模式
  • 在命令模式下按:进入底线命令模式。光标移动到最低端,可以输入底线命令

在这里插入图片描述

  • 底线命令模式可以输入单个或多个字符的命令,可使用非常多命令
  • 常见基本命令:
#完成操作后,通常输入命令wq【在底线命令模式中】,保存文件并退出Vim
q:退出程序

w:保存文件

Ese:退出底线命令模式
8.2.5 小结
  • 完成流程:
    • 新建或编辑文件,按i进如编辑模式,编写内容
    • 编写完成后,按Esc退出编辑模式
    • :进入底线命令模式
    • wq保存文件并退出
  • Vim工作图示

在这里插入图片描述

8.3 Vim按键说明

  • 除了上述常用按键,Vim还有非常多按键可以使用
8.3.1 第一部分:命令模式
  • 一般模式可用的光标移动、复制粘贴、搜索替换等

  • 移动光标的方法

按键含义
k 或 上方向键光标向上移动一个字符
j 或 下方向键光标向下移动一个字符
h 或 左方向键光标向左移动一个字符
i 或 右方向键光标向右移动一个字符
ctrl + f屏幕向下移动一页
ctrl + b屏幕向上移动一页
ctrl + d屏幕向下移动半页
ctrl + u屏幕向上移动半页
+光标移动到非空格符的下一行
-光标移动到非空格符的上一行
数字+空格快捷切换光标,光标向右移动N个字符
0 或 功能键Home移动到这一行的最前面字符处
$ 或 功能键End移动到这一行的最后面字符处
H光标移动到这个屏幕最上方那行的第一个字符
M光标移动到这个屏幕中间那行的第一个字符
L光标移动到这个屏幕最下方那行的第一个字符
G移动到这个文件的最后一行
数字+G移动到这个文件的第N行
gg移动到这个文件的第一行
数字+Enter光标向下移动N行
  • 搜索替换
按键含义
/word光标之下寻找一个名为word的字符串
?word光标之上寻找一个名为word的字符串
n重复前一个搜索动作,寻找下一个word字符串
N重复前一个搜索动作,寻找上一个word字符串
  • 删除、复制与粘贴
按键含义
x,Xx向后删除一个字符(Delete),X向前删除一个字符(Backspace)
数字+x连续向后删除N个字符
dd删除游标所在的一整行
数字+dd删除游标所在向下N行
d1G删除游标所在到第一行的所有数据
dG删除游标所在到最后一行的所有数据
d$删除游标所在位置到本行最后一个字符
d0删除游标所在位置到本行第一个字符
yy复制游标所在的一行
数字+yy复制游标所在向下n行
y1G复制游标所在到第一行所有数据
yG复制游标所在到最后一行所有数据
y0复制游标所在位置到本行第一个字符
y$复制游标所在位置到本行最后一个字符
p,Pp:将已复制数据粘贴在游标下一行。P:将将已复制数据粘贴在游标上一行
J将光标所在行与下一行数据结合成同一行
c重复删除多个数据,例如:10cj,向下删除10行
u复原上一个动作
Ctrl + r重做上一个动作
8.3.2 第二部分:输入模式
按键含义
i,Ii:从目前光标处输入。I:从目前所在行第一个非空格符处输入
a,Aa:从目前光标所在下一个字符处开始输入。A:从目前光标所在行最后一个字符处开始输入
o,Oo:从目前光标所在的下一行,输入新的一行。O:从目前光标所在的上一行,输入新的一行
r,Rr:取代光标所在的那个字符一次。R:一直取代光标所在文字,直到按下Esc
Esc退出输入模式,回到命令模式
8.3.3 第三部分:底层命令模式
按键含义
:w将编辑的数据写入硬盘档案中,即保存文件
:w!文件属性只读时,强制写入文件
:q退出Vim
:q!强制退出Vim,不保存文件
!一般代表强制的意思
:wq保存并退出
ZZ若文件无改动,不保存退出;文件改动,保存退出
:w + 文件名将编辑的数据存储成新文件
:r + 文件名在编辑数据中,读入其他文件的数据,内容加到当前光标后
:n1,n2 w + 文件名将从n1到n2的内容,存到新文件
:! command退出Vim命令行模式下执行command,显示结果。command可替换
:set nu显示行号
:set nonu取消显示行号
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
注:下载后,评价给5星,还你11分 Part I. Basic and Advanced vi 1. The vi Text Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 A Brief Historical Perspective 5 Opening and Closing Files 6 Quitting Without Saving Edits 10 2. Simple Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 vi Commands 13 Moving the Cursor 14 Simple Edits 18 More Ways to Insert Text 30 Joining Two Lines with J 31 Review of Basic vi Commands 32 3. Moving Around in a Hurry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Movement by Screens 35 Movement by Text Blocks 38 Movement by Searches 39 Movement by Line Number 43 Review of vi Motion Commands 44 4. Beyond the Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 More Command Combinations 47 Options When Starting vi 48 Making Use of Buffers 51 Marking Your Place 52 Other Advanced Edits 53 Review of vi Buffer and Marking Commands 53 vii 5. Introducing the ex Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 ex Commands 55 Editing with ex 58 Saving and Exiting Files 63 Copying a File into Another File 65 Editing Multiple Files 65 6. Global Replacement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Confirming Substitutions 72 Context-Sensitive Replacement 73 Pattern-Matching Rules 74 Pattern-Matching Examples 81 A Final Look at Pattern Matching 89 7. Advanced Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Customizing vi 95 Executing Unix Commands 99 Saving Commands 103 Using ex Scripts 114 Editing Program Source Code 120 8. Introduction to the vi Clones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 And These Are My Brothers, Darrell, Darrell, and Darrell 125 Multiwindow Editing 126 GUI Interfaces 127 Extended Regular Expressions 128 Enhanced Tags 129 Improved Facilities 134 Programming Assistance 138 Editor Comparison Summary 140 Nothing Like the Original 141 A Look Ahead 141 Part II. Vim 9. Vim (vi Improved): An Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Overview 146 Where to Get Vim 150 Getting Vim for Unix and GNU/Linux 151 Getting Vim for Windows Environments 156 Getting Vim for the Macintosh Environment 157 Other Operating Systems 157 viii | Table of Contents Aids and Easy Modes for New Users 157 Summary 158 10. Major Vim Improvements over vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Built-in Help 159 Startup and Initialization Options 160 New Motion Commands 167 Extended Regular Expressions 169 Customizing the Executable 171 11. Multiple Windows in Vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Initiating Multiwindow Editing 174 Opening Windows 177 Moving Around Windows (Getting Your Cursor from Here to There) 180 Moving Windows Around 181 Resizing Windows 183 Buffers and Their Interaction with Windows 186 Playing Tag with Windows 190 Tabbed Editing 191 Closing and Quitting Windows 192 Summary 193 12. Vim Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 What’s Your Favorite Color (Scheme)? 195 Dynamic File Type Configuration Through Scripting 205 Some Additional Thoughts About Vim Scripting 213 Resources 218 13. Graphical Vim (gvim) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 General Introduction to gvim 220 Customizing Scrollbars, Menus, and Toolbars 225 gvim in Microsoft Windows 236 gvim in the X Window System 237 GUI Options and Command Synopsis 237 14. Vim Enhancements for Programmers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Folding and Outlining (Outline Mode) 240 Auto and Smart Indenting 251 Keyword and Dictionary Word Completion 259 Tag Stacking 268 Syntax Highlighting 270 Compiling and Checking Errors with Vim 279 Some Final Thoughts on Vim for Writing Programs 284 Table of Contents | ix 15. Other Cool Stuff in Vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Editing Binary Files 285 Digraphs: Non-ASCII Characters 287 Editing Files in Other Places 289 Navigating and Changing Directories 290 Backups with Vim 292 HTML Your Text 293 What’s the Difference? 294 Undoing Undos 296 Now, Where Was I? 297 What’s My Line (Size)? 300 Abbreviations of Vim Commands and Options 302 A Few Quickies (Not Necessarily Vim-Specific) 303 More Resources 304 Part III. Other vi Clones 16. nvi: New vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Author and History 307 Important Command-Line Arguments 308 Online Help and Other Documentation 309 Initialization 309 Multiwindow Editing 310 GUI Interfaces 311 Extended Regular Expressions 311 Improvements for Editing 312 Programming Assistance 315 Interesting Features 315 Sources and Supported Operating Systems 315 17. Elvis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Author and History 317 Important Command-Line Arguments 317 Online Help and Other Documentation 319 Initialization 319 Multiwindow Editing 320 GUI Interfaces 323 Extended Regular Expressions 328 Improved Editing Facilities 328 Programming Assistance 332 Interesting Features 335 elvis Futures 340 x | Table of Contents Sources and Supported Operating Systems 340 18. vile: vi Like Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 Authors and History 343 Important Command-Line Arguments 344 Online Help and Other Documentation 345 Initialization 346 Multiwindow Editing 347 GUI Interfaces 349 Extended Regular Expressions 357 Improved Editing Facilities 359 Programming Assistance 365 Interesting Features 368 Sources and Supported Operating Systems 374

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值