Mac Vim Usage – Advanced

Vim articles

My vim related articles

Vim Registers

Vim include 18 registers

" 0 1 2 3 4 5 6 7 8 9 k - * . : % /

Check vim registers

:reg

E.g. after typing :reg, we have below information

:reg

--- Registers ---
""    THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE CONTROL^J
"0    THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE CONTROL^J
"1   ^J
"2   ^J
"3       public static void main(int i, String args[]){^J
"4   package com.shizhen.test;^J
"5   package com.shizhen.test;^J
"6   class Fruit{ ^J    public void printFruitName() {^J
"7   ^J
"8   import java.lang.String;^J
"9   abdccd edd d^J
"k   kkkkkkll
"-   #
"*   Enter insert mode (from line beginning)?| ^JI
".   Iyi
":   reg
"%   build.xml
"/   build-all-

Press ENTER or type command to continue

Unnamed register

""

This is Vim’s default register and can be accessed via above form. Essentially, when you try to paste the content from default register, below two forms will have the same effect.

p

and

""p

Register 0 to 9

Any text deleted or yanked will be stored into this default register. The latest yanked text will always be stored in 0 register. So, via below command, you will always have your latest yanked text to paste.

"0p

The content yanked earlier will be shifted to registers from 1 to 9 consequently when a later yank happens. In other words, you will have at most 10 yanked text maintained in registers from 0 to 9 with register 0 being the latest and 9 being the earliest.

Paste the content from an register, e.g. register 9

"%9

" means to refer to a register,% is the register to be referred to,9 is to paste the content in this register.

Read only registers

There are 4 read only registers: "."%": and "#

". The last inserted text.

"% The current file path relative to the directory where vim was first opened.

": The most recently executed command. In normal mode, simply type @: to repeat the last executed command.

"# The name of the alternate file.

Access a register from Vim command line

E.g search the text stored in register:

/Ctrl+r : <CR>
/ means starting to search,Ctrl+r is telling Vim the next input will be the register. : means we are accessing this register. Note thatCtrl+r :will not be visible when you type them. But after you type the register, the content stored in that register will appear in the command line.

 

Clipboard sharing between Vim and macOS

From Vim to macOS

Enter visual mode: v

You can visually select text and type

:w !pbcopy<CR>

Explanation

  • :w means “write to”
  • ! means following is a shell command.
  • pbcopy is the command to access system clipboard.

In a sentence, write current selected content to system clipboard. 

Alternative

Firstly, enter visual mode to select the text to be yanked.

:v

Then yank the selected text to system clipboard register + .

"+y <CR>

To select all the lines of file and copy to clipboard.

:%y+

or

gg"+yG

Explanation

  • % to refer the next command to work on all the lines
  • y to yank those lines
  • + to copy to the system clipboard

From macOS to Vim

Using Ctr+C to copy some text outside of Vim.

:r !pbpaste<CR>

Or Enter command mode and type “+ register, enter p to paste.

Explanation

  • :r means “replace the character under the cursor”
  • ! means following is a shell command.
  • pbpaste is the command to access system clipboard and paste its content.

In a sentence, paste the content of  system clipboard to current cursor postion. 

Alternative

"+p

Note that pbcopy and pbpaste are macOS specific commands, they won’t work with Ubuntu or Debian. 

Reference

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

https://www.brianstorti.com/vim-registers

Advertisement

3 thoughts on “Mac Vim Usage – Advanced

  1. Pingback: Mac Vim Usage – Customization – Arophix

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

  3. Pingback: Mac Vim Usage – Basics – 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 )

Twitter picture

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

Facebook photo

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

Connecting to %s