[Linux-vi] The simple set of vi command

Source : https://www.cs.colostate.edu/helpdocs/vi.html

What is vi?

The default editor that comes with the UNIX operating system is called vi (visual editor). [Alternate editors for UNIX environments include pico and emacs, a product of GNU.]
The UNIX vi editor is a full screen editor and has two modes of operation:
Command mode commands which cause action to be taken on the file, and
Insert mode in which entered text is inserted into the file.
In the command mode, every character typed is a command that does something to the text file being edited; a character typed in the command mode may even cause the vi editor to enter the insert mode. In the insert mode, every character typed is added to the text in the file; pressing the <Esc> (Escape) key turns off the Insert mode.
While there are a number of vi commands, just a handful of these is usually sufficient for beginning vi users. To assist such users, this Web page contains a sampling of basic vi commands. The most basic and useful commands are marked with an asterisk (* or star) in the tables below. With practice, these commands should become automatic.
NOTE: Both UNIX and vi are case-sensitive. Be sure not to use a capital letter in place of a lowercase letter; the results will not be what you expect.
To Get Into and Out Of vi

To Start vi

To use vi on a file, type in vi filename. If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.
*    vi filename    edit filename starting at line 1
     vi -r filename    recover filename that was being edited when system crashed
To Exit vi

Usually the new or modified file is saved when you leave vi. However, it is also possible to quit vi without saving the file.
Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command is completed by hitting the <Return> (or <Enter>) key.
*    :x<Return>    quit vi, writing out modified file to file named in original invocation
     :wq<Return>    quit vi, writing out modified file to file named in original invocation
     :q<Return>    quit (or exit) vi
*    :q!<Return>    quit vi even though latest changes have not been saved for this vi call
Moving the Cursor

Unlike many of the PC and MacIntosh editors, the mouse does not move the cursor within the vi editor screen (or window). You must use the the key commands listed below. On some UNIX platforms, the arrow keys may be used as well; however, since vi was designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange effects in vi and should be avoided.
If you go back and forth between a PC environment and a UNIX environment, you may find that this dissimilarity in methods for cursor movement is the most frustrating difference between the two.
In the table below, the symbol ^ before a letter means that the <Ctrl> key should be held down while the letter key is pressed.
*    j or <Return>
  [or down-arrow]    move cursor down one line
*    k [or up-arrow]    move cursor up one line
*    h or <Backspace>
  [or left-arrow]    move cursor left one character
*    l or <Space>
  [or right-arrow]    move cursor right one character
*    0 (zero)    move cursor to start of current line (the one with the cursor)
*    $    move cursor to end of current line
     w    move cursor to beginning of next word
     b    move cursor back to beginning of preceding word
     :0<Return> or 1G    move cursor to first line in file
     :n<Return> or nG    move cursor to line n
     :$<Return> or G    move cursor to last line in file
Screen Manipulation

The following commands allow the vi editor screen (or window) to move up or down several lines and to be refreshed.
     ^f    move forward one screen
     ^b    move backward one screen
     ^d    move down (forward) one half screen
     ^u    move up (back) one half screen
     ^l    redraws the screen
     ^r    redraws the screen, removing deleted lines
Adding, Changing, and Deleting Text

Unlike PC editors, you cannot replace or delete text by highlighting it with the mouse. Instead use the commands in the following tables.
Perhaps the most important command is the one that allows you to back up and undo your last action. Unfortunately, this command acts like a toggle, undoing and redoing your most recent action. You cannot go back more than one step.
*    u    UNDO WHATEVER YOU JUST DID; a simple toggle
The main purpose of an editor is to create, add, or modify text for a file.
Inserting or Adding Text

The following commands allow you to insert and add text. Each of these commands puts the vi editor into insert mode; thus, the <Esc> key must be pressed to terminate the entry of text and to put the vi editor back into command mode.
*    i    insert text before cursor, until <Esc> hit
     I    insert text at beginning of current line, until <Esc> hit
*    a    append text after cursor, until <Esc> hit
     A    append text to end of current line, until <Esc> hit
*    o    open and put text in a new line below current line, until <Esc> hit
*    O    open and put text in a new line above current line, until <Esc> hit
Changing Text

The following commands allow you to modify text.
*    r    replace single character under cursor (no <Esc> needed)
     R    replace characters, starting with current cursor position, until <Esc> hit
     cw    change the current word with new text,
starting with the character under cursor, until <Esc> hit
     cNw    change N words beginning with character under cursor, until <Esc> hit;
  e.g., c5w changes 5 words
     C    change (replace) the characters in the current line, until <Esc> hit
     cc    change (replace) the entire current line, stopping when <Esc> is hit
     Ncc or cNc    change (replace) the next N lines, starting with the current line,
stopping when <Esc> is hit
Deleting Text

The following commands allow you to delete text.
*    x    delete single character under cursor
     Nx    delete N characters, starting with character under cursor
     dw    delete the single word beginning with character under cursor
     dNw    delete N words beginning with character under cursor;
  e.g., d5w deletes 5 words
     D    delete the remainder of the line, starting with current cursor position
*    dd    delete entire current line
     Ndd or dNd    delete N lines, beginning with the current line;
  e.g., 5dd deletes 5 lines
Cutting and Pasting Text

The following commands allow you to copy and paste text.
     yy    copy (yank, cut) the current line into the buffer
     Nyy or yNy    copy (yank, cut) the next N lines, including the current line, into the buffer
     p    put (paste) the line(s) in the buffer into the text after the current line
Other Commands

Searching Text

A common occurrence in text editing is to replace one word or phase by another. To locate instances of particular sets of characters (or strings), use the following commands.
     /string    search forward for occurrence of string in text
     ?string    search backward for occurrence of string in text
     n    move to next occurrence of search string
     N    move to next occurrence of search string in opposite direction
