continuous improvement | Home | Services | FAQs | Notes | Contact Us |
Delete lines with one character or none
:g/^.\?$/d
Remove spaces at end of each line
:% s/\s\+$//g
Lines with one word and postcode at end of line, append to previous line
:g/^\w\+\s\d\{4}$/normal kJ
Put a space at the beginning of each line for lines containing no digits \D
:% s/\(\D\+$\)/ * \1/g
Move lines that start with a space and move to previous line
:g/^\s\*/normal kJ
Remove the following Codes
:g/\sBC\s\|\sDC\s\|\sGPO\s\|\sMC\s\|\sPO\s/d
Show long lines greater than 60 characters
/\%&gr;60v.\+
Delete lines of text
:g/Where a name/d
Check for lines with leading digits
/^\d\+
ctrl+w o
alias g="vim --remote-silent"
Use the option textwidth to set the line length of inserted text :set tw=30
Turn wrapping off :set nowrap
Print from Vim :%w !lp
Yanking text to the clipboard "*y
Pasting from clipboard "*p
"x yiw
"xp
Ignore case :set ic
Highlight search, insensitive search :set hls is
Remove highlighting :set nohlsearch
To do a find and replace :s/back/black/gc
on the current line
To do a find and replace on the whole file :%s/back/black/gc
g=global c=confirm
To find the next instance of a word, have the cursor on the word then type *
while in command mode
Go to line number 60, from command mode. 60G
or use 60gg
Set a mark m character
Go to mark ' character
Go to matching bracket %
Inialise variable :let cnt = 0
Global command:g/^/let cnt = cnt + 1|execute"s/^/".cnt."\t"
Or you could do something like this.
From line 3 to end of file :3,$ g/^/let cnt = cnt + 1|execute"s/^/".cnt."\t"
Turn on line numbers :set nu
Turn off line numbers :set nonu
show what the key mappings are in use :map
To check the spelling of a file first save the file :w!
then use ispell :! ispell %
An easier method is to use the :set spell
command. Then press z
followed by =
for each missed spelled word.
Type a command in vim then pass the line to the shell and return the results into vim
Eg. Type date
in Vim. Then in ex mode type :. !sh
For Oracle, sqlplus only:
DEFINE _EDITOR='gVim -c "set filetype=sql"'
Turn off syntax highlighting :highlight on
:set nohls
:set nohls
make the colors for syntax highlighting darker :set bg=dark
:set ingnorecase
:set noingnorecase
:e ftp://password@example.com/folder/file
edit a file:E ftp://password@example.com
display netrw file browser
Record Macro qa
q=record macro a=record macro to lecodeer a
Run Macro @a
@=run macro a=macro in a
Explorer view :E
shows the files located at Vim's current working directory.
You can use the J and K keys to navigate files and directories. To open a file press enter key.
Explorer view opens a window to view the filesystem. Pressing i will toggle the display.
:1,$g/blah/d
= delete begining to end blah
:1,.g/blah/d
= delete to current position
:1,$g/blah/i
= delete begining to end blah
:set list
= Show invisible characters
:%s/^V^K//
= ASCII Char 13 is represented as ^K in VIM
2>>
The 2 represents the number of lines to indent
Before
blah
blah
After
blah
blah
asfdjaslkjflkasfa
:s/\(.*\)/\U\1/
change U to u for first character uppercase
ASFDJASLKJFLKASFA
:s/\(.*\)/\L\1/
change L to l for first character lowercase
clear the "a" register qaq
view contents of register "a" :reg a
Insert Mode
Open q:
Close ctrl+w q
Command Mode
Open ctrl+f
Close ctrl+w q
:h ctrl-w_ctrl-x and/or :h ctrl-w_ctrl-r
If the leading V means visual mode
VU
change line to uppercase
Vu
change line to lowercase
unix
dos
mac
This command will change the fileformat of the file in the current
buffer, this may be necessary if you are seeing many ^M or ^@ symbols.
You may then need to remove /r characters.
:w ++ff=unix
:w ++ff=dos
:w ++ff=mac
Here we are changing a DOS file to unix fileformat. I like all my files to be in Unix format.
:e! ++ff=unix
You will then see the \r shows up as ^M.
:% s/\r//g
Repalce \r with nothing. This will remove the ^M at the end of each line.
set encoding - changes the output encoding shown in the terminal.
set fileencoding - changes the output encoding of the file that is written.
When editing this code there is one element per line as seen when viewing raw XML in a MS web browser.
:%s/>.*//g
Remove everything after first tag.
:%s/\///g
Remove trailing forwad slashes.
:%normal xxx
Remove first three characters from each line.
$wget -m robots=off http://www.kaizentek.com.au
curl -I http://example.com/
:let @a='xxxxx'
Use a capital letter.
"Ay
Open VIM then type the following in command mode.
:e ftp://<username>@<URL>
G command matches lines that start with word function, and the .,+
is a range which selects the current line and line below then the lines are yanked to register a (capital A means append to a register).
:g/^function/.,+ y A