hvn-network

My Blog List

Sunday, September 18, 2016

Vim Editor


I. ABOUT VIM

 

1. Install Vim:

  • sudo apt-get install vim

2. Open Vim:

  • vim
  • vim [path]

3. Save a file:

  • :w

4. Closing a file:

  • :q

5. Type of Vim modes:

Normal
The Vim editor starts in this mode where you can execute all editor commands
Insert
This mode is inserting text
Command line
This mode is for executing commands at the bottom of the editor

6. Advanced modes

Visual
You can select text in visual mode and execute Vim command
Ex
After entering a command, you remain in command line mode

7. Moving in Vim

j or
Down one line
k or
Up one line
h or
Move right one character
I or
Move left one character

8. Vim configuration file (vimrc)

- Local vimrc: ~/.vimrc
- Golbal vimrc: $VIM/.vimrc
  set nu
  set syntax=on
  set history=1000
 
  syntax enable
  set background=dark
  colorscheme solarized
- For customize your vimrc, refer https://dougblack.io/words/a-good-vimrc.html

9. Show version of vim

  • :version

10. About Vim help

  • :help

11. Vim tutor:

  • $vimtutor viet

II. BASIC NAVIGATION

1. Scroll:

CTRL-F
Scroll Down
CTRL-B
Scroll Up
CTRL-D
Scroll Down half page
CTRL-U
Scroll up half page

2. Word navigation

  • word consists of a sequence of letters, digits and underscores.
  • WORD consists of non-blank characters, separated with white space.
  • 192.168.1.3 contains 7 words, but is considered as 1 WORD
w/W
go to beginning of next word/WORD
e/E
go to the end of current word/WORD
b/B
go to the beginning of previous word/WORD

3. Position cursor at specific location within a line

0
Go to the starting of current line
$
Go to the end of current line
^
Go to the first non blank character of current line
g_
Go to the last non blank character of current line

4. Navigate to Nth character, Nth percentage of a file, jump to Nth line

50%
Go to the 50th percentage of file
100A
Navigation key is: 100 followed by A
:50
Jump to 50th line

5. Navigation with long line

gj
Scroll down a visual line
gk
Scroll up a visual line
g^
Go to the starting of current visual line
g$
Go to the end of current visual line
gm
Go to the middle of current visual line

6. Create local bookmarks inside file

  • There are two types of bookmarks - Local bookmarks and Global bookmarks
  • For local bookmark: m{mark-name}
ma
Bookmark the current cursor with name 'a'
`a
Jump to exact cursor of bookmark 'a'
'a
Jump to the beginning of line containing the bookmark
  • For global bookmark: use capital character (B for example) when open multiple files in Vim
  • Display all bookmarks: :marks
  • Display a specific bookmarks: marks {mark-name}

III. TEXT NAVIGATION

1. Basic Text Manipulation

Insert or Append Text
  • Insert
i
Insert text at the current position
I
Insert text at the beginning of the line
o
Insert a new line after the current line
O
Insert a new line before the current line
:r
Insert another file content into current file after the current line
  • Append
a
Append text after the current cursor position
A
Append text to the end of the line

Replace text
r
Replace a single character with a single character
R
Replace characters until <ESC> is pressed

Substitute text
s
Substitute the current character
S
Substitute the current line with new text

Join two lines: J

Copy
y<char navigation keys>
copy a single character
yw
copy a single word
yy
copy a single line
y<mark name>
copy up to a bookmarked line
y`<mark name>
copy up to a bookmarked position

Paste
p
Paste after the current cursor location
P (capital)
Paste before the current cursor location

Delete
  • Delete is similar to copy, however use 'd' instead of 'y'
  • x, dw,…

2. Visual modes

v
Normal visual mode. Use arrow key to navigate and select text
V
Line visual mode
CTRL-V
Visual block mode
d
Delete only the highlighted text
D
Delete rows which have highlighted text
y
Copy only highlighted text
Y
Copy rows which have highlighted text
c
Delete highlighted text and go to insert mode
C
Delete rows which have highlighted text and go to insert mode

3. Advanced Text manipulation


a) Using buffer
  • Use valid named buffer: a to z
"ayy
Copy current line to buffer a
"a5yy
Copy 5 line to buffer "a"
"ap
Paste copied lines from buffer "a" after the cursor
"aP
Paste copied lines from buffer "a" before the cursor