Determining Line Numbers

Being able to determine the line number of the current line or the total number of lines in the file being edited is sometimes useful.
     :.=    returns line number of current line at bottom of screen
     :=    returns the total number of lines at bottom of screen
     ^g    provides the current line number, along with the total number of lines,
in the file at the bottom of the screen
Saving and Reading Files

These commands permit you to input and output files other than the named file with which you are currently working.

:r filename<Return>    read file named filename and insert after current line
(the line with cursor)
     :w<Return>    write current contents to file named in original vi call
     :w newfile<Return>    write current contents to a new file named newfile
     :12,35w smallfile<Return>    write the contents of the lines numbered 12 through 35 to a new file named smallfile
     :w! prevfile<Return>    write current contents over a pre-existing file named prevfile

时间: 2024-11-03 21:52:45

[Linux-vi] The simple set of vi command的相关文章

Unix Linux vi vim 使用手册|vi vim 常用命令 详解

Unix Linux vi vim 使用手册 Vi 简介 Vi 是 Unix 世界里极为普遍的全萤幕文书编辑器,几乎可以说任何一台 Unix 机器都 会提供这套软体.Linux 当然也有,它的 vi 其实是 elvis(版权问题),不过它们都 差不多.熟悉 DOS 下的文书处理後,也许会感到 vi 并不好用:Unix 上也已经发展出 许多更新.更好用的文书编辑器,但是并不一定每一台 Unix 机器上都会安装这些额外 的软体.所以,学习 vi 的基本操作还是有好处,让你在各个不同的机器上得心应手.

Linux命令(5):vi

进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件,并将光标置于第n行首 vi + filename :打开文件,并将光标置于最后一行首 vi +/pattern filename:打开文件,并将光标置于第一个与pattern匹配的串处 vi -r filename :在上次正用vi编辑时发生系统崩溃,恢复filename vi filename....filename :打开多个文件,依次进行编辑 移动光标类命令h :光标左

Linux学习笔记 (三)Vi文本编辑器

vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版本,vi编辑器是完全相同的,因此您可以在其他任何介绍vi的地方进一步了解它.Vi也是Linux中最基本的文本编辑器,学会它后,您将在Linux的世界里畅行无阻. 1.vi的基本概念  基本上vi可以分为三种状态,分别是命令模式(command mode).插入模式(Insert mode)和底行模式(last line

[转]linux下终端常用命令和vi命令修改文件及保存的使用方法

首先介绍一下Ubuntu下各个目录的一般作用: / 这就是根目录,一台电脑有且只有一个根目录,所有的文件都是从这里开始的.举个例子:当你在终端里输入"/home",你其实是在告诉电脑,先从/(根目录)开始,再进入到home目录. /root 系统管理员(root user)的目录.至于系统管理员的权限有多大我这里就不在废话了.因此,请小心使用root帐号. /boot 系统启动文件,所有与系统启动有关的文件都保存在这里 . /bin 这里是存放系统的程序. /etc 主要存放了系统配置

LINUX初学之文本编辑器(vi ,vim)

Linux 系统的哲学思想是一切皆文件,我们平常需要往文件填充内容时经常用到文本编辑器在之前我们便学习过 nano 编辑器,但 nano 编辑器功能较单一,不适合进行复杂操作,本篇将主要学习功能更为强大的 VI 和 VIM 文本编辑器.VI和VIM编辑器又被称为模式化编辑器,通常在几个模式下进行一系列的操作.VI/VIM大致有编辑模式(命令模式),末行模式,插入模式(输入模式),替换模式和可视化模式几种.下面将介绍具体操作: ⑴ VI/VIM的打开方式:①打开VI/VIM的界面: ②VI/VIM

LInux切换到root用户下vi语法高亮不成功

配置vi时,一般的用户会显示语法高亮,但是切换到root用户高亮总是不成功,什么原因呢?(redhat) 原来对于一般用户,当我们查看命令别名时会发现vi = vim [[email protected] ~]$ which vialias vi='vim'  /usr/bin/vim 而对于root来说,则默认没有定义此别名,因而vi就是vi,而不是vim [[email protected] robin]# which vi/bin/vi vi没有这个功能,vim才有语法高亮的功能. 知道原

linux下配置QT5.12 ERROR: Unknown command line option &#39;-no-xcursor&#39;.!

xcursor是个什么东西.为什么会报错,怎么处理!在qt的autoconfigure.sh中添加下三条命令都不行.提示不认识-no-xcursor或者不认识nomake命令. -no-xcursor -no-xfixes -no-xrandr -no-xrender -no-separate-debug-info -no-fontconfig \-nomake examples -nomake tools -nomake tests -no-iconv \ linux下配置QT5.12 ERR

Linux 命令行报错 -bash: vi: command not found

Mac 下使用终端遇到了这个问题: appledeMacBook-Air:~ air$ vi .bash_profile -bash: vi: command not found 如图所示: 解决方法寻找中…… 这篇文章 Linux命令行报bash:.....:command not found的解决办法 介绍的方法能暂时用,但还未解决,继续摸索中…………

linux简单介绍,helloworld,vi使用,用户管理

linux特点1.免费的.开源的2.支持多线程.多用户的3.安全性好4.对内存和文件管理优越 缺点:操作相对困难 linux最小只需要4m -> 嵌入式开发 我们使用 vm[虚拟机] 虚拟了一个 linux startx 进入图形化界面图形界面注销回到命令行 linux命令 shutdown -h now 立刻进行关机shotdown -r now 重启计算机reboot 重启计算机 su - 登陆时尽量少用root登陆,因为它是系统管理员,最大的权限,避免操作失误,可以利用普通用户登录,登陆后