Byte Order Mark  • • •  死在沙滩上       all posts in Archive

Vim Tips

This is a memory notes, will be continuous updating …

1. Use Registers

Unlike a single clipboard for Operating System. Vim provide multiple Registers for delete, yank and put commands.

Registers are named with character, name can be 0-1 or a-z. And we can use OS System Register, 「 + 」 for Linux and MacOS, 「 * 」 for windows.

1.1 Normal Register

"ayy ( copy current line into register a )
"Ayy ( append to register a by using capital letter ) 
"ay3y ( yank 3 lines to register a )
v"ay ( v: enter visual mode and select lines )
<C-v>"ay ( ctrl-v: enter visual mode and select selection )

"ap ( paste from register a )
<C-r>0 ( Insert Mode, paste from register 0 )

:reg "0 ( display register content )

1.2 System Clipboard

gg"+yG  (gg: go to first character, G: go to the end of the file )
:%y+ ( %: refer to work on all lines )
:%w !pbcopy ( copy the whole file in MacOS )

"+p ( paste from system register, windows should replace + with * )
:r !pbpaste ( past from the clipboard in MacOS )

BTW, copy whole file, no need use Vim, there are alot of ways … cat {file} | pbcopy

2. Use Buffers

Buffers in Vim is an in-memory representation of files.

List All Buffers

 :buffers ( list all buffers )
 :ls ( list all buffers )
 :b3 ( switch buffer with numbers )
 :bnext ( swith to next buffer )
 :bdelete 3 ( delete one buffer with number )
 :2,5 bdelete ( delete buffers from 2 to 5 )

We can use: MiniBufExpl

MiniBufExpl

While listing all buffers, Vim using # to indicate current buffers, with MiniBufExpl we can see all buffers and current buffer at the top.

3. Use Windows

Vim always started with one single window, we can divide it horizaontally or vertically to multiple small windows.

<C-w>s ( split horizaontally )
:split ( split horizaontally )

<C-w>v ( split vertically )
:vsplit ( split vertically )

<C-w>w ( switch in cycle between open windows )
<C-w>h ( focus the window to the left )
<C-w>j ( focus the window below )
<C-w>k ( focus the window above )
<C-w>l ( focus the window to the right )

:qa ( quit all windows )
:qa! ( quit all windows without saving )
:set hlsearch ( enable high light search result )
:set nohlsearch ( disable high light search result )
:set ignorecase ( case insensitive )

/search-text ( search search-text forward from current position )
?search-text ( search search-text backward from current position )
* ( search the word under the cursor )

/text1\|text2 ( search text1 or text2, the pipe has to be escaped )
/^text1.*text2.*text2/ ( search text1 and text2 and text3, which text1 is the start of line )

/\v"[^"]+" ( search any string inside quotes )

n ( after get searched results, use n to jump to next match )
N ( after get searched results, use n to jump to previous match )
<C-o> ( jump backward in the range of jump locations: that have checked with n/N )
<C-i> ( jump forward in the range of jump locations: that have checked with n/N )

:%s/search-text1/replace-with-text2/  ( search whole file and replace )
:%s/search-text1/replace-with-text2/c ( search whole file and replace, confirm each replace )

More for substitute: Vim Substitute and Global

5. Delete Lines with Pattern

:g/pattern/d  ( delete all lines that contain a pattern )
:g!/pattern/d  ( delete all lines that do not contain a pattern )

The :global command allows us to run an Ex command on each line that matches a particular pattern.

6. Save to Nonexistent Directories

:!mkdir -p %:h
  • The -p flag is mkdir’s parameter, it means to create intermediate directories.
  • :h means Head of the file name (the last component and any separators removed). In this line, :h will removes the filename while preserving the rest of the path

7. Save as the Root User

When we forgot to open file with sudo command, no need re-edit file again.

:w !sudo tee % > /dev/null
  • The symbol % has special meaning on Vim’s command line, it expands to represent the path of the current buffer.
  • The tee command copies standard input to standard output, making a copy in zero or more files. The output is unbuffered.

8. HTML Releated

di<  ( delete characters inside, could change d to yank, change too )
vit ( select character of the tag, could change v to delete, yank too )