cvwiki

Vim

Nov 5, 2022

# Why Vim?

Learning to use Vim is worth it for several reasons. First, Vim (or vi) is almost guaranteed to be installed on any remote server you may need to ssh into. Second, although Vim was released in 1991, it’s widely available as a plugin on modern IDEs, like Intellij, VS Code, Eclipse, etc. Third, once you’re comfortable with the controls, Vim allows you to quickly navigate to any place on the page with precision. Combining that with the ability to record chains of commands and replay them, it allows for programmatic editing of files on the fly. Once you’re comfortable with Vim, you have a text editor at your service that you can on virtually any server.

# Vim Commands

# Sort Commands

The following command sorts all lines and removes duplicate lines:

1
:sort u

https://stackoverflow.com/questions/26981192/how-do-i-remove-non-duplicate-lines-in-vim

# Search Commands

Search for lines containing this pattern - **: ? + - \*\*

Find and Replace: %s/foo/bar/g

The traditional approach to find lines not matching a pattern is using the :v command:

# Record Macro Commands

# Window Panes in Vim

# Regex for Number Searches

Regex syntax is a little crazy in Vim, but being able to record macros that search for certain patterns is very useful for extracting data.

Search numbers of fixed length say 5 (matches 12345, 123456). Numbers more than 5 digits contain substring of 5 digits, and are also found by this regex.

1
/\d\{5\}

word boundary start:

1
\<

word boundary end:

1
\>

Then use below to search numbers of exact 5 digits (match 12345 but not 123456):

1
/\<\d\{5\}\>

Use below to search numbers of 5 or more digits:

1
/\<\d\{5,\}\>

Use below to search numbers of 5 to 8 digits:

1
/\<\d\{5,8\}\>

Use below to search numbers of 8 or less digits:

1
/\<\d\{,8\}\>

Shortcut numbers of 1 or more digits

1
/\d\+

# .vimrc file

1
2
3
4
5
6
set nu
set relativenumber
set scrolloff=8
set mouse=a
set ttymouse=sgr
set clipboard=unnamed