Favorite vim tips/tricks (preferably one per post)

184 views
Skip to first unread message

Dilawar Singh

unread,
Apr 20, 2013, 1:23:51 PM4/20/13
to wncc...@googlegroups.com
MODIFYING A READ ONLY FILE

I often realize that I was modifying a file which should only be modified by a super-user (those config files in /etc dir). Closing the file and opening again with 'sudo vim' without loosing the changes is headache. One can write the file to a temp file to some other file and then replace the original. Following shortcut does the same

    :w !sudo tee %

Give your sudo password and you are done. 'w' command (if followed by some other command) sends the buffer to that command. The command 'tee' is like a T pipe. It sends the buffer it receives to two places : stdout and to any other place mentioned by user. We have mentioned '%' which is short for current filename we are modifying. Therefore, w sends the content of file to tee and tee saves it to %. Since 'tee' is invoked by superuser therefore it can write to this file.

tee is also useful when debugging a program. One can direct the output to a text file and also see it on the terminal. More useful than 2>&1 in many cases.

PS : Terminal command vimtutor (or :help inside vim) is still an excellent way to learn vim.

--
Dilawar

Navin Chandak

unread,
Apr 20, 2013, 1:28:48 PM4/20/13
to wncc...@googlegroups.com
A  cpp file can be indented perfectly by using the = command...
If you are at the start of a file and type '=G' it indents the whole file.. Any navigation command can follow the = command.


--
--
The website for the club is http://stab-iitb.org/wncc
To post to this group, send email to wncc...@googlegroups.com
 
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ashwin Paranjape

unread,
Apr 20, 2013, 1:35:06 PM4/20/13
to wncc...@googlegroups.com
The wonderful dot command
=====================

Let's see a concrete example. Say you have declared three new functions in your header file, and you need to implement them in the module. You copy the following text and paste it into your implementation file:
All code starts like this, doesn't it?

Now you have to remove their semicolons, and adding an empty body would be a good idea. With the cursor as above, you can use 'A' to go straight into insert mode at the end of the line:
After 'A' - see the insert mode cursor (vertical line) at the end

Now you delete the semicolon with <backspace>:
Deleting is simple

And type <return> { <return> } <return> to insert the body:
Inserting too, as in any other editor

And finally, type <Esc> to return to normal mode:
And now, quickly!, we return to normal mode

And now you have to repeat this with the other two. How do you do it? Easy: first, press 'j' to move the cursor down. And then, press '.' to repeat the last full editing command (the 'A' command with the backspace and the inserted text). If you do 'j.j.', that is, twice, you get the following:
Just j.j. to do this!

The vi command architecture was key here, together with the fact that <backspace> is as part of the editing sequence as regular typing, and the editing operations being pretty repetitive. But think about it, how much of your daily editing is repetitive? Yeah, I thought so.

#copiedButUseful
--
Ashwin P. Paranajape
Manager, Technovation
Visiting Scientist IST Austria
3rd year CSE Btech.
IIT Bombay

Piyush Kumar

unread,
Apr 20, 2013, 1:36:01 PM4/20/13
to wncc...@googlegroups.com
Vim editor also supports sessions just like a browser like firefox.

