Archive for the ‘editor’ Category
Vim: Tabs like firefox in non-gui vim
This might be old news for some but today I discovered firefox like tabs (not to be confused with tab character for indenting) in vim (not gvim).
To open a file in a new tab while inside vim simply do:
:tabe filename
To open all files in a directory in vim tabs from command line do:
vim -p dir_name/*
To move around in tabs in command mode you do:
gt
It is hard to google for them as it is easy to confuse them with tab character. Here is the online doc page for them:
http://vimdoc.sourceforge.net/htmldoc/tabpage.html
The current tab was setup to be not underlined vs other tabs were underline I discovered the way to show current tab in reverse:
:highlight TabLineSel cterm=reverse
Of course the options are endless and when I shared this discovery with Arnab he told me there is an entire chapter about this in the swroopch’s Vim book actually it one section in his Book.
Thank you Daniel Wong for introducing me to vim tabs. It was instant pay off on encouraging Daniel to use either use a real programmers’ editor like Vim or Emacs instead of gedit and teaching him the basic Vim (once he choose my choice of text editor on his own, I did not pressure him, honest).
Vim: Converting to JavaCase to ruby_case
This morning I was in a mode to do a regex kata and thought it would be interesting to see if I can substitute CamelCase (I call it JavaCase for fun) to underscore_separated_words (I did a search and could not find a good word for this so) I will call it ruby_case.
Here is how I started (note in my setting of vim you have to escape parens \):
:%s/\([a-z]\)\([A-Z]\)/\1_\2/gc
The above matches lowercase letter followed by uppercase letter and captures them then replaces them with underscore between them.
:%s/\(\l\)\(\u\)/\1_\2/gc
I found that Vim has metacharacters for upper (\u) and lowercase (\l). So above become cleaner version of the previous, call it refactoring. (JavaCase => Java_Case)
:%s/\(\l\)\(\u\)/\1_\L\2/gc
I found how to change an uppercase capture to lowercase. But this was leaving first character of the word still uppercase (JavaCase => Java_case)
:%s/\(\<\u\).*\(\l\)\(\u\)/\L\1\2_\L\3/gc
In this attempt to capture the first letter and lower case it I lost the letters in between. (JavaCase => ja_case)
:%s/\(\<\u.*\)\(\l\)\(\u\)/\L\1\2_\L\3/gc
Finally, I got what I wanted. (JavaCase => java_case) Hurray!
But I notice the match is greedy, so if there are multiple CamelCase on the same line this would fail.
For now I will leave this as an exercise for the reader as I am out of time for now, when I have a solution I will add it here.
This was a fun little exercise and I enjoyed it very much. Now I can find and solve a problem like this every morning it will jump start the day.
Reference: Vim Regular Expression 101 (http://www.geocities.com/volontir/)
Cheers!
Vim: Auto indenting based on filetype
I had two different behaviors on my windows and linux machine when I used Vim to edit my ruby files.
I liked the auto-indent behavior I had on my windows machine but I did not know how to make it happen on my other linux machine.
I finally invested sometime to find out the difference between the two:
At first I thought I was simply missing
set autoindent
but I verified that I had that in both of my vimrc files.
The difference was that on my windows machine I was using the vimrc_example file which came with this line:
" load indent files, to automatically do language-dependent indenting. filetype plugin indent on
This did the trick and I am so happy now.
Now when I start an if block and hit enter the second line starts with the appropriate indentation and when I type end the editor automatically indents it to the previous indentation level of the appropriate block of code.
Cheers!
Vim: Inserting output of unix/linux command
I had seen other vim experts do this but couldn’t remember how they did. Today I finally search and found how to insert the output of a unix/linux command directly in a vim buffer(without copy-pasting).
:r!ls /home/
or in the visual mode (by pressing V) simply !ls /home/
Simple isn’t it and quite useful at times.
Vim: Delete every line in the file that does not match a pattern
I realized the power of :g much later than many other commands in vim and I think this one has been there even in vi for a long time.
Here is a great tip that covers the virtues of :g in vim:
The reason I ended up with this tip was that I needed to find a simple command that would delete all lines matching a pattern.
I knew how to delete all lines that matched a pattern.
:g/pattern/d
but I was looking for the inverse and I found it :g’s cousin :v in the above tip as a user comment.
:v/pattern/d
did the job very well. Once again vim saved the day.
Vim: Turn off gVim toolbar
I don’t know about you but I don’t like my gVim real estate to be polluted by toolbar under the menus so in my early days of using gVim I researched and discovered a way to turn them off.
Here is what you need to put in your .gvimrc (or _gvimrc on windows):
" To set the toolbars off (icons on top of the screen)set guioptions-=T
Vim: Setting height and width of gVim window
I always struggled with this on my windows Vim and today I saw the fix on a vim users mailing list addressing this exact question.
Here is the way to set height (lines) of gVim (graphical Vim) window:
:set lines=40
and here is how to set width (columns) of gVim windows:
:set columns=80