Mac Vim Usage – Basics

Vim articles

My vim related articles

Environment

  • System Version: macOS 10.13 (17A405)
  • Kernel Version: Darwin 17.0.0

Vim installation

For how to install vim on Mac, you can refer to Install vim on macOS High Sierra using source code.

Version

Check which vim is being used

~/Documents$ which vim

/usr/bin/vim

Check vim version

~/Documents$ vim --version

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jul 26 2017 19:10:24)

Included patches: 1-503, 505-642

Compiled by root@apple.com

Vim Modes

  • Normal
  • Insert
  • Visual
  • Command

Text operations

Insertion

i Enter insert mode (before current cursor)

I Enter insert mode (from line beginning)

a Enter insert mode (append after cursor)

A Enter insert mode (append from line end)

Deletion

x Delete character before cursor

X Delete character after cursor

dw Delete word

diw Delete current word

dd Delete line

Joint line

J Joint line.

Save and quit

Press ESC to exit from insert mode

:q! Quit without saving

:w Save only

:q Quit

:wq Save and quit

Cursor movment

w Move cursor forward by a word

b Move cursor backward by a word

0 Move cursor to line beginning

$ Move cursor to line end

gg Move cursor to file start

G Move cursor to file end

Copy and pase

To cut-and-paste or copy-and-paste inside Vim

  1. Position the cursor at the beginning of the text you want to cut/copy.
  2. Press v to begin character-based visual selection, or V to select whole lines, or Ctrl-v or Ctrl-q to select a block.
  3. Move the cursor to the end of the text to be cut/copied. While selecting text, you can perform searches and other advanced movement.
  4. Press d (delete) to cut, or y (yank) to copy.
  5. Move the cursor to the desired paste location.
  6. Press p to paste after the cursor, or P to paste before.

Visual selection (steps 1-3) can be performed using a mouse.

If you want to change the selected text, press c instead of d or y in step 4. In a visual selection, pressing performs a change by deleting the selected text and entering insert mode so you can type the new text.

Some quick commands

yiw Copy a word

yy Copy a line

p Paste before current cursor position.

P Paste after current cursor position.

Undo and redo

u Undo last operation

Ctrl+r Redo the last undone operation.

Search and replace

E.g. we are going to search a keyword “arophix”

Search

In normal mode, type/ to enter your text to be searched. Type<CR> to perform a search.

/arophix search keyword “arophix”

n search next occurrence of keyword “arophix”

N search previous occurrence of keyword “arophix”

:set ignorecase set search mode to be case insensitive

:set noignorecase set search mode to be case sensitive

Replace

E.g. we are going to replace “arophix” with “Arophix”

Firstly, set search mode be case sensitive.

:set noignorecase

Secondly, use the:substitute command. In short, substitute command can be :s simply.

:%s/arophix/Arophix/gc search keyword “arophix” in all lines (%) and replace with “Arophix”, but ask for confirmation(/gc).

Open a directory

E.g.  open /User/<myusername>/Download

:e /User/<myusername>/download

Go back to previous editor

Ctrl + ^

Open current directory of the opening file

:Ex

or

:e .

Vim help command

:help <command>

Reference

https://www.cs.oberlin.edu/~kuperman/help/vim/windows.html

 

Advertisement

3 thoughts on “Mac Vim Usage – Basics

  1. Pingback: Mac Vim Usage – Customization – Arophix

  2. Pingback: Install vim on macOS High Sierra using source code – Arophix

  3. Pingback: Mac Vim Usage – Advanced – Arophix

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s