If you have 8-10 tabs having > 30 files open in vim [if you're working on a project of reasonably big size] , and you have to reboot your PC or restart x server for that matter, it can get really irritating to close vim, and then open all files again .

Vim session solve exactly this problem. When you have to close your vim for any such thing, you can just do

:mksession session.vim

This will store everything in current vim session including all open files, all visible buffers, window sizes and keyboard mappings etc.

Now when you want to restore vim from this particular session, you can just do

command_prompt$ vim -S session.vim

This will restore everything just the way it was.



Regards,
Piyush Kumar
Third year Undergraduate
Computer Science Department
IIT Bombay

Piyush Kumar

unread,
Apr 20, 2013, 1:36:37 PM4/20/13
to wncc...@googlegroups.com
Tired of vim complaints of command not found for accidental :Q or :W ? Vim user commands can help here.
Place this in your ~/.vimrc file

command! -bang -nargs=* -complete=file E e<bang> <args>
command! -bang -nargs=* -complete=file W w<bang> <args>
command! -bang -nargs=* -complete=file Wq wq<bang> <args>
command! -bang -nargs=* -complete=file WQ wq<bang> <args>
command! -bang Wa wa<bang>
command! -bang WA wa<bang>
command! -bang Q q<bang>
command! -bang QA qa<bang>
command! -bang Qa qa<bang>


Regards,
Piyush Kumar
Third year Undergraduate
Computer Science Department
IIT Bombay



Piyush Kumar

unread,
Apr 20, 2013, 1:39:17 PM4/20/13
to wncc...@googlegroups.com
While we're at it, why not post on the group Ashwin made

Dilawar Singh

unread,
Apr 20, 2013, 2:40:55 PM4/20/13
to wncc...@googlegroups.com
Well, I personally have nothing against facebook but it is not the place which programmers usually search. Search engines would not return FB as results either. Besides, there are so many other things on FB. I seriously doubts if anyone will be willing to read there anything more than 40 words. 'Like' or sharing without reading beats the whole purpose of a group. IM(not so)HO.

--
Dilawar

Pritam Baral

unread,
Apr 20, 2013, 2:43:31 PM4/20/13
to wncc...@googlegroups.com
+1

WnCC is publicly visible. A forum like StackOverflow is even better, since it's meant (and designed) to be browsed.


 

Regards,
Chhatoi Pritam Baral

Ashwin Paranjape

unread,
Apr 20, 2013, 3:02:51 PM4/20/13
to wncc...@googlegroups.com
^ & ^^
It was simply for fun that the page was created. To connect fellow vimmers/could be vimmers, whom I knew and share the best of our experiences, after all VIM means something different for each one of us. It was supposed be "tip of the day kind-of" and about integrating something new everyday. It was supposed to be neither an indexed blog for tips nor a forum for fastest keystroke combinations (and needless to say there are quite a lot of them). I found that many of my friends were still (arguably) struggling with gedit, and charity begins at home. Never had aims of conquering the world.

Let's not digress, let the tricks flow in!!

Sudarshan Wadkar

unread,
Apr 20, 2013, 4:07:31 PM4/20/13
to wncc...@googlegroups.com
:x

-S

Harsh Gupta

unread,
Apr 20, 2013, 6:34:09 PM4/20/13
to wncc...@googlegroups.com
We can have some website like http://www.spojbot.com/ (it mails spoj problems) which will mail regular vim tips from http://vim.wikia.com or any other website. Though useful but large number of tips at once doesn't have much impact. It would be interesting if we could come out with some test to judge the skill level of the vim user and send them tips according to their skill level.
Harsh

Rajendra Kumar Solanki

unread,
Apr 21, 2013, 12:00:21 AM4/21/13
to wncc...@googlegroups.com
My favorite copy/paste or move a block of code:

- take cursor on first char of the block and set a marker say 'm'
mm

- move cursor to position right after last char of block
- copy block
"by`m 
"b - define buffer, y - yank or copy, `m - move to marker

- or cut block
"bd`m 
"b - define buffer, d - cut/delete, `m - move to marker

- paste to some other place
"bp




Thanks and Regards
Rajendra Kumar Solanki

CSE | IIT Bombay | +91-9167520033

Rajendra Kumar Solanki

unread,
Apr 21, 2013, 12:07:52 AM4/21/13
to wncc_iitb
To get rid of control char (e.g. ^M) in a file:

:%s/^M//gc

Trick is how to print ^M correctly in command mode, simple caret (^) won't help here. 

First press control+v (this will give ^sign) and then press control+m - you will get ^M, replace m with any character. 



Thanks and Regards
Rajendra Kumar Solanki

CSE | IIT Bombay | +91-9167520033


Pranav Jawale

unread,
Apr 21, 2013, 3:07:28 AM4/21/13
to Web and Coding Club IITB
Insert line numbers:

:%s/^/\=offset+line('.')/

offset can be any number, if you dont want numbering to start form 1.



On Sun, Apr 21, 2013 at 12:07 AM, Rajendra Kumar Solanki
*
*
The best way to get something done is to begin. ~Author Unknown

Dilawar Singh

unread,
Apr 23, 2013, 5:07:23 PM4/23/13
to wncc...@googlegroups.com
  • Ctrl+z sends the vim to background. Terminal becomes free and can be used to run the compiled program or to do something else. 'fg' brings back the vim to foreground. To check how may processes are in background, type 'jobs'. Issuing 'vim' again does not start the old process. A swap file .swp is created whenever you do something naughty with vim, you will be asked 'what to do with it' next time you launch vim.
  • One can also use ':!command' where 'command' is any shell command.
  • It is always beneficial to enable auto-complete to save you some some typing. I like supertab. Disable 'dictionary complete' when programming. It can make vim really slow. 'set complete-=k'.
  • A good terminal should have anti-aliasing of fonts. xfce4-terminal is my favorite with 'Liberation mono' fonts. A good programming font should contrast between alphabets and numbers and special character. And most importantly, one must be able to see difference between 0 and O easily. Fixed-width are also good candidate.
  • It is good to treat _ as a keyword. 'set iskeyword+=_' in vimrc.
  • There is a plugin called Alternate which makes you switch between header and source file very easily. Nerdtree can also be used.
  • ctags are incredibly helpful when browsing multiple source file for a particular function.

On Mon, Apr 22, 2013 at 8:22 AM, Skand Hurkat <skand...@gmail.com> wrote:
Should be done with dos2unix. The ^M appears because DOS files end with CRLF whereas Unix files end with LF.

Skand.

aayush singhal

unread,
Apr 23, 2013, 8:11:01 PM4/23/13
to wncc...@googlegroups.com
How to change scrolling speed in vim ?
When I go up/down using j/k keys, i want vim to move faster through the lines. How can i change that speed ?

Dilawar

unread,
Apr 24, 2013, 12:35:37 AM4/24/13
to wncc...@googlegroups.com
I doubt if single keystrokes can be made faster without changing the pc.
You sure there is nothing in your vimrc which might be making it slower?

If you type :help in vim, and read the first chapter 'moving around', you will
find great many other keys which can be using to move fast. My fav is shift+]
and shift+[ which moves one block up/down.

Here is a blog which summaries few tips. In short 10j moves 10 lines down.

http://jeffkreeftmeijer.com/2012/relative-line-numbers-in-vim-for-super-fast-movement/

If you have to go to and fro to some fixed locations, you should try marks. Go
to a place and type ma, this place is marked 'a'. From anywhere else, you can
type 'a to go to this location. If capital letters are used such that A, B,
etc, then you can move across the files. It is incredibly useful when your
function is in some other file and you wish to change some other function in
some other file.

--
Dilawar
EE, IITB

subhasish saha

unread,
Apr 24, 2013, 12:44:57 AM4/24/13
to wncc...@googlegroups.com
Forward with lesser speed : <Ctrl>+d
Forward with more speed : <Ctrl> + f
Backward with lesser speed : <Ctrl> + u
Backward with more speed : <Ctrl> + b


To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.


--
--
The website for the club is http://stab-iitb.org/wncc
To post to this group, send email to wncc...@googlegroups.com

--- You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.





--
Thanks & regards,
Subhasish

aayush singhal

unread,
Apr 24, 2013, 4:11:22 AM4/24/13
to wncc...@googlegroups.com
It may be possible. But 2 years back, when I was coding like noobs in a slow vim, a guy changed something in .vimrc and that particular speed increased. 

Dilawar

unread,
Apr 24, 2013, 5:41:19 PM4/24/13
to wncc...@googlegroups.com
Check if 'set noloadplugins' (in ~/.vimrc) makes it faster. If yes, then
attach your vimrc or paste is on pastebin etc.

--
Dilawar
EE, IITB
>--
>--
>The website for the club is http://stab-iitb.org/wncc
>To post to this group, send email to wncc...@googlegroups.com
>
>---
>You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
>To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.

Sudarshan Wadkar

unread,
Apr 25, 2013, 2:07:38 PM4/25/13
to wncc...@googlegroups.com
On Wed, Apr 24, 2013 at 2:37 AM, Dilawar Singh <dilawar....@gmail.com> wrote:
  • A good terminal should have anti-aliasing of fonts. xfce4-terminal is my favorite with 'Liberation mono' fonts. A good programming font should contrast between alphabets and numbers and special character. And most importantly, one must be able to see difference between 0 and O easily. Fixed-width are also good candidate.
You may wanna check out Anonymous Pro , it's free BTW. Do email-attachments work on Google Groups? I use the Minus 1.0.1 release which doesn't have bitmapped at lower font sizes and looks neat even at a size of 9

As for the tip, umm-
ggVG=
works most of the time (if consistent new lines for opening brackets after control statements)

-Sudhi :)

