ALL LINUX
  • Linux 4 Network Engineers
  • Introduction
    • Untitled
  • LINUX - "HOW-TO-DO"
    • SYSADMIN
      • MONITORING
        • Stress Testing Linux
      • DEBIAN APT
      • DISK
        • Directory Share - NFS,CIFS
        • LOGICAL VOLUME MANAGEMENT (LVM)
        • FILE SYSTEMS
          • ZFS
      • FIND & DU
      • SECURITY
        • SSH Open Format
      • USERS
        • Accounts, Groups etc
        • Assess User Activity
    • SCRIPTING & TEXT EDIT
      • BASH SCRIPTING & PROJECTS
        • 1) How to Build a Bash Script
        • 2) Variables & Shell Expansions
        • 3) Processing Command Lines
        • 4) Requesting User Input
      • VIM Editor
        • Vim Tips
      • GREP, EGREP & REGEX
        • REGEX
        • REGEX2
        • NINJA-REGEX
      • SED and AWK
  • Containers
    • MULTIPASS
    • LXD LXC
    • KUBERNETES (K8's)
  • FOSS
    • CUMULUS LINUX
      • Fundamentals
        • Cumulus Linux Introduction
        • Cumulus Linux Architecture
        • Cumulus VX
        • Initial Setup
      • NCLU
  • Linux Prof Inst Cert [LPIC-1]
    • LPIC 1
      • CH1-Linux Command-Line Tools
        • Work on the Command Line
        • Streams, Pipes and Redirects
          • Text Streams Using Filters
        • Search Text Files - Regular Expressions
        • Basic File Editing
      • CH2-Managing software and Processes
      • Ch3-Configuring Hardware
      • Ch4-Managing Files
      • Ch5-Booting, Initialising and Virtualising Linux
Powered by GitBook
On this page
  • Resources
  • Vim Philosophy
  • Understanding Vi Modes
  • Normal Node
  • Insert Mode
  • Command Mode
  • Visual Mode
  • Working With Files
  • Opening files:
  • Closing Files:
  • Saving Files
  • Navigation
  • Jumping Around the File
  • Navigating in INSERT MODE
  • Search

Was this helpful?

  1. LINUX - "HOW-TO-DO"
  2. SCRIPTING & TEXT EDIT

VIM Editor

VI was the first screen editor written for UNIX. Although it was intended to be small and simple, it can be a challenge for users who use GUI editors such as Notepad++ or gedit, to name a few:)

Previous4) Requesting User InputNextVim Tips

Last updated 3 years ago

Was this helpful?

Resources

To use Vi we must first understand the 3 modes in which this powerful program operates, in order to begin learning later about its text-editing procedures. Please note that modern Linux distro's ship with a variant of Vi called Vim (vi improved). For this narrative we use vi and vim interchangeably.

Vim Philosophy

Vim is different to other text editors but 2 main features makes it so different:

  • Modal Editing - Effectively editing text makes Vim more powerful than any other text editor. Want to jump to the top of the file gg or go to the bottom G. In normal text editors you type gg or G you will in fact type those letters into your file, that's why Vim has modes, and pressing gg or G in a different mode produces different results

  • Operators- If you are moving your cursor using arrow keys, "page-up", "page-down", "end" and "home" keys - Vim has so much more to offer: d$ if you want to delete text to the end of the line ($= end of line and d=delete). Here 'd' is the operator command. These type of commands (operators) can delete, change or insert text, copy or format it etc

Understanding Vi Modes

Vim has 12 modes in total but we will normally use between 4 and 5 in our daily use. Very important to understand these.

Normal Node

When you start vim = normal mode. Mostly used for navigation and text manipulation. A good habit to adopt and keep in mind: whenever you're not typing - get back into normal mode

Insert Mode

Inserts new text, but can also run some commands

Command Mode

In this mode you can run Ex commands like :set number, enter search patterns like /word and enter filter commands. After running the command Vim returns to normal mode.

Visual Mode

For navigation and manipulation of text selections. It's similar to Normal mode, but the movement commands extend a highlighted area. When you use a non-movement command, it’s executed for the highlighted area. By default, there is “– VISUAL –” shown at the bottom of the window.

