Vim

2021-08-22

  • 1 Basics
    • 1.1 Edit
      • 1.1.1 Join Two Lines
      • 1.1.2 Global Command :g
    • 1.2 Folding
      • 1.2.1 Basic shortcuts
  • 2 Advanced
    • 2.1 Edit
      • 2.1.1 Fix syntax highlighting
      • 2.1.2 Replace Tab with Whitespaces
      • 2.1.3 Replace string within a range of lines
      • 2.1.4 Replace string matching the whole word
      • 2.1.5 Paste a column of text after a different column of text
      • 2.1.6 Paste in Insert Mode
      • 2.1.7 Record commands and replay
      • 2.1.8 Remove ^[ and all of the escape sequences
      • 2.1.9 Append the Output of an Shell Command
    • 2.2 Configuration
      • 2.2.1 Slow when editing Chinese
      • 2.2.2 Convert DOS line endings to Linux line endings in vim
      • 2.2.3 Encoding
      • 2.2.4 Monitor Real Time Changes
      • 2.2.5 Disable bell sound
      • 2.2.6 Match angle bracket <>
    • 2.3 System and application
      • 2.3.1 Find which plugin making Vim slow
      • 2.3.2 Saving File Under Dropbox Sync Directory Causing Long Waiting Time
      • 2.3.3 Windows: gvim background color not initialized since 8.2

1 Basics

1.1 Edit

1.1.1 Join Two Lines

Press the uppercase J.

1.1.2 Global Command :g

Delete all lines matching a pattern.

:g/pattern/d

Delete all lines that do not match a pattern.

:g!/pattern/d
:v/pattern/d

Delete all blank lines (^ is start of line; \s* is zero or more whitespace characters; $ is end of line).

:g/^\s*$/d

1.2 Folding

1.2.1 Basic shortcuts

  • zo: open the fold under the current cursor
  • zO: Open the folds throughout the whole buffer
  • zc: close the fold under the current cursor
  • zC: Close the folds throughout the whole buffer
  • za: toggle the folding under the current cursor
  • zA: Toggle the folding throughout the whole buffer
  • zr: reduce the folding by opening one more level of folds throughout the whole buffer
  • zR: Reduce the folding by opening all levels of folds throughout the whole buffer
  • zm: give more folding by closing one more level of folds throughout the whole buffer
  • zM: Give more folding by closing all levels of folds throughout the whole buffer

2 Advanced

2.1 Edit

2.1.1 Fix syntax highlighting

Sometimes the syntax highlighting is broken. Use :syntax sync fromstart to fix it.

2.1.2 Replace Tab with Whitespaces

Use :retab.

2.1.3 Replace string within a range of lines

:376,549s/\\<abc\\>/edf/g

2.1.4 Replace string matching the whole word

Normally do as follows,

:%s/\\<abs\\>/bng/g

However, typing \\< \\> each time is tedious. Since my vimrc file highlights the word where the cursor locates, I can directly run the following command when the cursor is at abs.

:%s//bng/g

2.1.5 Paste a column of text after a different column of text

For example, I have

abc
def
ghi

and

123
456
789

and I want

123 abc
456 def
789 ghi

The solution is: Use visual block (ctrl-v) to cut the letter column. Then move to the first line of the number column. Move to the end and make one space. Then paste the letter column.

2.1.6 Paste in Insert Mode

CTRL-R {register} pastes things in the insert mode. For example, CTRL-R * will insert in the contents of the clipboard and CTRL-R " (the unnamed register) inserts the the last delete or yank.

2.1.7 Record commands and replay

  1. Enter the recording mode: press q in the normal mode
    • Store the commands in the named register a: press qa.
  2. Do something
  3. Stop the recording: press q.
  4. Replay: [email protected]

2.1.8 Remove ^[ and all of the escape sequences

The escape sequences are ASCII escape sequences which could be used to render colored text in the terminal.

Using keyboard entry:

sed 's/[Ctrl-v][Esc]//g'

alternatively

sed 's/[Ctrl-v][Ctrl-[]//g'

where [Ctrl-v] is the key Ctrl + key v.

2.1.9 Append the Output of an Shell Command

Use :read. For example, :read !pwd.

2.2 Configuration

2.2.1 Slow when editing Chinese

Enable spellcheck for Chinese

set spell spelllang=en_us,cjk

2.2.2 Convert DOS line endings to Linux line endings in vim

set ff=unix

2.2.3 Encoding

set encoding=utf-8  " The encoding displayed.
set fileencoding=utf-8  " The encoding written to file.

2.2.4 Monitor Real Time Changes

set autoread

2.2.5 Disable bell sound

set belloff=all

2.2.6 Match angle bracket <>

:set matchpairs+=<:>

Reference

2.3 System and application

2.3.1 Find which plugin making Vim slow

vim --startuptime timeCost.txt timeCost.txt

2.3.2 Saving File Under Dropbox Sync Directory Causing Long Waiting Time

This behavior occurs on my work machine, an old version of debian. I cannot upgrade the system. Therefore I googled and found the solution.

It is caused by the backupcopy feature of Vim. The simple solution is

set backupcopy=yes

Reference: link.

2.3.3 Windows: gvim background color not initialized since 8.2

vim 8.2 includes a default _gvimrc that has a “set nice colors” (hah) block of highlight commands in it. Create a blank .gvimrc in the home directory, and that fixes the problem. I’m installing vim with scoop in case that matters.

.
Created on 2021-08-22 with pandoc