b) Using macro
  • Follow these steps:
    • Start recording by pressing q
    • Perform any typical editing actions inside Vim
    • Stop recording by pressing q
    • Play the recorded macro by pressing @ followed by macro name

 IV. MISCELLANEOUS

1. Sort file content

:sort
Sort in ascending order
:sort!
Sort in descending order
:sort i
Ignore case while sorting
:sort u
Remove duplicated lines

2. Vim as a programmers Editor 

Highlight code on/off:
  • :syn on/off
Increment/Decrement Number
  • Place the cursor over a number
CTRL-A
Increment number
CTRL-X
Decrement number
Temporarily execute command mode in Insert mode: CTRL-O

3. Vim Look and Feel, Tabs, and Windows

 a) Split the current window:
  • Vertical: :vs
  • Horizontal: :split

b) Navigate between windows:
  • CTRL-W {arrow keys or h, j, k, l}

c) Resize the windows
CTRL-W +
Increase the size of current window
CTRL-W -
Decrease the size of current window
CTRL-W >
Expand the column width
CTRL-W <
Reduce the column width

d) Navigate Source Code using Ctags
  • Install Ctags: sudo apt-get install exuberant-ctags
  • On the folder of C files: ctags *.c
  • Usage 1: Navigate to particular function: :ta {function_name}
    • :ta main
  • Usage 2: Navigate to function call: CTRL-]
  • usage 3: Return back to the caller from the definition: CTRL-T
  • Usage 4: Navigate through a list of functions which have similar names


e) Convert Vim Editor to Source Code Browser

4. Edit multiple files in Tabs

  • Using command: vim -p file1 file2 file3
:tabedit FILENAME
Open another file in a new tab under current vim session
:tabs
List all open tabs
:tabn N
Go to Nth tab
:tabclose
Close the current tab
:tabdo CMD
Execute a command in all tabs
:tabn
Go to the next tab
:tabp
Go to the previous tab

5. Undo/redo/repeat

Single undo: u
Undo all: U
Redo: CTRL-R or :red
 Repeat the lastest action: . (dot)

6. Edit Multiple Files using the Traditional method

  • using command: vim file1 file2 file3
  • Open another file: :e another_file
  • List all opened files: :ls
  • Go to the Nth file: :e #N
  • Toggle between two files: CTRL-^
  • Move to next/previous file: :next and :previous
  • Quit all: :qa

7. Review Differences between Files

  • Use command: vimdiff file1 file2
  • See changes in one file: : changes

8. Searching

Navigation by search:
/
Search forward
?
Search backward
n
Go to the next occurrence
N
Go to the previous occurrence
// or ??
Repeat previous forward or reverse search

9. Find and replace

  • Syntax: :[range]s/{pattern}/{string}/[flags] [count]
  • For [flags]:
    • [c] - confirm each substitution
    • [g] Replace all occurrences in the lines
    • [i] Ignore case for the pattern
  • Example 1: :%s/old-text/new-text/g
    • This command is for replace all old-text appeared on file to new-text
    • % specifies all lines
    • g specifies all occurrences in a line
  • Example 2: :s/helo/Hello/gi
    • Lack of [range] means do substitution on the current line only
    • I flag means specifying insensitive searching
  • Example 3: :1,10s/I/We/g
    • Only search in range of line 1 to line 10
  • Example 4: :'<,'>s/hello/Hello/g
    • '<,'> represents to visually selecting specific lines of visual mode.
  • Example 5: :s/helo/hello/g 4
    • Search range from the lines have cursor to the next 4 lines
  • Example 6: :s/\<his\>/her/
    • Substitute only whole words and not partial matches
  • Example 7: :%s/\(good\|nice\)/awesome/g
    • Substitute either word1 or word2 with a new word

10. Search across multiple files using vimgrep

  • Using command: :vimgrep <key> *
  • cn  - Jump to the next match
  • :cN - Jump to the previous match
  • :clist - View all the files that matched the vimgrep search keyword without jumping to the individual files
  • :cc number - Jump to the specific search number.

11. Using :match

  • to display all instances of a particular keyword in a certain color scheme.
  • Example: :match ErrorMsg /Error/
    • Highlight the keyword Error in red
  • There are few predefined color schemes available in Vim:
    • ErrorMsg
    • WarningMsg
    • ModelMsg
    • MoreMsg
 
References:
1. Vim 101 Hacks - Practical examples for becoming fast and productive in Vim editor

No comments:

Post a Comment