2 September 2019
Vim(2)
by Jerry Zhang
LeetCode Day 34: P206. Reverse Linked List (Easy)
题目
反转一个单向链表。
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
我的思路
依次修改箭头方向。
答案
public class E_206_ReverseLinkedList {
public ListNode reverseList(ListNode head) {
ListNode current = head;
ListNode pre = null;
while(current != null) {
current = current.next;
head.next = pre;
pre = head;
head = current;
}
return pre;
}
}
Vim CRUD
Insert
a/i/o, A/I/O
Delete in normal mode
- x: delete one charater
- dw: delete a word, default is “daw”, also delete all spaces after the word.
- diw: delete inner word
- dd: delete a line
- dt): delete to “)”
- d$: delete to end
- d0: delete to start
- 2dd: delete two lines
- 4x: delete four characters
- u: undo
- Ctrl + r: undo undo
Change
r(replace), c(change), s(substitute)
- r: just replace a character
- R: Replace multiple characters.
- s: substitute and go to the insert mode.
- S: delete the whole line and go to insert mode.
- 4s: delete the next four characters and go to insert mode.
- cw: change the word. caw: change a word
- C: delete the whole line and insert
- ct*: change to something.
Search
- / or ? search forward or backwards
- n/N jump to next or last match
- * or # search for the same word as current word.
- :set hls highlight words (not working on my computer)
- :set incsearch increment search.
Search and Replace
- substitute
:[range]s[ubstitute]/{pattern}/{string}/[flags]
10, 20 means 10-20 lines; % means all
- g global
- c confirm
- n number: search for match number
For example:
:% s/abc/def/g
Substitute all “abc” to “def”.
:1,3 s/abc/def/g
Substitute all “abc” to “def” between line 1 and line 3 include.
:1,5 s/abc//n
Counting number of a certain word. Count how many “abc” from line 1 to line 5.
(not working in the vscode vim, but it works in the linux vi)
- Substitute with Regex
:% s/\<abc\>/def/g
Only substitute the word “abc” without anything around.