VIM (Vi IMproved) is an open source, lightweight, powerful, and popular (modal) text editor available for Debian-based Linux, RPM-based Linux, and Windows. (Note: MacOS is a BSD distro - Debian based). Other editors include vi, nano, pico, emacs, nvi, elvis, ... .
See: https://www.vim.org
First, check to see if Vim is already installed.
$ which vim
If already installed, you will see the path displayed, for example:
/usr/bin/vim
If VIM is not found, install it by typing the following commands at the command prompt:
$ sudo apt-get update
$ sudo apt-get install vim
To ensure Vim is correctly installed, execute the following command −
$ which vim
Or yum install vim with yum.
Vim is a modal editor. Keystrokes have different results based on the mode. The modes are:
- Normal: navigate the file and make edits
- Insert: for inserting text
- Replace: for replacing text
- Visual (plain, line, or block): select text
- Command: run commands
The mode is displayed in the bottom left corner (unless in normal mode).
You should be in normal mode most of the time. To enter normal mode, press the ESC key (from any mode).
-
hjkl(left, down, up, right) -
words:
w(next word),b(beginning of word),e(end of word) -
Lines:
0(beginning of line),^(first non-blank character),$(end of line) -
Screen:
H(top of screen),M(middle of screen),L(bottom of screen) -
Scroll:
^u(up),^d(down) -
File:
gg(beginning of file),G(end of file) -
Line numbers:
:{number}<CR>or{number}G(line {number}) -
Misc:
%(corresponding item) -
Find:
f{character},t{character},F{character},T{character}- find/to forward/backward {character} on the current line
,/;for navigating matches
-
Search:
/{regex},n/Nfor navigating matches
Used to enter and edit text. The current mode is shown at the bottom left corner of the window. To switch from default command to insert mode, press the i key.
To exit insert mode, use the escape key, esc.
The default mode. Commands include those in this mode, such as copy, paste, delete, and replace. Vim commands without a colon are executed in command mode.
Enter command mode with : from Normal mode. Functionalities in this mode include opening, saving, and closing files. Common commands in this mode include:
-
:qquit and close the window -
:wsave (write) buffer -
:wqsave and quit -
:e {filename}open file to edit -
:lslist/show open buffers -
:help {topic}:help :wopens help on the command:w:help wopens help onwmovement
To write to the buffer:
:w
To quit vim:
:q
To quit without saving changes:
:q!
Save changes and quit the editor with:
:wq
:help
:help [topic]
:help modes
| Command | Meaning |
|---|---|
i |
insert |
o |
newline below (insert mode) |
O |
newline above |
d |
delete |
dw |
delete word |
u |
undo |
y |
yank, copy |
p |
paste |
yy |
copy line |
yw |
copy word |
fo |
find "o" |
. |
repeats last command |
4j |
down 4 lines |
c2w |
change 2 words |
2dw |
delete 2 words |
When working with text files, copying, cutting, and pasting are among the most commonly performed tasks. Vim uses specific terminology:
- Yank (
y) for copy - Delete (
d) for cut - Put (
p) for paste
yy: yank the current line3yy: yank 3 lines from the cursory$: yank from cursor to end of liney^: yank from cursor to start of lineyw: yank to next wordyiw: yank current wordy%: yank to matching character ((),{},[])
dd: delete current line3dd: delete 3 linesd$: delete from cursor to end of linedw: delete to next wordd^: delete to start of line
p: paste after the cursorP: paste before the cursor
-
Move cursor to start of selection
-
Enter visual mode:
v: character selectionV: line selectionCtrl+v: block (column) selection
-
Move to end of selection
-
Press
yto copy ordto cut -
Move to destination and press
porPto paste
Open a file:
vi filename
To go into edit mode: press ESC and type i
To go into command mode: press ESC
To save a file:
:w fileName
To save and quit:
:wq
To jump to a line:
:{line_number}
To search for a string:
/wordToSearch
To quit:
:q
vi /pathtofile/MyFile.txt
Then press:
:X
Enter and confirm password, then save:
:wq
Vim will prompt for the password.
- Open the encrypted file
- Press
:X, hitEntertwice to remove password - Save with
:wq
- Open the file
- Use
:Xto enter a new key - Save with
:wq
Vim is customized via ~/.vimrc
- Extend Vim with plugins
- Use multiple windows:
:sp / :vsp
def fizz_buzz(limit):
for i in range(limit):
if i % 3 == 0:
print('fizz')
if i % 5 == 0:
print('fizz')
if i % 3 and i % 5:
print(i)
def main():
fizz_buzz(10)
The Apple Cat // modem is by far the most expandable modem on the market today...
-
Fix the following in fizz buzz:
- Main is never called
- Starts at 0 instead of 1
- Prints 'fizz' and 'buzz' on separate lines for multiples of 15
- Prints 'fizz' for multiples of 5
- Hard-codes 10 instead of taking a command-line argument
-
Open a new file in Vim.
-
Write some text.
-
Search for a word.
-
Use visual mode to select text.
-
Cut and paste text.
-
Save and exit.
-
Reopen and make changes.
-
Use undo/redo.
-
Replace a word.
-
Save and exit.
