Vi tips

From PvdMWiki
Jump to: navigation, search

vi editor tips

Keys

Key(s): Movement:
0 (zero) 1st column on the line
^ 1st non-blanc char on line
$ Last char on line
g_ Last non-blanc char on line
w Next word
W Next word and skip interpunction
e End of current word
E End of current word incl .interpunction
b Begin of current word
B Begin of current word incl. interpuction
ge End of previous word
gE End of previous word and skip interpunction
O Insert new line and switch to insert mode
Key(s): Action:
x Delete char under cursor
X Delete char before cursor
r Replace char under cursor with next typed char
d Delete chars until position of next command
dd Delete line
y Copy text until position of next command
yy Copy line
p Paste text after cursor
P Paste text before cursor
Key(s): Selection:
V Start selecting block of text
x Cut block of text
c Copy block of text
iw Word
aw Word incl spaces
is Sentence
as Sentence incl spaces
ip Paragraph
ap Paragraph incl spaces and empty lines
i( Block between brackets
a( Block incl brackets
i< Block between < >
a< Block incl < >
i{ Block between { }
a{ Block incl { }
i” Block between “ “
a” Block incl “ “
i` Block between ` `
a` Block incl ` `
Search: Action:
/text Search for text starting at cursor
n Find next
N Find previous
// Repeat last search
* Search forward for word currently under cursor
# Search backwards for word currently under cursor
 :set hlsearch Highlights all matches
 :set nohlsearch Disable ^
 :nohl Don't show highlights from last search
 :set incsearch Enables incremental search
 :set noincsearch Disables ^

u undo

cut multiple lines: ###yy

delete rest of line from cursor: SHIFT-d

To open multiple files on a window

  • Horizontal split
vi -o file-one file-two
  • Vertical split
vi -O file-one file-two
  • Switch between the panes you are using.
     CTRL + W + Left key to activate left windows
     CTRL + W + Right key to activate right windows
     CTRL + W + Up key> to activate to windows above current one
     CTRL + W + Down key> to activate to windows down current one
  • Split Open one file at a time
vi file-one
  • To open the second file, go to the command mode (Esc)
:new file-two

OR

:split file-two
  • to split vertically
:vsplit
  • Typ 'CTRL-w w' to switch between the windows
  • Open new file:
:e newfile

Tabs

To open Vi with multiple tabs open:

vi -p /var/log/*.log
  • switch between tabs with gt
  • open new tab
:tabnew
or
:tabe file

to open new file in new tab

Search and Replace

Go in command mode and type:

Replace First occurence of string

:%s/string-to-replace/replace-with-string/

Replace All occurence of string

:%s/string-to-replace/replace-with-string/g

Replace All occurence of string with confirmation

:%s/string-to-replace/replace-with-string/gc

avoid staircase effect when pasting

If you're working in Vim and paste something into the terminal, sometimes you'll get a "staircase" effect where each line is progressively spaced farther outward, like so:

line 1
 line 2
  line 3
   line 4

Obviously, this isn't usually desirable.

To correct this, you can enable paste to prevent the staircase effect. In command mode, type:

:set paste

This isn't on by default. When paste is enabled, it disables mapping and some other functions, so you probably want to know how to turn it off as well:

:set nopaste

Highlighting

syntax highlighting for programming languages

:syntax on

Store settings

Put your most used commands, like :syntax on in ~/.vimrc