Working With Files

Current file = current 'buffer'

Opening files:

  • From terminal

    • vim /etc/passwd

  • Inside Vim

    • :e /etc/passwd

Copy a file or contents of a file to the current file using the read command or r :r file.txt Insert the file file.txt below the cursor in current buffer. :0r file.txt Insert the file file.txt before the first line. :r!sed -n 2,8p file.txt Insert lines 2 to 8 from file file.txt below the cursor :r !ls Insert a directory listing below the cursor.

Closing Files:

:wq Save currently opened file and exit Vim (even if file is not changed). :x Exit Vim but write only when changes have been made. ZZ Equivalent to :x. Notice there’s no : This is a key press :q! Exit Vim without saving currently opened file. :qa Exit all open files in current Vim session

Saving Files

:w Save currently opened file (which was previously saved). :w file.txt Save currently opened file as file.txt. :w! file.txt Save file as file.txt with overwrite option. :sav file.txt Save current buffer as a new file file.txt. :up[date] file.txt Like :w but only save when the buffer has been modified

Navigation

Key Command

Description

h or left arrow (5h or 5 left arrow)

Go one character to the left (or 5 characters left)

j or down arrow (5j or 5 down arrow)

Go down one line

k or up arrow (6k or 6up arrow)

Go up one line

l or right arrow (6l or 6right arrow)

Go one character to the right

H

Go to top of the current window

M

Go to middle of the current window

L

Go to bottom of current window

w (or 3w etc)

Move one word (incl delimiters)to the right (can also use 3w etc)

W

Move one word (excludes deliminator like .{ } - $ etc

b (or 7b etc)

Move back one word to the left (incl deliminator)(Can also use 7b etc)

B

Move back one word to the left (excludes deliminator)

e

end of current word (incl deliminator)

E

end of current word (excludes deliminator)

0(zero)

Go to the beginning of the current line

^

Go to the first non blank character on the current line

$

Go to the end of the current line

Ctrl-b

Go back one page

Ctrl-f

Go forward one page

i

Insert

r

Replace a single letter under the cursor

R

Replace multiple characters

x

Deleting text under character

Shift-x

Deletes character before cursor

cw

Change the current word with new text under cursor

dw

Deleting the single word beginning with character under cursor

dd

Delete entire current line

Shift-d

Deletes everything from the cursor to the end of the line

5dd

Deletes 5 (or whatever number) lines beginning with current line

D

Delete remainder of the line starting with current cursor position

:set nu

Displays all line number

:12,22w test

Writes lines 12 - 22 to new file 'test' in pwd

Replaces every occurrence (the g) of apple with pear

%s/apple/pear/g

:%s/^M//g

How to remove Ctrl-M characters from a file in UNIX.(Hold down Ctrl key press V and M in succession

yy

copy(yank,cut) the current line into the buffer

Nyy

copy(yank,cut) the next N lines including current line

p

put(paste) the line(s) in the buffer after the current line

Jumping Around the File

gg Go to the top of the file G Go to the bottom of the file { Go to the beginning of current paragraph } Go to the end of current paragraph % Go to the matching pair of ( ), [ ], { } 50% Go to line at the 50% of the file :NUM Go to line NUM. :28 jumps to line 28

Navigating in INSERT MODE

Search

/hello - search hello using the forward slash, use n for next or N to search backwards Can use ggn to start from the top or GN to start from the bottom up :) * or # Search for word under cursor use * going forward and # searching backwards - COOL!! / or ? + up or down arrows to search previous history

When you cut and paste into vi form an outside program, the formatting can get a little messed up! Vim comes with a feature called "pastetoggle" which you can enable by editing the line /etc/vim/vimrc and adding the following at the bottom:

nnoremap <F2> :set invpaste paste?<CR>
set pastetoggle=<F2>
set showmode

2MB
Mastering Vim Quickly From WTF to OMG.pdf
pdf
Mastering Vim Quickly From WTF to OMG
2MB
Vim 101 Hacks.pdf
pdf
Vim 101 Hacks
Hit F2 either in normal mode or after INSERT mode and you can see if 'paste' or 'no paste' mode is set