Archive

Archive for the ‘Vim Related’ Category

Vim tip 34534

May 23rd, 2011 No comments

I’ve been looking for a way to delete up to a character, but not including the character from within vim, and I finally figured out how to do it.
Lets say you have something like this:

big_huge_array_with_a_big_long_name[ indexThingy ] = 5;

If the cursor is at the beginning of the line, and we want to change the name of the array completely, we could type this:

df[

This will delete all the way to the bracket. Unfortunately, it deletes the bracket.
So instead, this works nicely to NOT remove the character you type:

dt[

This will delete every character up until the bracket, but leave the bracket. How nice.
So what does this “t” movement key do exactly?
It simply moves the cursor to the character right before the one specified, and it stays in “normal” mode.
If you wanted to immediately type after deleting the characters, you would use the following:

ct[

Which will delete the text, leave the “[” character, but change to insert mode right before the “[” character.

Another thing you can do is type:

de

Categories: Blog, Vim Related Tags:

Monthly vim tips

May 17th, 2011 No comments

If you want to search for the word right where your cursor is, you can do the following:

/Ctrl-r Ctrl-w

If you want to maximize your vertical or horizontal buffer splits, type one of the following:

Ctrl-w|
Ctrl-w_

If you want to equally resize all of your buffers, type the following:

Ctrl-w=

To make a vertical split a horizontal one, or visa versa, type the following:

Ctrl-w H
Ctrl-w K

To center the cursor on your screen, press:

zz

To bring up the build in “file explorer” window, type one of the following:

:E
:Ex
:n .
Categories: Blog, Vim Related Tags:

Resizing your vim buffer

April 28th, 2011 No comments

To resize your buffer up and down type the following:

<ctrl-w>- (that's minus)
<ctrl-w>+

To resize your buffer left and right, type the following:

<ctrl-w>< (that's a less than aligator symbol ;p )
<ctrl-w>>

You could make a mapping to do these without pressing the ctrl-w key combo first.
Something like this for the vertical stuff:

map + <c-w>+
map - <c-w>-

You have to put that in your .vimrc file.

Categories: Blog, Vim Related Tags:

How to quickly comment out entire sections of code

April 28th, 2011 No comments

Go to the first line and type the following:

^ to go to the beginning line. (probably want the top too)
Ctrl + Alt + V (to enter block highlight mode)
#j (where # is the number of lines to be commented out) Or just move the cursor manually.
I (capital i)
//
Press the esc key.
They should all be commented out now, with c++ style comments.

Categories: Blog, Vim Related Tags:

Remove trailing whitespace

April 28th, 2011 No comments

Last blog post, I mentioned you could clean up and reformat a file by using :retab.
This doesn’t remove unnecessary trailing whitespace though. To remove trailing whitespace, use the following:

:%s/\s\+$//
Categories: Blog, Vim Related Tags: ,

Quickly convert all tabs to spaces

April 26th, 2011 No comments

Make sure, in your .gvimrc file, that the following are set to your liking:

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab

Then type the following within vim:

:set expandtab
:retab
Categories: Blog, Vim Related Tags: , ,

A few more vim tips

April 13th, 2011 No comments

retrieves a yanked/deleted entry before the last yanked/deleted entry

"0p - used to paste after yanking
"1p - used to paste after deleting
"2p - used to paste after deleting (2nd from last)

Ex: yank/delete something, then yank/delete something else. Then type “0p or “1p

Count the number of words in the file of <word>

:%s/<word>//gn
[I (capital i) - Shows the lines containing the word under the cursor
ga - displays the ascii, hex, and octal value of the character under the cursor
g<CTRL-G> - to see technical info about the file you are in
CTRL-i & Ctrl-o : trace movements forward & backwards in the file
Categories: Vim Related Tags:

Changing text between brackets, parens, etc. quickly

March 24th, 2011 No comments

Vim tip: Move your cursor between any of set the following: (), “”, [], {}, etc.

Then type one of the following:  

yi(
di)
ci[

Or any combination. it will remove or change or yank all of the text between those braces.

Categories: Blog, Vim Related Tags: ,

How to reload your .gvimrc file without quitting gVim

March 16th, 2011 No comments

At work I modify my .gvimrc file, and it’s a big hassle to have to quit mvim (mac) and reload it. So a neat command you can do is to type this:

:source <path to .gvimrc>

This will reload your .gvimrc file without leaving gvim.
This should work with regular vim too.

Categories: Blog, Vim Related Tags: ,

Vim link dump

May 28th, 2010 No comments

I’ve been using Vim for quite a while and I love it. So I have placed a bunch of useful ViM links here. If you’re a programmer, I highly recommend learning Vim. Unfortunately Vim doesn’t have very good intellisense like functionality. So if you’re on linux, use NetBeans and the jvi plugin, and if you’re on windows and you want to stick with Visual Studio, get the viEmu extension (not free).

Read more…

Categories: Vim Related Tags: , , , , , ,

Vim Emulators

May 28th, 2010 2 comments

NetBeans: jvi
For VS2010, free but sucked last time I checked: VsVIM
For VS 2010/2008/2005: ViEmu

Vim reference sheet

May 28th, 2010 No comments

– HELP INFO –
:help tutor – launches vim tutor
:help – opens help window (ZZ to exit out)
Ctrl+] – jumps to tag
Ctrl+T – pops tag off the stack (goes back)

–MOTIONS–
w – until start of next word seperated by punctuation
W – until start of next word seperated by space
e – end of current word
$ – end of line
i – inside used with di> to delete contents between angle brackets, parentheses, etc.
a – like “i”, but includes brackets too
b – goes back one word, punctuation delimited
B – goes back one word, space delimited

– MISC –
esc – exits out of insert mode
:q! – exit without saving changes
#<command> – executes that command # times
q<char> – records a macro
@<char> – executes macro
q – stops recording macro
ma – marks current position as “a”
`a – recalls the marked position called “a”
:ab string substitution (abbreviation)
:una string – delete key mapping
:map – key map for command mode (unsure)
:map! – key map for insert mode (unsure)
:mapclear – clears all keymappings
Ctrl-g – displays status line
. – repeats the last delete or change command (df> for example)
:set all – shows all options
Shift-V -> move cursor -> zf – marks section for folding
zc – close fold
Space – open fold
:split – create horizontal split
:vsplit – create vertical split
Ctrl-w-<Direction> – move between buffers where direction=hjkl
:!<command> – executes external command
:copen – open compiler error buffer
:cclose – close error buffer
:set makecmd=”commandToCompile”
v – highlights by character
V – highlights by line
:make – runs make
:copen – gives list of errors in new buffer
:cclose – closes error buffer

– FILE –
:w – write changes to file
:q! – quit without saving
:wq – write changes and quit
:w>>filename – appends contents of buffer to filename
ZZ – exits and saves file
ZQ – quit without saving
:e – open file
:edit – opens new file
:enew – creates new file
:r <somethingExternal> – retrieves something, file, or command such as !ls
v<highlight>:w – writes highlighted text to file

– MOVING –
hjkl – move around
[[ - go to next function
]] – go to previous function
b – moves backward one word
w – moves forward one word
e – moves cursor to end of word
$ – move to end of line
#$ – moves to end of line # lines down
^ – moves to first non-blank character of the line
0 – moves to before the first non-blank char on the line
G – go to end of file
gg – go to top of file
#G – go to cursor on line #, if no #, it goes to EOF
Ctrl-u – scrolls up half a screen
Ctrl-d – scrolls down half a screen
Ctrl-f – scrolls forward one full screen
Ctrl-b – scrolls backward one full screen
% – goto matching parentheses
+ – move to first non-whitespace char in next line
– - move to first non-whitespace char in previous line
zt, zb, zz – move screen around cursor
H,M,L – Moves cursor to highest, middle, lowest area of screen

– EDITING –
i – insert before selected character
a – appends after selected character
A – appends at end of line
O – opens up a new line above cursor
o – opens up a new line below cursor
x – deletes character under cursor
X – deletes character to left of cursor
dd – deletes an entire line
d<motion> – delete (dw deletes a word, d3w deletes 3 words)
D – also d$, deletes to end of line
r – replace mode, types over char, for a single character only
R – replace mode, types over chars, for multiple characters
c<motion> – deletes then places user into insert mode
cc – just like dd, but puts user into insert mode
C – same as c$
u – undo
U – undo edits to entire line
Ctrl+R – redo
~ – changes a character’s case
gU – makes all character’s uppercase
d – cut (delete)
y – copy (operates identically to ‘d’)
p – pastes content after cursor
P – pastes content before cursor
>> – shifts line to right
<< – shifts line to left
3>> – shifts 3 lines to the right
>aB – moves block to left
<aB – moves block to right
J – join lines with a space
di( or yi( or ci” – yanks, changes, deletes all text between (, “, [, etc.

– STRING FILE SEARCHING –
/<string> – search forward for string
n – go forward to next found string
N – go backwards to previous found string
?<string> – search for string going backwards

Ctrl-I – goes forward to where cursor was
Ctrl-O – goes backwards to where cursor was

– SINGLE LINE SEARCHING –
t<char> – search til, like f, but stops one char before char
T<char> – search til, like F, but stops one char before char
f<char> – forward search line until <char> is reached
F<char> – backward search line until <char> is reached
, – repeat the last f,t,F, or T in opposite direction

– SEARCH & REPLACE –
:s/old/new – substitutes “old” for “new” for first occurrence
:s/old/new/g – substitutes “old” for “new” on current line
:#,#s/old/new/g – substitutes “old” for “new” between line numbers # & #
:%s/old/new/g – substitutes “old” for “new” in entire file
:%s/old/new/gc – substitutes “old” for “new” in entire file with prompt
* – searches forward for the next occurrence of the word under the cursor
# – searches backward for the previous occurrence of the word under the cursor

– Settings –
:set ic – ignore case when searching
:set noignorecase – turns off ignore case

Categories: Vim Related Tags: , , ,