40

Vim in 3 minutes

 5 years ago
source link: https://www.tuicool.com/articles/hit/ZR32Abe
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

This article was inspired in Paul Gorman’s article with a very similar name (vim in 5 minutes) .

What is Vim?

uMFbUbM.png!web
  • Vim is a text editor.

How do I get Vim?

  • Depending on your operating system, either download and install it (windows), with apt (Linux) or brew (macOS).

How do I use Vim?

From the command line, type ‘vim’ or ‘vim {filename}’, or ‘gvim’ for the graphical version.

Vim is a modal text editor and it has 4 main modes:

  • Normal Mode : Vim starts in normal mode. It’s used to navigate through text and entering commands.

Commands are built the following way:

[operator] [count] [motions] where an operator indicates an action, count is the number of times to execute the action and motions the character or group of characters to apply.

For example:

y3w => yank ( copy ) the next 3 words.

d5j => delete 5 lines down.

In the end of this article we can find a list of operators and motions.

  • Insert mode : it is used to insert text and it is reached by entering ‘i’ while in Normal mode.

To go back to Normal mode enter ‘ESC’ ( this is valid for all modes).

  • Command mode : it is used to enter commands ( like save and exit ).

It is reached from Normal mode with ‘:{command}’ for commands or ‘/{string}’ for search or even ‘?{string}’ for backwards search.

The most common commands are:

:w => save

:wq => save and quit

:q! => quit without save

:e => reload from disk

/foo => search forward for ‘foo’

?foo => search backwards for ‘foo’

  • Visual mode : it is used to select and manipulate groups of characters.

It is reached from Normal mode with ‘v’. Normal motions change the highlighted area and normal operations will affect it.

Last but not least, Vim has some of the best help menus I’ve ever seen. To get help use:

:help

or :help {topic}

:vimtutor will launch a quick, hands-on Vim tutorial.

rM7zumm.png!web
Vimtutor example

Summing up:

All you really need to know to start using Vim is to open a file with ‘vim {filename}’. Enter ‘i’ to start adding text. Enter ‘ESC’ when you’re done adding text. Finally, enter ‘:wq’ to save and quit, or ‘:q!’ to quit without saving .

Of course, Vim is much more than this. This article was the result of trying to sum up a quick way to introduce the bare essentials to Vim. For more detailed information check vimtutor or one of this two books:

Azeymqf.png!web
This book will take you from Vim installation to use it as an IDE.

Extra:

As promised, here is a list of Operators and motions that can be used on Vim.

Operations

d      Delete
dd     Delete line
y      Yank
yy     Yank line
c      Change
u      Undo
Ctl-r  Redo
r      Replace character
.      Repeat last operation

Motions

h      Left (like left arrow)
j Down (like down arrow)
k Up (like up arrow)
l Right (like right arrow)
w Work forward/right
b Work back/left
W Whitespace work forward
B Whitespace work back
0 Start of line
$ End of line
^ Non-whitespace start of line
Ctl-f Screen forward
Ctl-b Screen back
gg Start of file
G End of file
( Sentence back
) Sentence forward
{ Paragraph back
} Paragraph forward
% Jump to matching brace
23G Jumpt to line 23
`. Jump to last change
* Jump to next instance of word under cursor
# Jump to previous instance of word under cursor
fx Next character x on this line
Fx Previous character x on this line
; Repeat fx or Fx
uiiIRnB.png!web

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK