Skip to content

vbeaucha/simple-vim-explain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

VIM — A New Paradigm Opens Up to You

How We Edit Text Today

The current standard for editing text is to use a graphical tool (Sublime Text, gedit, Word).

You press a key, text appears, Backspace deletes a character, and you move around either with the arrow keys or with a mouse click.

Nothing groundbreaking so far.

That's All Fine, But Why VIM?

Vim completely changes the paradigm and is bewildering to newcomers.

So why bother learning a new way to edit text?

Simple: you won't always have access to a graphical tool.

If you work on servers, you'll find yourself in the dark world of the terminal and the command line. Better to have a few weapons in hand when that day comes!

In Vim you have 2 modes (3 in reality, but we'll come back to that):

  • EDIT [ESC] : I manipulate existing text
  • INSERT [i] : I type text (nothing new under the sun)

This is exactly where Vim shines: its edit mode!

The Edit Mode Survival Kit

Moving Around

It would be too easy to use the arrow keys! Instead, we use:

- ↑ k : up
- ↓ j : down
- ← h : left
- → l : right
- → e : end of word
- → E : next whitespace / end of line
- ← b : beginning of word
- ← B : previous whitespace / beginning of line

- :[NUM] : go to line [NUM]

Modifying an Element

There are two main commands for modifying existing text:

- d : delete -> stays in edit mode
- c : change -> switches to insert mode

Having these commands is great, but how do you use them? With a context or an object!

Contexts:

 - i : inside
 - t : till

Objects:

- w : word
- p : paragraph
- [KEY] : any arbitrary character

A few examples:

 - di{ : deletes everything inside the next {}
 - cw  : change the current word
 - d[KEY1][KEY2] : delete from KEY1 up to KEY2

In Practice

Let's say you want to modify this code:

function main(){
	// main prints hello world
	console.log("hello world")
}

Place your cursor here:

function main(){
	// main prints hello world
->>	console.log("hello world")
}

A quick di{ or di} and — magic:

function main(){
}

I Want to Go Faster!

Still here? In that case, here is a list of commands that will let you switch between edit and insert mode and speed up your work in Vim:

- o          : insert a new line below the cursor
- G          : go to the end of the file
- $          : go to the end of the line
- { or }     : jump to the beginning or end of a paragraph (text block with no blank line)
- CTRL+(u/d) : scroll half a screen height (up, down) d’une demie hauteur d’écran (up, down)

I made a mistake! → no worries:

- u      : undo (equivalent to CTRL+Z)
- CTRL+r : redo

I'm fed up, delete everything!

- cc / dd    : delete an entire line (with or without switching to INSERT mode)
- d[number]d : delete [number] lines

Copy and paste:

- y : yank/copy (same usage as c/d commands)
- p : paste     (same usage as c/d commands)

I Want Out of This Hell!

On the internet you can find an incredible number of methods for exiting Vim.

For example: unplug your computer → effective but impractical.

Here is the proper method!

- :w  : write (i.e., save)
- :q  : quit (finally!)
- :wq : save and quit
- :q! : quit and discard all changes

The 3rd Mode of Vim — Visual Mode

As mentioned earlier, there is a 3rd mode in Vim: Visual mode [v], which lets you select text as you move the cursor.

There are actually three variants of Visual mode:

- v      : character-wise visual mode — select text character by character
- V      : line-wise visual mode     — select entire lines at once
- CTRL+v : block visual mode         — select a rectangular block of text (column editing)

Once you have a selection, you can apply commands to it:

- y : yank (copy) the selection
- d : delete the selection
- c : change the selection (deletes it and enters insert mode)
- > : indent the selection
- < : un-indent the selection

In Practice

Imagine you want to add // at the beginning of several lines to comment them out.

Place your cursor on the first line, press CTRL+v to enter block mode, then move down with j to select the lines, press I (capital i) to insert at the beginning of the block, type //, and press ESC — done!

// const a = 1
// const b = 2
// const c = 3

Searching in Edit Mode

You can search through text using several commands:

- f[key] : jump to the first occurrence of [key] on the current line
- ;      : next occurrence
- ,      : previous occurrence

- /[foo] : search for a pattern across the whole file
- n      : next match
- N      : previous match

Replace — Substitution

Vim has a powerful substitution command that works like a find-and-replace:

:s/old/new/        : replace the first occurrence of "old" with "new" on the current line
:s/old/new/g       : replace all occurrences on the current line
:%s/old/new/g      : replace all occurrences in the entire file
:%s/old/new/gc     : replace all occurrences in the file, asking for confirmation each time

The % means "the whole file", and the g flag means "global" (all occurrences on each line, not just the first).

In Practice

You have a file that uses foo everywhere and you want to rename it to bar:

:%s/foo/bar/gc

Vim will highlight each match and ask: replace with bar (y/n/a/q/l)?

  • y → yes, replace this one
  • n → no, skip this one
  • a → all, replace all remaining without asking
  • q → quit substitution

Marks — Bookmarking Your Position

Marks let you bookmark a position in a file and jump back to it instantly.

- m[a-z]  : set a mark at the current cursor position (e.g., ma, mb, mc…)
- `[a-z]  : jump to the exact line and column of mark [a-z]
- '[a-z]  : jump to the beginning of the line of mark [a-z]
- ``      : jump back to the position before your last jump
- ''      : jump back to the line before your last jump

In Practice

You're deep inside a long file editing one function, but you need to quickly check another function far below.

  1. Press ma to mark your current position.
  2. Navigate to the other function and make your changes.
  3. Press `a to teleport straight back to where you were.

Macros — Automating Repetitive Tasks

Macros let you record a sequence of keystrokes and replay them as many times as you like — perfect for repetitive edits.

- q[a-z]  : start recording a macro into register [a-z] (e.g., qa)
- q        : stop recording (press q again when done)
- @[a-z]  : replay the macro stored in register [a-z]
- @@       : replay the last macro again
- [N]@[a-z] : replay the macro [N] times (e.g., 10@a)

In Practice

You have a list of plain words and you want to wrap each one in quotes:

apple
banana
cherry
  1. Place your cursor on apple.
  2. Press qa to start recording into register a.
  3. Press I to insert at the beginning, type ", press ESC.
  4. Press A to append at the end, type ", press ESC.
  5. Press j to move to the next line.
  6. Press q to stop recording.
  7. Press 2@a to replay the macro on the next 2 lines.

Result:

"apple"
"banana"
"cherry"

Splits and Buffers — Working with Multiple Files

Vim lets you split your screen to view and edit multiple files at the same time.

Opening Splits

- :split [file]  (or :sp)  : open a horizontal split
- :vsplit [file] (or :vsp) : open a vertical split
- :new                     : open a new empty horizontal split
- :vnew                    : open a new empty vertical split

If you don't provide a filename, the split opens with the current file.

Navigating Between Splits

- CTRL+w w     : cycle to the next split
- CTRL+w h/j/k/l : move left / down / up / right between splits
- CTRL+w =     : make all splits equal size
- CTRL+w +/-   : increase or decrease the height of the current split

Buffers

Every file you open in Vim is loaded into a buffer. You can have many buffers open even without splits.

- :ls (or :buffers) : list all open buffers
- :b [N]            : switch to buffer number [N]
- :b [name]         : switch to a buffer by partial filename
- :bn               : go to the next buffer
- :bp               : go to the previous buffer
- :bd               : close (delete) the current buffer

In Practice

Open two files side by side to compare them:

:vsplit other_file.js

Then use CTRL+w l and CTRL+w h to jump between the left and right panes.

Customize Your Vim

Vim supports a huge range of plugins and settings through its .vimrc configuration file.

Here are some useful articles and plugins to explore:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors