Vim Learning 1 - Introduction, File Operations & Navigation

Made by Mike_Zhang


Unfold Linux Topics $\triangledown$


0. Vim Cheat Sheet

Image source: Graphical vi-vim Cheat Sheet and Tutorial - www.viemu.com


1. Introduction

There’s so much you want to do in life, and so little time. It’s the story of our modern lives. Now, you want to learn Vim. Congratulations for making this decision! (J. Ilic)

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as “vi” with most UNIX systems and with Apple OS X. (https://www.vim.org/)

Vim:

  • A special text editor
  • Two main ideas:
    • Modal editing, Operators
      • Because most of the time when we are coding, we are not typing, but rather moving and viewing around the code and editing some parts of it.
      • Vim set different modes to separate the different actions and tasks.

Try Vim online: Interactive Vim Tutorial

1.1 Installing

  • Ubuntu/Debian: apt-get install vim
  • CentOS/Fedora: yum install vim
  • MacOS: brew install vim

1.2 Starting

  • vim: start vim
  • vim filename: open a file in vim
  • vim +NUM filename: open a file in vim and go to line NUM
    • Very useful when debugging, you can go to the line where the error occurs directly.

2. Basic Concepts

2.1 Modes

There are totally 12 modes in Vim, but following 5 are the most important ones:

  1. Normal Mode

    • The default mode when starting Vim;
    • For entering normal editor commands, navigation and text manipulation.
    • Press Esc to return to Normal mode from other modes;
  2. Insert Mode

    • For entering text into the file;
    • Press i to enter Insert mode from Normal mode;
    • -- INSERT -- is shown at the bottom of the screen.
  3. Command Mode

    • For running Ex commands (:wq), entering search patterns (/word), and entering filter commands;
    • After running the command, Viw will return to Normal mode.
  4. Visual Mode

    • For navigation and manipulation of text selections;
    • The movement commands will extend a highlighted area of text;
    • Non-movement commands will be executed on the highlighted text;
    • Press v to enter Visual mode from Normal mode;
    • -- VISUAL -- is shown at the bottom of the screen.
  5. Insert Normal Mode

    • When in the Insert mode, press Ctrl + o to enter Insert Normal mode;
    • Similar to Normal mode, but after running one command, Vim will return to Insert mode;
    • -- (insert) -- is shown at the bottom of the screen.

A good habit: “whenever you’re not typing, it’s better to get back to Normal mode (press on \).”

2.2 Operators/Commands

Command Types Description
Ex commands Run as :{cmd}, e.g. :help.
Mapped commands Map or bind some complex commands to some keys for convenience.
Editing commands Used in Normal and Insert Mode, e.g. d4w for deleting 4 words after the cursor.

2.3 Buffer

We use ‘Current Buffer‘ to refer to the current opened file in Vim.

  • The buffer is a piece of memory that is been loaded with the content of the file.

3. Basic File Operations

3.1 Opening Files

1.Open from terminal

  • Type vim and the file name
1
$ vim demo.txt

2.Open from inside Vim

  • After starting Vim by vim;
  • Run the command :e <filename> to open a file;
1
2
3
$ vim

:e demo.txt

3.Open and copy file to the current file

  • read command: :r <filename>
  • Insert content into the current buffer
Command Description
:r demo.txt Insert the file demo.txt below the cursor in the current buffer.
:0r demo.txt Insert the file demo.txt before the cursor in the current buffer.
:r!sed -n 2,8p demo,txt Insert lines 2 to 8 from demo.txt below the cursor.
:r !ls Insert a directory listing below the cursor. (Works only in Linux or MacOS)

3.2 Closing Files

Command Description
:wq Save current opened file and exit Vim (Do saving even if no change is made)
:x Save current opened file and exit Vim, but write only change is made
ZZ Same as :x
q! Exit vim without saving current opened file
:qa Exit all opened files in current session

3.3 Saving Files

Command Description
:w Save current opened file
:w filename Save current opened file as filename
:w! filename Save file as filename with overwrite option
:sav filename Save current buffer as a new file filename
:up[date] filename Similar to :w, but only save when change is made

4. Navigation

4.1 Basic Movement

Alternative to arrow keys ($\uparrow \downarrow \leftarrow \rightarrow$)
Using h, j, k, l to move around on the QWERTY keyboard

  • h: left ($\leftarrow$)
  • j: down ($\downarrow$)
  • k: up ($\uparrow$)
  • l: right ($\rightarrow$)

Easy to remember:
Far left h for left, far right l for right, j looks like a down arrow $\downarrow$, last k for up arrow.

It keep user’s hands and fingers around the home row on the keyboard, which is more efficient.

4.2 Navigate Through Words

Key Description
w Go to the start of the next word
W (shift-W) Go to the start of the next WORD
e Go to the end of the current word
E (shift-E) Go to the end of the current WORD
b Go to the start(before) of the current word
B (shift-B) Go to the start(before) of the current WORD

Where

  • word is delimited by non-keyword characters;
    • ends at a non-word character, such as a ., - ).
  • WORD consists of a sequence of non-blank characters.
    • delimited by white space.

For sentence: I "am" learning vim-editor now!

  • 5 WORDs: I, "am", learning, vim-editor, now!
  • 10 words: I, ", am, ", learning, vim, -, editor, now, !

4.3 Scrolling Pages

Shortcut Description
Ctrl-d Scroll down half page
Ctrl-u Scroll up half page
Ctrl-f Scroll down full one page (forwards)
Ctrl-b Scroll up back one page (backwards, beginning)

4.4 Jumping Around the File

Command Description
gg Go to the top of the file
G Go to the bottom of the file
{ Go to the beginning of the current paragraph
} Go to the end of the current paragraph
% Go to the matching pair for (), [], {}
50% Go to the line at the 50% of the file
:NUM Go the line NUM, e.g. :10 jump to the line 10

4.5 Navigating Inside the Windows

Key Description
H (shift-H) Go to the first line (highest) of the current window
L (shift-L) Go to the last line (lowest) of the current window
M (shift-M) Go to the middle line of the current window

References

Graphical vi-vim Cheat Sheet and Tutorial - www.viemu.com

“Interactive Vim tutorial,” www.openvim.com. https://www.openvim.com/ (accessed Jul. 07, 2023).

J. Ilic, Mastering Vim quickly : from WTF to OMG in no time. Berlin: Jovica Ilic, 2018.

‌“welcome home : vim online,” www.vim.org. https://www.vim.org/


Outro

尾巴
会继续更新Vim的学习。
最后,希望大家一起交流,分享,指出问题,谢谢!


原创文章,转载请标明出处
Made by Mike_Zhang




感谢你的支持 | Thank you for supporting

Vim Learning 1 - Introduction, File Operations & Navigation
https://ultrafish.io/post/vim-learning-1/
Author
Mike_Zhang
Posted on
July 7, 2023
Licensed under