Skip to content

Latest commit

 

History

History
172 lines (101 loc) · 7.56 KB

vi-101.md

File metadata and controls

172 lines (101 loc) · 7.56 KB

vi-101

IMPORTANT - The vimium chrome extension is known to cause issues with using vi in lab terminals due to the fact that it intercepts the keystrokes that the vi editor is expecting. The whole point of this extension is to use vi commands to drive the browser itself, therefore the vi inside the browser will never receive the commands!
If you have this extension, please disable it.

Other browser extensions may also cause issues, especially ones that add or change hot keys.

Real simple guide to the most important stuff with vi editor and questions commonly asked

REMEMBER Labs work best in Chrome. Firefox and Edge should be OK. Other browsers like Brave etc. are not verified so you shouldn't use them.

PRACTICE is king! Try out and get used to all these techniques in the KodeKloud labs so that they are second nature when you come to the exam.

I cannot type anything!

Probably the first time you've used vi right? When you start the editor, it is in what is known as normal mode, which is a command entry mode. Certain keystrokes will be interpreted as commands, others may be ignored. What won't be happening is text is being typed into the editor.

To begin editing, you must enter insert mode, by typing i. If you've typed some other keys in confusion, this may not work immediately. Press ESC then press i. The bottom line of the editor should read --INSERT--. Now you can edit stuff.

I cannot exit vi

Some possibilities.

  1. You are not in normal mode.

    You need to be in normal mode to execute any of the commands to exit the editor. If it says INSERT, VISUAL or anything like that at the bottom of the screen, press ESC until it no longer does.

    Then you should be able to press : and see it come up at the bottom of the screen, so you're able to enter something like wq

  2. You are editing a read-only file.

    You get a message like E505 "file" is read only (add ! to override) when you try :wq.

    The message is encouraging you to do :wq! to force overwrite of a read-only file, but if that file belongs to root and you are not root, then this isn't going to work and you'll get the same error. The only way to exit in this case is to exit without saving, which is :q!. For root owned files, you must use sudo, e.g.

    sudo vi /etc/hosts
    

    Note that in Kubernetes labs, you are already the root user so you'll never need sudo. In Linux labs, you are never the root user when the lab starts, and you'll need sudo a lot!

Modes

The editor has several modes. It operates differently in each mode.

Normal mode

This is the mode that you are in when you first start vi. It is a command mode, therefore most key strokes are interpreted as editor commands. Some useful commands are detailed further down.

Pressing the ESC key when in any other mode returns you to Normal mode. When you are in Normal mode, typing : should make that character appear at the bottom left corner of the vi workspace.

Insert mode

In this mode you can edit text in the document. It should say -- INSERT -- at the bottom of the screen.

To enter Insert mode, hit i when in Normal mode

Visual Mode

In this mode you can select blocks of text on which to operate. Use normal mode commands to operate on the selected text. There are 3 flavours of this:

  • Visual - This mode allows you to select text character by character using cursor keys. It will say -- VISUAL -- at the bottom of the screen. Press v when in normal mode to enter.
  • Visual Line - This mode allows you to select entire lines. It will say -- VISUAL LINE -- at the bottom of the screen. Press SHIFT-V when in normal mode to enter.
  • Visual Block - This mode allows you to select blocks of text horizontally and vertically. It will say -- VISUAL BLOCK -- at the bottom of the screen. Press CTRL-V when in normal mode to enter.

Most useful normal mode commands

All the following work in normal mode. As soon as you enter insert mode, you need to press ESC before any of the following will work again.

Files

  • Saving without exiting

    • :w - When editing Kubernetes YAML in the exam, do this and run kubectl apply in another terminal window. Saves you going in and out the editor if you have syntax errors. Only quit vi once kubectl is successful.
  • Exit without saving

    • :q!
  • Exit and save

    Both these commands achieve the same thing.

    1. :wq
    2. :x
  • Pasting from documentation

    • :set paste - Turn on paste mode. Then hit i for insert mode. It will say INSERT (paste)
    • :set nopaste - Turn off paste mode. Do this immediately after pasting from documentation.
  • Getting rid of TABs in YAML

Editing

  • i - Enter insert mode at cursor position
  • SHIFT - A - Enter insert mode at end of current line
  • y - Yank (copy) current line to vi internal buffer (not desktop clipboard)
  • p - Paste from vi internal buffer. It will paste below not at the line the cursor is on.
  • dd - Delete (cut) line under cursor. (2dd next 2 lines, 5dd next 5 lines etc.). Lines removed this way are placed in vi internal buffer, i.e. can be pasted.
  • u - Undo last edit (2u last 2 edits etc.)
  • r - Replace character under the cursor. Press r followed by the character you want to put in. It will replace the character that the cursor is on.
  • SHIFT + D - Delete from cursor to end of line
  • gg - Go to top of file
  • SHIFT + G - Go to last line in file
  • > - Indent current line one tab stop. (. indent again)
  • < - Outdent current line one tab stop. (. outdent again)
  • SHIFT + V - Enter visual line mode. You can select multiple lines of text and perform operations like dd, y, >, < on the selected lines

Line Numbers

There are a couple of modes for showing line numbers in the editor. Generally you'll want to show line numbers so that when an error message from some command refers to a line number in a file you're processing, you can find it. You change the line number mode from normal mode using : commands.

Absolute numbers

This mode numbers the lines normally starting with 1 as the first line of the file.

Turn on/off with :set nu/:set nonu

Relative numbers

This mode shows numbers relative to where the cursor is positioned, so line 1 is the line directly following the cursor. This mode is especially useful for finding an error in a Kubernetes manifest containing multiple resource documents.

Turn on/off with :set rnu/:set nornu

See examples of these modes in action here

Suggested settings for YAML editing

When taking an exam, open a terminal and do this first before attempting any questions.

Create a file ~/.vimrc and add the following lines to it.

set nu
set sw=2
set et
set ts=2
set ai

What these do, in order:

  1. Enable line numbers

  2. Set shift width 2 chars

    Note: May already be configured on exam terminal

  3. Expand tabs to spaces

    Note: May already be configured on exam terminal

  4. Set tab stop to 2 chars

    Note: May already be configured on exam terminal

  5. Enable auto indent

Return to main FAQ