Pranav Jawale

unread,
Apr 27, 2013, 4:31:53 AM4/27/13
to Web and Coding Club IITB
Reach matching braces by typing % 

Add noremap % v%  .vimrc to jump the "matching" object (source: http://vim.wikia.com/wiki/Moving_to_matching_braces)

Pritam Baral

unread,
May 24, 2013, 9:28:54 AM5/24/13
to wncc...@googlegroups.com
Collaborative editing: https://github.com/FredKSchott/CoVim.

While there are other ways (read: hacks) possible (tmux, floobits, cloud9,) none are native to Vim.

 

Regards,
Chhatoi Pritam Baral



On Sat, Apr 27, 2013 at 2:01 PM, Pranav Jawale <pranav...@gmail.com> wrote:
Reach matching braces by typing % 

Add noremap % v%  .vimrc to jump the "matching" object (source: http://vim.wikia.com/wiki/Moving_to_matching_braces)

--
--
The website for the club is http://stab-iitb.org/wncc
To post to this group, send email to wncc...@googlegroups.com
 
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.

Harsh Gupta

unread,
May 27, 2013, 9:07:42 AM5/27/13
to wncc...@googlegroups.com
Using google docs with Vim.

With a tool call googlecl we can access google docs and other google services through command line.
And it is now possible to edit your documents on google docs with vim or emacs.


--
Harsh

Gelli Ravikumar

unread,
May 28, 2013, 11:53:55 AM5/28/13
to wncc...@googlegroups.com
Thanks for the post. I have installed the googlecl as: sudo pip install googlecl (link). It has been installed now. Then, I've tested it with the command (link): $ google docs edit "Shopping list". I have got the following error. Can any one help please.

============================
Traceback (most recent call last):
  File "/usr/local/bin/google", line 849, in <module>
    main()
  File "/usr/local/bin/google", line 835, in main
    run_once(options, args)
  File "/usr/local/bin/google", line 540, in run_once
    options.config)
  File "/usr/local/bin/google", line 373, in import_service
    service_module = import_at_runtime('googlecl.' + service + '.client')
  File "/usr/local/bin/google", line 332, in import_at_runtime
    return __import__(module, globals(), fromlist=['0'])
  File "/usr/local/lib/python2.7/dist-packages/googlecl/docs/client.py", line 47, in <module>
    googlecl.client.BaseClientCL):
  File "/usr/local/lib/python2.7/dist-packages/googlecl/docs/client.py", line 55, in DocsClientCL
    DOCLIST_FEED_URI = gdata.docs.client.DOCLIST_FEED_URI
