VIM - Command Examples: Difference between revisions

From IT-Arts.net
Created page with "Category:Wiki === Opening and Closing Files === To open a file in Vim, use the following command: <nowiki> vim filename</nowiki> To open multiple files: <nowiki> vim file1 file2 file3</nowiki> To quit Vim: <nowiki> :q</nowiki> To save changes and exit: <nowiki> :wq</nowiki> To force quit without saving changes: <nowiki> :q!</nowiki> To save a file without exiting: <nowiki> :w</nowiki> === Moving the Cursor === Move the cursor word by word: <nowiki> w (..."
 
m Text replacement - "Category:Wiki" to "Category:Wiki '''''[https://it-arts.net/index.php/Category:Wiki Return to Wiki Index]''''' "
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
[[Category:Wiki]]
[[Category:Wiki]]


=== Opening and Closing Files ===
'''''[https://it-arts.net/index.php/Category:Wiki Return to Wiki Index]'''''
 
 
== Opening and Closing Files ==
To open a file in Vim, use the following command:
To open a file in Vim, use the following command:
  <nowiki>
  <nowiki>
Line 26: Line 29:
:w</nowiki>
:w</nowiki>


=== Moving the Cursor ===
== Moving the Cursor ==
Move the cursor word by word:
Move the cursor word by word:
  <nowiki>
  <nowiki>
Line 63: Line 66:
N</nowiki>
N</nowiki>


=== Editing Text ===
== Editing Text ==
Delete a character under the cursor:
Delete a character under the cursor:
  <nowiki>
  <nowiki>
Line 108: Line 111:
cw</nowiki>
cw</nowiki>


=== Visual Mode ===
== Visual Mode ==
Enter visual mode (to select text):
Enter visual mode (to select text):
  <nowiki>
  <nowiki>
Line 121: Line 124:
Ctrl + v  (Visual block mode)</nowiki>
Ctrl + v  (Visual block mode)</nowiki>


=== Searching and Replacing ===
== Searching and Replacing ==
Find and replace in the entire file:
Find and replace in the entire file:
  <nowiki>
  <nowiki>
Line 138: Line 141:
*  (Search for the word under the cursor)</nowiki>
*  (Search for the word under the cursor)</nowiki>


=== Working with Buffers ===
== Working with Buffers ==
List all open buffers:
List all open buffers:
  <nowiki>
  <nowiki>
Line 155: Line 158:
:bd</nowiki>
:bd</nowiki>


=== Working with Windows and Tabs ===
== Working with Windows and Tabs ==
Split the window horizontally:
Split the window horizontally:
  <nowiki>
  <nowiki>
Line 183: Line 186:
:tabp</nowiki>
:tabp</nowiki>


=== Macros ===
== Macros ==
Start recording a macro:
Start recording a macro:
  <nowiki>
  <nowiki>
Line 200: Line 203:
5@a  (Repeat macro 'a' five times)</nowiki>
5@a  (Repeat macro 'a' five times)</nowiki>


=== Customizing Vim ===
== Customizing Vim ==
Set line numbers:
Set line numbers:
  <nowiki>
  <nowiki>
Line 233: Line 236:
:syntax on</nowiki>
:syntax on</nowiki>


=== Working with Multiple Files ===
== Working with Multiple Files ==
Open files side by side:
Open files side by side:
  <nowiki>
  <nowiki>
Line 243: Line 246:
Ctrl + w, w</nowiki>
Ctrl + w, w</nowiki>


=== Advanced Text Navigation ===
== Advanced Text Navigation ==
Go to the matching parenthesis or bracket:
Go to the matching parenthesis or bracket:
  <nowiki>
  <nowiki>
Line 268: Line 271:
?search_term</nowiki>
?search_term</nowiki>


=== Ex Commands ===
== Ex Commands ==
Execute a shell command:
Execute a shell command:
  <nowiki>
  <nowiki>
Line 285: Line 288:
:qa!</nowiki>
:qa!</nowiki>


=== Autocommands ===
== Autocommands ==
Set an autocmd to highlight trailing spaces:
Set an autocmd to highlight trailing spaces:
  <nowiki>
  <nowiki>
autocmd BufWritePre * %s/\s\+$//e</nowiki>
autocmd BufWritePre * %s/\s\+$//e</nowiki>


