订阅 RSS 源

The Vi application is the default text editor on most Linux systems, so it's the primary interface you will use when you need to edit a configuration file. If you're used to a graphical text editor, such as Notepad++ or VS Code, Vi can be confusing at first.

In its default form, Vi launches and runs in a terminal and is entirely keyboard-driven. But once you learn the basics of Vi, you're likely to find that it's a fast, efficient, and flexible editor.

Start Vi

To start Vi, open a terminal and type vi followed by a file path. If the file you provide doesn't exist, it's created when Vi opens:

$ vi example.txt

The vi command is usually a symlink to the vim (Vi improved) command, but many users refer to Vi as Vi, not Vim.

Command mode and insert mode

Vi has two different modes. There's command mode and insert mode. Command mode is the default, and you can think of it as Vi's menu. Because Vi doesn't have a menu bar like most applications have, you use command mode to tell Vi what you want to do next (move up or down a line, delete a character or a whole word, copy and paste, and so on.)

You can always get back to command mode by pressing Esc on your keyboard. Get used to that; you'll be pressing Esc a lot in Vi.

To enter insert mode, press i. In insert mode, you can type and edit text and use the arrow keys to navigate around your document. There's not much to say about insert mode. It's very much as its name advertises: In insert mode, you can insert text, so there's nothing to learn except, maybe, touch typing.

Command mode, however, has a wealth of keyboard shortcuts and commands for you to discover. Remember, to enter command mode, press Esc. If you're ever in doubt about which mode you're in, just press Esc, and you can be sure that you're now in command mode.

Keyboard shortcuts for command mode

In Vi, you don't use your mouse or any of the keyboard shortcuts you're used to in other editors. For instance, you don't copy with Ctrl+C and paste with Ctrl+V because Vi has its own set of quick shortcuts that optimize key presses. It's unquestionably a learning curve, but one that pays dividends. Once you've got the basic keyboard shortcuts committed to muscle memory, you'll work faster in Vi than you've worked in any editor.

Modes

  • i: for insert mode
  • (that's an upper-case i): insert text at beginning of current line
  • a: append text after cursor
  • A: append text at end of current line
  • Esc: command mode

[ Boost your Linux skills by downloading the Bash shell scripting cheat sheet. ]

Navigation

Navigation happens on the home row of your QWERTY keyboard:

  • h: cursor left
  • j: cursor down (j looks trivially like a down arrow)
  • k: cursor up
  • l (that's a lower-case L): cursor right
  • b: cursor to beginning of current word
  • e: cursor to end of current word
  • $: go to end of line
  • 0 (that's a zero): go to beginning of line
  • gg: go to first line of file
  • G: go to last line of file
  • 9G: go to line 9 of file

Select

  • v: select text
  • V: select line

Copy and paste

  • y: copy ("yank") selection
  • yy: copy ("yank") the entire line
  • p: paste

Delete

  • x: delete character
  • dw: delete word
  • d$: delete from current position to end of line
  • d0: delete from current position to beginning of line
  • dG: delete to last line of file

Undo

  • u: undo

Save

  • :w: save
  • :wq or :ZZ: save and quit
  • :q!: force quit without saving

Get to know Vi

Knowing Vi is a valuable skill on Linux because it's on nearly every Linux system you're likely to encounter. Once you get comfortable with its key bindings and how they enable you to keep your hands on the keyboard instead of bouncing back and forth to your mouse or even to Function keys, you'll see why Vi has endured for so long.


关于作者

Seth Kenlon is a Linux geek, open source enthusiast, free culture advocate, and tabletop gamer. Between gigs in the film industry and the tech industry (not necessarily exclusive of one another), he likes to design games and hack on code (also not necessarily exclusive of one another).

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Virtualization icon

虚拟化

适用于您的本地或跨云工作负载的企业虚拟化的未来