AttributeError: 'module' object has no attribute 'DOCLIST_FEED_URI'

================================

--
with Regards
----------------------------------------------------
Gelli Ravikumar
Research Scholar (Ph.D),
Field Computations Lab,
Dept. of Electrical Engineering,
IIT Bombay, Powai, Mumbai 400076
Ph: 022-2576 4424, 089 765 983 96

Gandhian Young Technological Innovation Award - 2013
POSOCO Power System Award: PPSA - 2013
IITB PhD Executive Member - 2013

Dilawar

unread,
Jul 5, 2013, 2:18:30 PM7/5/13
to wncc...@googlegroups.com
GITHUB BASED PLUGIN MANAGEMENT SYSTEM

Installing vim-plugins to ~/.vim is eoften asy. If a plugin creates problems, there is no easy way to disable it. Two plugins management system, 'pathogen' and 'vundle', are worth exploring. I prefer vundle because of its simplicity. I recently started using vundle and found it clean and powerful. Using either patogen and vundle requires some knowledge of git.

Sometimes one needs to customize a plugin a bit to suit his style of coding. It is useful to 'fork' repo on such plugin on github.

Details are straight-forward and available on vundle page https://github.com/gmarik/vundle

You need to configure your git to use http/https proxy.

Dilawar

unread,
Oct 23, 2013, 10:05:10 AM10/23/13
to wncc...@googlegroups.com
To draw ASCII-ART in vim, use this

https://github.com/vim-scripts/DrawIt

 Normal installation (without vundle)

http://www.vim.org/scripts/script.php?script_id=40

--
Dilawar
Reply all
Reply to author
Forward
0 new messages