Vim Commands

Vim Commands
Vim Commands

vim commands:

/usr/share/vim/vim73/vimrc_example.vim ~/.vimrc
Copy example file to home directory or wherever
you keep it. The example file may be located
somewhere else depending on os. You could use
another vimrc like the one below.

Install ultimate:awesome vimrc 
———————————————————————————————
git clone git://github.com/amix/vimrc.git ~/.vim_runtime
sh ~/.vim_runtime/install_awesome_vimrc.sh

To update ultimage vimrc:
cd ~/.vim_runtime
git pull --rebase

May need to install ctags:
On Debian base:
sudo apt-get install ctags

end-install-ult-vimrc
—————————————————————


vi - start vi
vim - start vim
vim test.txt +12 - open test.txt and go to line 12
vim notes.txt - open file notes.txt for editing


Only named buffers (a-z) are saved when jumping
between files with :e anotherfile.txt (:e = edit)

v - while in less or more will open vi (or preferred
editor) and edit file exiting, exiting goes back to
less/more

mod1/alt - used with a normal mode button will apply
and exit insert mode, (alt+h move left and exit
insert mode, alt+x delete char behind cursor,
exiting insert mode) ** note that command with two
letters like dd need the first d pressed with alt
and the second without** (alt+d d - delete current
line and exit insert mode)
** Note that certain terminals may overide this.
** For instance, xterm may enter accented characters
** instead.

Windows carriage returns can be created with:
ctrl-v ctrl-m = ^M


normal / command mode
---------------------
ctrl-l - refresh screen
u - undo
U - undo all changes to current line (as long as line has not been left) (vi only)
h - move left (left arrow) (3h 2h 12h)
j - move down (down arrow)
k - move up (up arrow)
l - move right (righ arrow)
#j - moved down # lines (3j 2j 20j)
x - delete one char under cursor
X - delete one char left of cursor
ctrl-r - redo
:w - save file
:up - save file only if it has been updated
:sav filename.txt - save as filename.txt
ZZ - save and exit
:wq! - save and exit
:x - save and exit
:q! - exit without saving
:exit test.txt - exit and save test.txt
:e notes.txt - edit another file (must save changes in current file 1st)
'0 - open previous file
:e! - otherfile.txt - edit other file without saving current
ctrl-e Moves screen up one line
ctrl-y Moves screen down one line
ctrl-u Moves screen up ½ page
ctrl-d Moves screen down ½ page
ctrl-b Moves screen up one page
Ctrl-f Moves screen down one page
z - scroll current line to top of window
zt - scroll current line to top of window
z. - scroll current line to middle of window
zz - scroll current line to middle of window
z- - scroll current line to bottom of window
zb - scroll current line to bottom of window
w - move to beginning of next word (3w 5w 10w)
e - move to end of next word
b - move to beginning of previous word
W - move to begining of next word (determined by whitspace)
E - move to beginning of previous word (determined by whitspace)
B - move to beginning of previous word (determined by whitspace)
$ - move to end of line
^ - move to first non-white char of line
0 - move to beginning of line
J - join two lines together (brings next line up into current line)
#| - move to the nth column of current line, 5| 12| (5th 12th column)
f char - find next occurance of char on current line
t char - move before next occurance of char on current line
F char - find previous occurance of char on current line
T char - move after previous occurance of char on current line
; - repeat last f, t, F, or T
, - reverse the last f, t, F, or T
% - jump to matching () {} [] under cursor
H - move to highest/top line of window
M - move to middle line of window
L - move to lowest/last line of window
m char - mark line as char (a-z), lasts till line deleted, or same char marked elsewhere
' char - return to line name char
'' - return from last movement
G - go to last line of file
#G - go to nth line ( 5G 23G 410G )
1G - go to first line of file
gg - go to first line of file
{ - move to beginning of paragraph
} - move to end of paragraph
( - move to beginning of sentence
) - move to beginning of next sentence
[[ - jump to beginning of code section, if not found jumps to begin of file, (c code sections {})
]] - jump to end of code section, if not found, jump to end of file
/string - find string searching forward
/ctrl-r ctrl-w - search for next occurance of word under cursor
/ctrl-r ctrl-a - search for next occurance of whole-word under cursor
?string - find string looking backward
* - find next occurence of word under cursor
# - find previous occurence of word
/f[iae]t - searches for fit, fat, fet
/a[^bcd] - find a and another letter except: ab ac ad
/\ - search ground only, not: grounded or playground

