This is an old revision of the document!
Table of Contents
VIM
There's an old joke that Emacs would be a great operating system if only it had a decent text editor,
whereas vi would be a great text editor if only it had a decent operating system.
File Cleaning
Remove Blank line from the file
to remove blank lines please use following.
:g/^$/d
OR
:g/^ *$/d
Remove Tailing space as well space at beginning using VI
To remove tailing space
:%s/\s\+$//
To remove space at beginning
:%s/^\s\+//
Replace tab with space
To replace tab with space you need to use
:%s/[CTRL-i]/ /g
Delete Line start with
To delete lines start with some word e.g. Redirect
:g/^Redirect/d
Remove Dos / Windows carriage-return
To remove Carriage return in vi use following
:%s/^M//g
NOTE : to create “^M” use “Ctrl+v Ctrl+M” Regular ^M does not help.
Convert all character to lower OR upper case
NOTE : This will apply to whole file as we are using (%)
To convert upper case use following
:%s/\([a-z]\)/\u\1/g
To convert lower case use following
:%s/\([A-Z]\)/\l\1/g
Match Start of line Append End of line
To Match Start of line and append that line use following
(search Append end of line)
%g/^DocumentRoot/s/$/\/www/
Which append “/www” if start of line is “DocumentRoot”.
Also you can append after word by following.
%s/word/&test/g
So all word is get replaced by “wordtest”.
Marking
VIM Scripts
Introduction to vim scripts “:help vim-script-intro”
Running Vim scripts
There is many way to run vim script simple way is to run calling script from `source`
:source /home/yahoo/test.vim
You can also call command from script directly
:call MyBackupFunc(expand('%'), { 'all':1, 'save':'recent'})
You can do mapping of the keyboard using the variables.
:nmap ;s :source /home/yahoo/test.vim<CR>
The key sequence `;s` will execute the specified script file
You can call fucntion using the stored mapping same way.
:nmap \b :call MyBackupFunc(expand('%'), { 'all': 1 })<CR>
To call the function you need to call enter squence `\b`
syntax highlighting
VIM support syntax highlighting. to turn it on.
:syntax on
to turn it off
:syntax off
VIM Variable / Initialization (.vimrc)
toggle syntax highlighting
function! ToggleSyntax() if exists("g:syntax_on") syntax off else syntax enable endif endfunction nmap <silent> ;s :call ToggleSyntax()<CR>
`;s` sequence to flip syntax highlighting on or off every time typed when you're in Normal mode.
nmap stands for “normal-mode key mapping”.
<silent> option causes the mapping not to echo any command it's executing.
Creating centered titles
function! CapitalizeCenterAndMoveDown() s/\<./\u&/g "Built-in substitution capitalizes each word center "Built-in center command centers entire line +1 "Built-in relative motion (+1 line down) endfunction nmap <silent> \C :call CapitalizeCenterAndMoveDown()<CR>
single backslash used for continuation marker
call SetName( \ first_name, \ middle_initial, \ family_name \ )
Also you can use “|” to write two line on singe line.
echo "Starting..." | call Phase(1) | call Phase(2) | echo "Done"
You can comment using single quote, as double quote used to define a string.
also you can use “|” to escape the comment in some cases.
echo "> " |"Print generic prompt
Defining variable
to define variable you need to use word `let`
let name = "Damian" let height = 165 let interests = [ 'Cinema', 'Literature', 'World Domination', 101 ] let phone = { 'cell':5551017346, 'home':5558038728, 'work':'?' }
- scalar: a single value, such as a string or a number. For example: “Damian” or 165
- list: an ordered sequence of values delimited by square brackets, with implicit integer indices starting at zero. For example: ['Cinema', 'Literature', 'World Domination', 101]
- dictionary: an unordered set of values delimited by braces, with explicit string keys. For example: {'cell':5551017346, 'home':5558038728, 'work':'?'}
Basic Info
Variable scoping
g: varname The variable is global s: varname The variable is local to the current script file w: varname The variable is local to the current editor window t: varname The variable is local to the current editor tab b: varname The variable is local to the current editor buffer l: varname The variable is local to the current function a: varname The variable is a parameter of the current function v: varname The variable is one that Vim predefines
pseudovariables
& varname A Vim option (local option if defined, otherwise global) &l: varname A local Vim option &g: varname A global Vim option @ varname A Vim register $ varname An environment variable