Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

Monday, May 7, 2012

Search and replace words in multiple files in Vim

Upgraded my project's factory girl and got some nasty warnings about Factory(:xxx) and Factory.create(:xxx) syntax being deprecated. I've been using the former for almost all of the specs so needed an easy way to search and replace words in multiple files in Vim.

:args app/models/*
:argdo %s/Factory(:/FactoryGirl.create(:/ge | update

Tuesday, January 31, 2012

Show whitespace characters in Vim

This toggles showing the whitespaces

:set list!

Friday, January 14, 2011

Sort lines in Vim


:{range}sort u
:{range}sort! u

Thursday, March 4, 2010

Vim can't make backup file

Ensure that the backup directory in .vimrc exists.

# in .vimrc

set backup
set backupdir=~/.vim/backups

mkdir -p .vim/backups

Vim unable to open swap file

If you see the following message when opening a vim file

Unable to open swap file for "{filename}", recovery impossible

The directory specified in your vim for swap files is missing. In my case it's ~/.vim/tmp.

# in .vimrc
set directory=~/.vim/tmp

mkdir -p ~/.vim/tmp

Sunday, February 21, 2010

Open files at line number in Vim


:e PATH_TO_FILE:LINE_NO

Eg.
:e app/models/user.rb:10

Sunday, October 18, 2009

Deleting a word backwards in Bash and Vim


# To delete one word back
CTRL + w

# To paste deleted word
CTRL + y

Sunday, October 11, 2009

Record & playback a Vim macro


# q{register} when in normal mode will start recording into register
qs

# q will stop recording
q

# Playback = @{register}
@s

Friday, October 2, 2009

Reload vimrc in Vim


# :so or :source
:so ~/.vimrc

Insert contents from clipboard into Vim


# This usually works in insert mode
SHIFT + INSERT

# However sometimes my "CTRL + c" on other application doesn't get contents into Vim's "clipboard".
# The solution is to use Vim's multiline insert mode

:i or :in or :insert

# This will bring up lines which you can paste from Ubuntu's clipboard by "CTRL + SHIFT + v" to you heart's content.
# Finish the multilines with a "." at the end
# NOTE: better to turn off Vim's "autoindent" with "!"

:in!

This is first line
This is 2nd line
This is last line
.

# A similar method would be append instead of insert

:a, :append

Yank all lines in Vim

3 methods.

# 1. Highlight select all lines before yanking
:0, SHIFT + v, G, y

# 2. State range to yank: G - the last line
:0,Gy

# 3. State range to yank: $ - end of text
:0,$y

Sunday, September 27, 2009

Best built in Vim color theme for Rails


# Seems like Evening's the best
colorscheme Evening

Friday, September 25, 2009

% and # in Vim

# '%' represents current filename in buffer
# Eg. insert current file contents in Vim
:!!cat %

# '#' represents previous filename in buffer
# Eg. git commit current and previous file
:! git commit -m "Message for commit" % #

Thursday, September 24, 2009

Insert shell output into Vim


# Insert date
!!date

# Insert directory listing
!!ls -l

Thursday, September 17, 2009

Copy into global clipboard in Vim


# Yanking in Vim will only copy into Vim buffer
y

# Get into command history mode and copy into clipboard instead
q:
"+yy

Monday, September 14, 2009

Execute shell commands in Vim

# :! to execute shell commands directly in Vim
:! git status

# % specifies current file
:! git commit -m "Added changes" %

Thursday, September 10, 2009

Highlight line in Vim

# Highlight line that cursor is on
SHIFT + v

# Highlight line and then tab it
SHIFT + v
>

Open shell in Vim

# Open shell inside vim to do shell operations
:shell

# Exit shell
exit

Autocomplete in Vim

# Autocomplete string in buffer
CTRL + p

# Autocomplete line in buffer
CTRL + x + l

Tuesday, August 18, 2009

Search and Replace in VIM

Syntax

:s/search/replace/
:[range]s/search/replace/

Examples

# Search & replace in current line
:s/entry/interview

# Search & replace in lines 8 to 10
:8,10 s/search/replace/

# Global search & replace
:%s/entry/interview/g

# Global search & replace with prompt
:%s/entry/interview/gc

More awesome search and replace tips at http://linux.com/learn/tutorials/8255-vim-tips-the-basics-of-search-and-replace