Thursday, December 5, 2013

Vim - Nice little handy tips and tricks

I found some nice little tricks to perform some actions a little bit faster

Toggle toggle toggle...
If you want to set line numbers you do like this:
:set number
you can even make it a bit shorter"
:se nu

To disable line numbers:
set no number
or
set no nu

But you can also toggle by adding an exclamation mark '!'
:se nu!



Another toggle:
:se cul!
This is to put a line under the cursor, some like it, some not. I really like it!

Sources: http://learnvimscriptthehardway.stevelosh.com/


Another one:
I often like to load a bunch of files at once and when I'm done editing with a file, I'll get another buffer to edit (":h buffer" for more information). But sometimes I want 2 files next to each other, just to see if they look similar (but not identical). In that case I'd like to have 2 files open next to each other, so I want the window split in two vertical windows. This is how we can do that:
:vert sb x
Where x is the buffer number you want, assuming you already loaded a bunch of files. Now you get two windows with current buffer on the right window and buffer x on the left. Cool!

Compare 2 buffers:
Ok, so the above one creates 2 windows, each containing a buffer. Suppose you have a big file like a few thousand lines and want to check the difference, no problem with Vim. On each window you execute the command:
:diffthis
or shorter,
:difft
To turn off:
:diffoff!
With the exclamation mark, it turns off for all windows.

To jump to the next difference:
]c
read as "go to the next compare". The other way around is:
[c

To obtain the difference from the other file to the current file:
do
Note, this is per difference, so not the whole file at once. The other way around:
dp
Exercise with it, you'll love it!
By the way, I mapped diffthis and diffoff as:
nmap [Leader]d :diffthis
nmap [Leader]dd :diffoff!
replace [ with <, this blogger automatically removes '<', I guess because of security point of view (xss).



Other cute commands:
If you have multiple windows, you can close them all except the current one by this:
:only
There is also:
:hide
which closes the current window, but than I rather use Ctrl+w c, which is faster.


Follow the breadcrumbs:
One of the important things an editor should have is navigating forward and backwards my cursor positions. Say you edited a function or a method in your source code, than you jump to another function to do some other editions there or copy a snippet of code, the next thing you want to do is go back to the previous position. In normal mode you type:
Ctrl+o
for jumping back to the previous position, to jump forward again:
Ctrl+i
This is a very useful and powerful Vim command, a must-have desperately tool.


Forgot to sudo:
Very often I forget to add sudo if I want to edit a file with root permission, here is the remedy:
:w !sudo tee %
This one is hackish :)


Let's hex :
As a programmer, coder or hacker you need to check the binary data of a file. For example, you want to check the jfif segment (wiki-jpeg) of a jpeg file. To analyze the data, you can issue the command:
:%!xxd
to go back to the original view:
:%!xxd -r
So light, yet so powerful.


Load multiple files from within Vim:
Sometimes when writing code in Vim, I just need some other files to edit or maybe just to look at some code. Here's how you can do that:
:args somefolder/*.php
or if you want to browse to a folder and than load you can do the following:
:e .
This makes Vim as file browser, than you go to a certain folder, i.e. 'somefolder', than select those files with regular expression by first typing:
mr
than type:
*.php
and than type:
me
So by typing mr you open a regular-expression shell to select the matching files, than type the matching expression and than me to edit the files that match with the regular expression.


Insert date:
I like this one too, inserting date in the current file on the current cursorposition:
:r !date


Are you ready for this:
I sometimes accidentally close a file I was working on and to type
:e myfolder/subfolder/somecrazyfile.crz is just too annoying. But check out the power of Vim:
:ls!
shows you the recently closed files, than type:
:xb
and boom! Your recently closed file is back, where x is the id number of one of the closed files.

But this can be done faster! Check this, really amazing, hit the following keys:
Ctrl+o
You're back to the last cursor position in you last closed file! Amazing, just ONE key combination, amazing!
No plugins, just Vim! I'm sorry, I don't know if Emacs can do that :P


Copy/paste tricks:
I was a bit frustrated to copy a word and than replace another word with the copied one. I don't know why I didn't figured it out much earlier, but check this how to do it:
yiw is to copy the word where your cursor is on it, than do:
viwp this selects the word and than paste it with the yanked word earlier. Cool huh.
If you want to do this for another word, do this:
viw"0p meaning replace it with the word from register 0. When you yank or delete a word, the word is automatcially saved in register 0. You select the register by " and than a number. And of course, p is for paste.


When you yank a complete line with yy, you can than replace another line with:
Vp
To do this again for another line:
V"0p
I found this handy trick on: http://vim.wikia.com/wiki/Replace_a_word_with_yanked_text

No comments:

Post a Comment

NSNotification example