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

Wednesday, July 25, 2012

Vim - open a list of files from previous command


Problem : You want to execute a find command and open all the files into vim

find . -name * | xargs vim

Something not working right ?

Try
$> find . -name *
$> vim $(!!)

What just happened ?

The first command executes a "find"

The second command is opening vim and providing an argument list from a previous command. You
could also have executed

$> vim $(find . -name *)

Keywords
output command previous xargs vim to pipe

Tuesday, February 7, 2012

Using key mapping in vi

vi or vim provide a very useful technique whereby user can assign arbitrary functions to control key strokes. For eg, here is one such mapping:

map ^k I/*A*/

When a hits ctrl+k, the line on which the cursor currently is, is commented. To explain the command:
map ^k - assigns the functionality to ctrl+k character
I/*A*/ - concatenation of smaller operations which are
- Move to Esc mode,
I - Insert in the beginning of the line
/* - characters to be inserted
- Move to Esc mode
A - Append at the end of the line
*/ - characters to be appended
- move to Escape mode

The Esc key can be typed by first typing Ctrl+V followed by Esc key.

By adding this mapping to .vimrc file, this mapping can be saved across vim invocations.

Tuesday, October 4, 2011

Vim - Join Lines with a Comma (or any other character)

Step 1 : Put the desired character at the end of each line. I will use a "," as an example

:%s/$/,/g

Step 2 : Join the lines together

%j!

Wednesday, April 8, 2009

Vim - Reduce Backslashing



Appending a "\v" reduces the amount of backslashing the search expressions.

/codes\(\n\|\s\)*where : normal regexp
/\vcodes(\n|\s)*where : very magic


source :
http://vim.wikia.com/wiki/Best_Vim_Tips

Tuesday, April 7, 2009

Vim - Open Current File in Explorer or Command Prompt

Put the following two lines in your vimrc files under window to get F11 and F12 to work as "command prompt here" and "explorer here" !


map :silent !start cmd.exe /k pushd %:p:h
map :silent !start explorer.exe %:p:h


source for tip1

"http://vim.wikia.com/wiki/Open_the_folder_containing_the_currently_open_file"

source for tip2

A variant of tip 1 using windows trick of adding a registry for folders to open a command prompt 

Note : % represents the file open in current buffer

Tuesday, February 24, 2009

marks in vim - bookmarks

- Go to a line where you want a mark and press mx - this will create a bookmark

( x can be any alphabet from a-z)

- Now navigate away from that place. If you need to get back to the earlier cursor location press "`x" (back-tick and x)

- You can set multiple "marks" in a single file and navigate using back-tick and the alphabet used to create a mark

Tuesday, January 27, 2009

marks in vim - bookmarks

Go to a line where you want a mark and press mx - this will create a bookmark

( x can be any alphabet from a-z)

- Now navigate away from that place. If you need to get back to the earlier cursor location press "`x" (back-tick and x)

- You can set multiple "marks" in a single file and navigate using back-tick and the alphabet used to create a mark

Sunday, October 19, 2008

pasting code in vim

:set paste

paste the code

:set nopaste

set pastetoggle=F3 in your vimrc file is an alternative.

Friday, June 27, 2008

Vim Movements - Some Common , Some Not so much

ESC + H Moves to the line at the top of the screen.
ESC + M Moves to the line in the middle of the screen.
ESC + L Moves to the line at the bottom of the screen.


[Ctrl-F] Move forwards one page.
[Ctrl-B] Move backwards one page.
[Ctrl-D] Move forwards by half a page.
[Ctrl-U] Move backwards by half a page.
[Ctrl-E] Display one more line at the bottom of the screen.
[Ctrl-Y] Display one more line at the top of the screen.

Reference : http://en.wikibooks.org/wiki/Learning_the_vi_editor/vi_Reference#Movement

Friday, June 6, 2008

vim : ignoring white spaces in vimdiff

