Saari Development

Ali Rizvi's Technical Blog as a Professional Software Development Engineer

Archive for the ‘vim’ Category

Vim: Using Ruby Regular Expression

leave a comment »

I needed to remove line number from a web copy-pasted code snippet. I know there is a way to configure vim to use the perl style regular expressions but I couldn’t find how quickly.

What I found was:
http://vim.wikia.com/wiki/Perl_compatible_regular_expressions

:rubydo gsub /pattern/,'replacement'</code>

Here is my specific command:

:rubydo gsub /^\s+\d{0,3}\s/, ''

Pre-requisite: look for +ruby in :ver output (if it is -ruby then it would not work).

The above web page also talks about Perl regular expression in Vim and different ways to do that.

Written by imsaar

August 28, 2013 at 2:17 pm

Posted in ruby, vim

Vim: Set vim filetype to ruby in a Treetop grammar file

leave a comment »

While writing a syntax parser using ruby treetop library for a work project I discovered the need to use ruby filetype for syntax highlighting etc a file ending in .treetop (treetop uses polyglot to look for and compile the grammar on runtime but the grammar file has to end in .treetop).

Here is a nice getting started tutorial for treetop: http://po-ru.com/diary/getting-started-with-treetop/

I could have used some configuration in my .vimrc file to associate this file extension with ruby filetype but I want to try the embedded configuration which I have seen other used but never used myself.

After some search I found the reference article below and was by just adding the following file in the .treetop file I was able to desired functionality:

# vim:filetype=ruby

Ref:Embedding vim Settings in the File You’re Editing

Written by imsaar

June 7, 2010 at 4:21 pm

Posted in editor, vim

Config: .vimrc (v2)

leave a comment »

” Plugins

” FuzzyFinder
http://www.vim.org/scripts/script.php?script_id=1984

" FufFileRecursive
"
http://intraspirit.net/scratchpad/a-simple-fuzzyfinder-improvement/

" MatchIt for multi-character match on % (def and end)
”  REF: http://awesomeful.net/posts/57-small-collection-of-useful-vim-tricks
”  http://www.vim.org/scripts/script.php?script_id=39

” Rails.vim
http://www.vim.org/scripts/script.php?script_id=1567

" Ali Rizvi's Vim Settings

set number
set shiftwidth=4

set incsearch
set hlsearch
set textwidth=80

” from lindes:
syntax on
hi Comment term=bold ctermfg=Cyan guifg=Cyan
set autoindent

” from benji fisher to turn on the matchit plugin automatically and more
filetype plugin on

” explicity map file extension .t to perl syntax instead of tads
” which is autodetected by filetype plugin on
” This line should always be after filetype plugin
autocmd BufNewFile,BufRead *.t set syntax=perl

” to show real tabs and spaces in file
set list
set listchars=tab:>-,trail:^,eol:$
“set listchars=tab:>-,trail:-

“to add spaces instead of tabs
set expandtab

” evil, bad! — hard tabs should be 8 chars… –lindes
” set tabstop=4
” _but_ we can use this:
set smarttab

“make the background light
set background=light

“show matching parens
set showmatch

“show row and column number
set ruler

” allow backspacing over everything in insert mode
set backspace=indent,eol,start

filetype plugin indent on

“set shiftwidth to 2 for ruby only
autocmd FileType ruby setlocal sw=2

“shortcuts inspired by http://weblog.jamisbuck.org/2008/11/17/vim-follow-up

let g:fuzzy_ignore = “*.log”
let g:fuzzy_matching_limit = 70
map fb :FufBuffer
map fd :FufDir<CR>
map ff :FufFile<CR>
” addition based on http://intraspirit.net/scratchpad/a-simple-fuzzyfinder-improvement/
map ff :FufFileRecursive<CR>
map fm :FufMruFile<CR>

silent execute '!mkdir -p ~/.vim_backups'
set backupdir=~/.vim_backups//
set directory=~/.vim_backups//

map <F2> :mksession! ~/vim_session <cr> " Quick write session with F2
map <F3> :source ~/vim_session <cr>     " And load session with F3

Written by imsaar

February 6, 2010 at 10:19 pm

Posted in configuration, editor, vim

Vim: Tabs like firefox in non-gui vim

leave a comment »

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).

Written by imsaar

October 14, 2009 at 10:17 pm

Posted in editor, vim

Vim: Converting to JavaCase to ruby_case

leave a comment »

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!

Written by imsaar

September 17, 2009 at 3:51 pm

Posted in editor, regex, vim

Vim: Auto indenting based on filetype

with one comment

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!

Written by imsaar

January 27, 2009 at 1:34 am

Posted in editor, vim

Vim: Inserting output of unix/linux command

leave a comment »

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.

Written by imsaar

December 24, 2007 at 12:48 am

Posted in editor, vim