=== Plugins and Extensions ===
== Plugins and Extensions ==
To install plugins with vim-plug:
To install plugins with vim-plug:
  <nowiki>
  <nowiki>
Line 299: Line 302:
:PlugInstall</nowiki>
:PlugInstall</nowiki>


=== Miscellaneous ===
== Miscellaneous ==
Show the current mode (insert, command, etc.):
Show the current mode (insert, command, etc.):
  <nowiki>
  <nowiki>

Latest revision as of 07:10, 17 January 2026


Return to Wiki Index


Opening and Closing Files

To open a file in Vim, use the following command:

vim filename

To open multiple files:

vim file1 file2 file3

To quit Vim:

:q

To save changes and exit:

:wq

To force quit without saving changes:

:q!

To save a file without exiting:

:w

Moving the Cursor

Move the cursor word by word:

w  (Move to the start of the next word)
b  (Move to the start of the previous word)

Move the cursor by line:

j  (Move down one line)
k  (Move up one line)

Move to the beginning or end of a line:

0  (Move to the beginning of the line)
$  (Move to the end of the line)

Move to the beginning of the file:

gg

Move to the end of the file:

G

Search for a word:

/search_term

Jump to the next match:

n

Jump to the previous match:

N

Editing Text

Delete a character under the cursor:

x

Delete an entire line:

dd

Delete a word:

dw

Delete until the end of the line:

D

Delete from the cursor to a specific character:

dF<char>  (Delete up to and including <char>)

Undo the last change:

u

Redo the last undone change:

Ctrl + r

Copy a line:

yy

Paste the copied text:

p

Replace the character under the cursor:

r<char>

Replace a word:

cw

Visual Mode

Enter visual mode (to select text):

v

Select a block of text:

V  (Visual mode for line selection)

Select text in a block, for columns:

Ctrl + v  (Visual block mode)

Searching and Replacing

Find and replace in the entire file:

:%s/old/new/g

Find and replace with confirmation:

:%s/old/new/gc

Find and replace in a specific range (lines 10 to 20):

:10,20s/old/new/g

Find and replace the current word under the cursor:

*  (Search for the word under the cursor)

Working with Buffers

List all open buffers:

:ls

Switch to the next buffer:

:bn

Switch to the previous buffer:

:bp

Delete a buffer:

:bd

Working with Windows and Tabs

Split the window horizontally:

:split

Split the window vertically:

:vsplit

Move between windows:

Ctrl + w, h  (Move left)
Ctrl + w, j  (Move down)
Ctrl + w, k  (Move up)
Ctrl + w, l  (Move right)

Open a new tab:

:tabnew

Switch to the next tab:

:tabn

Switch to the previous tab:

:tabp

Macros

Start recording a macro:

qa  (Record into register 'a')

Stop recording the macro:

q

Play the macro:

@a

Repeat the macro multiple times:

5@a  (Repeat macro 'a' five times)

Customizing Vim

Set line numbers:

:set number

Set relative line numbers:

:set relativenumber

Enable line wrapping:

:set wrap

Set indentation level:

:set shiftwidth=4

Turn on auto-indentation:

:set smartindent

Set search highlighting:

:set hlsearch

Disable search highlighting:

:set nohlsearch

Enable syntax highlighting:

:syntax on

Working with Multiple Files

Open files side by side:

:vs filename  (Vertical split with file)
:sp filename  (Horizontal split with file)

Cycle through files in the current window:

Ctrl + w, w

Advanced Text Navigation

Go to the matching parenthesis or bracket:

%

Move to the first non-blank character of the current line:

^

Move to the first character of the current line:

0

Move to the end of the current line:

$

Jump to a specific line:

:linenumber

Search backward:

?search_term

Ex Commands

Execute a shell command:

:!command

Write all files:

:wall

Open a file in a different tab:

:tabnew filename

Exit all files:

:qa!

Autocommands

Set an autocmd to highlight trailing spaces:

autocmd BufWritePre * %s/\s\+$//e

Plugins and Extensions

To install plugins with vim-plug:

Plug 'plugin/repo'

To install all plugins:

:PlugInstall

Miscellaneous

Show the current mode (insert, command, etc.):

Ctrl + g

Show file information (file name, line, column, etc.):

Ctrl + g