In your .vimrc file :
set diffopt+=iwhite


http://vim.wikia.com/wiki/Ignore_white_space_in_vimdiff

Wednesday, June 4, 2008

Appending to a register in vim

For example, you want to collect a sequence of lines into the a register. Yank the first line with:

"aY

Now move to the second line, and type:

"AY

Repeat this command for all lines. The a register now contains all those lines, in the order you yanked them.



Reference : Vim manual

Tuesday, May 6, 2008

Search Replace in all Vim Buffers


:bufdo %s/pattern/substitution/ge | update



bufdo – apply the following command to all buffers – see :help :bufdo.

%s – search and replace the whole file

g – change all instances in a line

e – avoid an error when the pattern doesn’t exist in a buffer

update – write the file only if changes were made

source : http://vim.wikia.com/wiki/Search_and_replace_in_all_buffers

Sunday, March 2, 2008

vim - stop autoindent while pasting text

On a putty terminal, when you copy-paste code from other sources, it results in a complete mess, due to vim trying to format it again. To avoid this, type

:set pastetoggle=

Now you can use to toggle between paste mode (and no paste mode).

When in paste-mode auto indent will be turned off. This is very useful when pasting text that's already indented.

source:
http://amix.dk/blog/viewEntry/19083

vim - going back in time

supported since vim 7

" Go back 1 minute
:eariler 1m

g+ and g- can be used for redo and undo ( u and CTRL + r still applies)

Monday, February 11, 2008

vim - recording keystrokes and reusing

http://www.vim.org/tips/tip.php?tip_id=144

The most useful feature that I find in VIM is the "recording" feature (:help recording). I have used this to automatically insert function headers, re-indent lines, and convert some 34 source files into HTML.

This feature is most useful when you want to do some repeated jobs, which you cant do easily using ".". You can set about writing a function, define a mapping, etc, but then these things might take time. By recording, you can try out and find the actual keystrokes that does the job.

To start recording, press "q" in normal mode followed by any of "0-9a-z". This will start recording the keystrokes to the register you choose. You can also see the word "recording" in the status(?) line. You can start the key sequences that you want to record. You can go to insert mode and type if you want.

To stop recording, press "q" in the normal mode.

To playback your keystrokes, press "@" followed by the character you choose. Pressing "@@" will repeat the same again.

Wednesday, January 30, 2008

Regular Expressions Across Multiple Lines

I was looking at this huge java file which had hundreds of SQL statements and thousands of java code interleaved like a spaghetti ! What I really needed was to analyze all the SQL statements in the file since they were all related to a single application which we were supposed to modify. I needed a mechanism to extract all the SQL statements from the file for sake of brevity.

Simple regular expressions using vim or sed did not work since they all deal with a single line, but each one of them provide a mechanism to match regular expression over multiple lines.

sed - the following command will extract all lines between a 'select' and the first ';' following that. Hence you would have printed all the sql queries from a file

sed -ne '/^select/,/;/p' SqlIntensiveFile.java

vim - using the 'g' command can achieve the same. For e.g. the following will delete all the sql queries from the file.

%g/select/,/;/d

Monday, January 14, 2008

Block Select in Vim

Go to the visual mode using "ctrl + v" instead of "v", then use the motion arrow keys *hjkl* to select the block you want.

Perform any copy/delete/edit function you would normally perform using vim ( y , d etc)!

Friday, October 12, 2007

Formatting XML in vim

":silent 1,$!xmllint --format --recover - 2>/dev/null"

http://www.vim.org/tips/tip.php?tip_id=349

Updated vim tips URL : 

http://vim.wikia.com/wiki/Format_your_xml_document_using_xmllint

Tuesday, July 10, 2007

Vim and Sqlplus

open the following file :
$ORACLE_HOME/sqlplus/admin/glogin.sql
Add following line :
DEFINE _EDITOR='gvim -c "set filetype=sql"'

Typing edit will now open your favorite editor