/ctrl-v ctrl-m - search for windows carraige returns ^M 
:1,$s/^M//g - remove all windows carriage returns
:%s/^M//g - remove all windows carraige returns

ggVG:sno///g - remove all html span tags
:%s/
//g - remove all html pre "open" tags
:%s/<\/table>//g - remove all html table "close" tags
:%s@\(<\/\w\{1,10\}>\)@&\r@g - line break after every html closing tag.
                               Add "c" at end to confirm each. 

:g/^$/d - remove empty lines
:g/^\s*$/d - remove empty lines with or without whitespace
:%s/note/NOTE/g - replace note with NOTES throughout file
:s/test/TEST/g - replace test with TEST on current line
:s/time/TIME/ - replace first occurance of time on current line
:%s/last/lasted/ - replace first occurance of last on each line with lasted
/07-..* + - find lines with 07- anything then a space and a +
\@! - add this to the end of a search, to reverse it
:s/^/# / - hit shift-v, highlight the lines you want, and run this command to comment multiple lines
:g/^/m0 - reverse contents of a file (by lines)
:'<,'>s/^/hahaha/ - add hahaha to the beginning of every line
n - repeat last / or ?
N - reverse last / ?
dw - delete word ***delete is like cut, it copies to buffer and can be pasted***
d$ - delete from cursor to end of line
d^ - delete from cursor to beginning of line
dd - delete current line
d) - delete to beginning of next sentence
dt char - delete til next char on current line (dtb dta dt5 dt?)
S - delete current line and open for insertion
P - put/paste contents of buffer befor cursor
p - paste contents of buffer after cursor
xp - swap two characters (cursor on 1st)
Xp - swap two characters (curson on 2nd)
dwwp - swap two words
d3wwP - delete 3 words and paste after the fourth
d4d - delete four lines
5w - move forward five words
2b - move back two words
yy - yank/copy current line
Y - copy current line
yw - yank word
y$ - yank from cursor to end of line
y) - yank to the beginning of the next sentence
yte - yank till the next e
yy - yank current line
yL - yank current line to lowest point of window
y/stop - yank from cursor till string stop
y2} - yank two paragraphs
y'a - yank current line till line with mark a (must have mark first)
"2P - paste buffer 2 before cursor ***last 9 cuts,copies and deletes kept in buffers 1-9***
"3p - paste buffer 3 after cursor (1-9) ( "4p "7p )
"ad} - delete till end of paragraph and store in buffer a ***can create buffers a-z***
"by5y - copy 5 lines into buffer b
"ny150G - copy/yank 150 lines into buffer n
"dp - put/paste buffer d after cursor
"xP - paste buffer x before cursor
c% - change text in between () {} [] (cursor must be on a bracket)
di> - deletes innards of angle bracket <>,[],(),{},w,W (cursor must be inside brackets)
da> - deletes brackets and innards
~ - change case of char under cursor
. - repeat last command
ctrl-g - Show file name, line count, % in document; (file status)
>> - shift current line right
<< - shift current line left
>} - shift right till end of paragraph
<} - shift left till end of paragraph
>aB - autoindent a block
== - reindent lines (useful with visual mode)
23== - reindent next 23 lines
ga - show info of char under cursor (ascii, decimal, hex, octal)
gf - open file under cursor
:syntax off - turn off color syntax. Use "on" to turn on.

v - enter visual mode. (selects by character)
V - enter visual mode. (selects by lines)
ctrl-v - enter visual mode. (selects by vertical columns)
"+y - copy selection to clipboard.  (use * instead of + on cygwin)
"+p - paste from clipboard. (use * instead of + on cygwin/windows)

