Vim: Difference between revisions
Jump to navigation
Jump to search
(27 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
Useful commands. | Useful commands. | ||
=registries= | |||
List registers. | |||
:reg | |||
Paste register 5 | |||
"5p | |||
==spell check== | |||
Enable spell check. Suggestions. Move between words. Stop spell check. | |||
:set spell spelllang=en,sv | |||
z= | |||
]s and [s | |||
:set nospell | |||
==Switch to last file== | ==Switch to last file== | ||
:e# | :e# | ||
==vim shortcuts== | |||
Shortcut Description | |||
ESC Switch to command mode. | |||
i Insert before cursor. | |||
a Insert after cursor. | |||
I Insert at the beginning of line. | |||
A Insert at the end of line. | |||
C Change text to the end of line (equivalent to c$). | |||
cc or S Change current line (equivalent to 0c$). | |||
s Delete a single character under the cursor and enter input | |||
r Replaces a single character under the cursor (without leaving command mode). | |||
R Replaces characters under cursor. | |||
v Edit (and execute) the current command in the text editor. | |||
h Move one character right. | |||
l Move one character left. | |||
w Move one word or token right. | |||
b Move one word or token left. | |||
W Move one non-blank word right. | |||
B Move one non-blank word left. | |||
e Move to the end of the current word. | |||
E Move to the end of the current non-blank word. | |||
0 Move to the beginning of line | |||
^ Move to the first non-blank character of line. | |||
$ Move to the end of line. | |||
% Move to the corresponding opening/closing bracket. | |||
fc Move right to the next occurance of char c. | |||
Fc Move left to the previous occurance of c. | |||
tc Move right to the next occurance of c, then one char backward. | |||
Tc Move left to the previous occurance of c, then one char forward. | |||
; Redo the last character finding command. | |||
, Redo the last character finding command in opposite direction. | |||
| Move to the n-th column (you may specify the argument n by typing it on number keys, for example, 20|) | |||
x Delete a single character under the cursor. | |||
X Delete a character before the cursor. | |||
d<mov. comm> Delete text of a movement command <mov. comm> (see above). | |||
D Delete to the end of the line (equivalent to d$). | |||
dd Delete current line (equivalent to 0d$). | |||
CTRL-w Delete the previous word. | |||
CTRL-u Delete from the cursor to the beginning of line. | |||
u Undo previous text modification. | |||
CTRL-r Undo, undo | |||
U Undo all previous text modifications. | |||
. Redo the last text modification. | |||
y<mov. comm> Yank a movement into buffer (copy). | |||
yy Yank the whole line. | |||
p Insert the yanked text at the cursor. | |||
P Insert the yanked text before the cursor. | |||
k Insert the yanked text before the cursor. | |||
j Insert the yanked text before the cursor. | |||
G Insert the yanked text before the cursor. | |||
/string or Search history backward for a command matching string. | |||
CTRL-r | |||
?string or Search history forward for a command matching string. | |||
CTRL-s (Note that on most machines Ctrl-s STOPS the terminal | |||
output, change it with `stty' (Ctrl-q to resume)). | |||
n Repeat search in the same direction as previous. | |||
N Repeat search in the opposite direction as previous. | |||
TAB or = or List all possible completions. | |||
CTRL-i | |||
* Insert all possible completions. | |||
~ | Invert case of the character under cursor and move a character right. | |||
==yank buffers== | ==yank buffers== | ||
Copy data to different buffers. | Copy data to different buffers. | ||
Line 28: | Line 103: | ||
Sort part of file. | Sort part of file. | ||
:96,99!sort # will pass lines 96 through 99 through the sort filter and replace those lines with the output of sort. | :96,99!sort # will pass lines 96 through 99 through the sort filter and replace those lines with the output of sort. | ||
==sort visual selection== | |||
Mark text and verify the following command is executed. | |||
:'<,'>sort /\ze\%V/ | |||
==run command== | |||
:!date | |||
==Add output of command== | |||
:r !date | |||
[[Category:Applications]] | |||
==Do something on startup== | |||
edit file start cursor at first occurance of pts/4 | |||
vim /tmp/forest -c "/pts\/4" | |||
==save file as root== | |||
:w !sudo tee % | |||
==search and replace== | |||
:%s/foo/bar/gc | |||
==indent lines== | |||
mark lines with V | |||
> To indent lines | |||
If you want to indent by two spaces. | |||
:set shiftwidth=2 | |||
==vim diff== | |||
vim -d passwd1 passwd2 | |||
==vim with many windows== | |||
New window | |||
CTRL + w | |||
move between windows. | |||
CTRL + w + -> or <- ... | |||
==open two files side by side== | |||
vim -O /file1 /file2 | |||
==Search for multiple words== | |||
/\(WORD1\|Word2\) | |||
==search for empty lines where next line does not start with *== | |||
/^$ \n\(^\*\)\@! | |||
==stop searching== | |||
:noh | |||
==disable colours== | |||
syntax off | |||
==delete words== | |||
dw delete until deimiter. | |||
dW delete until space delimiter | |||
df space deletes from the cursor position to, and including, the next space. |
Latest revision as of 11:31, 22 October 2024
Useful commands.
registries
List registers.
:reg
Paste register 5
"5p
spell check
Enable spell check. Suggestions. Move between words. Stop spell check.
:set spell spelllang=en,sv z= ]s and [s :set nospell
Switch to last file
:e#
vim shortcuts
Shortcut Description ESC Switch to command mode. i Insert before cursor. a Insert after cursor. I Insert at the beginning of line. A Insert at the end of line. C Change text to the end of line (equivalent to c$). cc or S Change current line (equivalent to 0c$). s Delete a single character under the cursor and enter input r Replaces a single character under the cursor (without leaving command mode). R Replaces characters under cursor. v Edit (and execute) the current command in the text editor. h Move one character right. l Move one character left. w Move one word or token right. b Move one word or token left. W Move one non-blank word right. B Move one non-blank word left. e Move to the end of the current word. E Move to the end of the current non-blank word. 0 Move to the beginning of line ^ Move to the first non-blank character of line. $ Move to the end of line. % Move to the corresponding opening/closing bracket. fc Move right to the next occurance of char c. Fc Move left to the previous occurance of c. tc Move right to the next occurance of c, then one char backward. Tc Move left to the previous occurance of c, then one char forward. ; Redo the last character finding command. , Redo the last character finding command in opposite direction. | Move to the n-th column (you may specify the argument n by typing it on number keys, for example, 20|) x Delete a single character under the cursor. X Delete a character before the cursor. d<mov. comm> Delete text of a movement command <mov. comm> (see above). D Delete to the end of the line (equivalent to d$). dd Delete current line (equivalent to 0d$). CTRL-w Delete the previous word. CTRL-u Delete from the cursor to the beginning of line. u Undo previous text modification. CTRL-r Undo, undo U Undo all previous text modifications. . Redo the last text modification. y<mov. comm> Yank a movement into buffer (copy). yy Yank the whole line. p Insert the yanked text at the cursor. P Insert the yanked text before the cursor. k Insert the yanked text before the cursor. j Insert the yanked text before the cursor. G Insert the yanked text before the cursor. /string or Search history backward for a command matching string. CTRL-r ?string or Search history forward for a command matching string. CTRL-s (Note that on most machines Ctrl-s STOPS the terminal output, change it with `stty' (Ctrl-q to resume)). n Repeat search in the same direction as previous. N Repeat search in the opposite direction as previous. TAB or = or List all possible completions. CTRL-i * Insert all possible completions. ~ | Invert case of the character under cursor and move a character right.
yank buffers
Copy data to different buffers.
vi /etc/{passwd,group,shadow} /tmp/newfile "a2yy # Yank two lines to yank buffer a. :n # Go to next file. "b2yy :n #c2yy :n "ap # paste yank buffer a. "bp "cp
Delete lines and add to a buffer.
vi /tmp/tmp1 /tmp/tmp2 "add # Delete some lines and add to buffer a. "Add # Go to next lines and add lines to buffer a. :wn "ap # Paste buffer.
Deleted lines atomically get numbered buffers.
"2p # Get back the deletion you did before this one.
filter
Sort part of file.
:96,99!sort # will pass lines 96 through 99 through the sort filter and replace those lines with the output of sort.
sort visual selection
Mark text and verify the following command is executed.
:'<,'>sort /\ze\%V/
run command
:!date
Add output of command
:r !date
Do something on startup
edit file start cursor at first occurance of pts/4
vim /tmp/forest -c "/pts\/4"
save file as root
:w !sudo tee %
search and replace
:%s/foo/bar/gc
indent lines
mark lines with V
> To indent lines
If you want to indent by two spaces.
:set shiftwidth=2
vim diff
vim -d passwd1 passwd2
vim with many windows
New window
CTRL + w
move between windows.
CTRL + w + -> or <- ...
open two files side by side
vim -O /file1 /file2
Search for multiple words
/\(WORD1\|Word2\)
search for empty lines where next line does not start with *
/^$ \n\(^\*\)\@!
stop searching
:noh
disable colours
syntax off
delete words
dw delete until deimiter. dW delete until space delimiter df space deletes from the cursor position to, and including, the next space.