yypVr—
Copy line; paste it; highlight it; replace it with chosen char
A way to underline a heading; replace "—" any character.

:w !sudo tee %
Save file when you forget to sudo before opening it.

:!xlcip - writes file to clipboard. (xclip must be installed)

end-normal/command-mode
———————————————————————


Insert Mode
-----------
esc - exit out of insert mode (command mode)
ctrl-[ - exit our of insert mode
i - insert starting at cursor
I - insert at beginning of current line
a - insert/append after cursor
A - append at end of line
o - open next line for editing
O - open previous line for editing
bksp - delete previous char
del - delete next char
#s - substitute/overwrite for # chars, ( 1s 4s 30s )
#S - substitute for # lines
r - replace char under cursor, don't need to hit escape to get out
R - enter overtype/overwriting mode
cw - change word
c$ - change till end of line
c) - change till next sentence
cte - change till next e
c4w - change next 4 words
cc - change current line
c} - change till end of paragraph
ctrl-r % - pastes/puts current filename


Inserting Unicode: (while in insert mode)
——————————————————
ctrl-v u2014 — insert emdash

end-insert-mode
———————————————


System Calls (unix commands)
————————————
{!}fmt -50 — format current paragraph width to 50 chars 
{!}sort - alpha sort lines of current paragraph
!!ls - print ls of current direcotry into vi


Options
———————
:set showmode - displays mode insert/append/command, etc
:set nu - displays line numbers on each line
:set all - shows all settings
:set hlsearch - highlights searched text
:set nohlsearch - turn off highlights searched text
:noh - clears highlights of text searched with / ? etc
:nohl - clears highlights of text searched with / ? etc
/aksfjklajkej - also clears highlighted searched text (search jibberish)
:set paste - useful for pasting in from gui etc, keeps format, no autoindents
:set nopaste
:set pastetoggle= - auto sets paste when pasting; turns off after pasted
:set tabstop=4 - set tab to be 4 spaces
:set shiftwidth=4 - set indention to 4 spaces
:set expandtab - stops 8/4 spaces from becoming a tab
:set nowrapscan - turn off wrapping of searches (don't go paste end or top of document)
:set wrapscan
:set ingorecase - ignore case while searching (:set ic)
:set noignorecase (:set noic)
:set autoindent (:set ai)
:set noautoindent (:set noai)
:set ai lisp - make autoindent lisp acceptable
:setlocal cindent - set indention to C C#
:set sm - match paranthesis
:set vb - set visual bell, instead of annoying beep

----
:set formatoptions? - View current format options

:set formatoptions-=o
Turn off auto comment next/previous inserting a line 
with o/O commands

autocmd FileType * setlocal formatoptions-=o
Add this to .vimrc to override plugins and everything
else that may change your format options.
-=o = turns off auto comments when inserting with o/O
----

To view all format options see:
http://vimdoc.sourceforge.net/htmldoc/change.html#fo-table

:highlight MatchParen cterm=bold ctermfg=red ctermbg=blue
Change color of cursor and matching bracket/paren highlight
in order to see the cursor when matching


Split Windows
—————————————
:sp - new window above
:vs - new window to left
:qa - close all windows
Ctrl-w {left,right,up,down}
Ctrl-w Ctrl-w - toggle window focus
Ctrl-w c - close current window
Ctrl-w = - autosizes windows
:ba - new buffer for all windows
:ls - list buffers


Transparency
————————————
To add transparency find the theme and comment out
the tranparency lines. For instance if using peaksea
find the transparency lines and uncomment them:
~/.vim_runtime/sources_forked/peaksea/colors
hi Normal       ctermfg=16  ctermbg=NONE    cterm=NONE
hi NonText      ctermfg=69  ctermbg=NONE    cterm=NONE
hi Normal       ctermfg=252 ctermbg=NONE    cterm=NONE

Previous
Next Post »