Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
5 total  XML / RSS feed 

vi find/replaces for adding a table prefix to a SQL dump

Was importing a DB but wanted to prefix the tables, but the file was too large for TextMate. Here's all the vim substitutions

%s/DROP TABLE IF EXISTS `/DROP TABLE IF EXISTS `prefix_/g
%s/CREATE TABLE `/CREATE TABLE `prefix_/g
%s/INSERT INTO `/INSERT INTO `prefix_/g
%s/LOCK TABLES `/LOCK TABLES `prefix_/g
%s/ALTER TABLE `/ALTER TABLE `prefix_/g

Open existing vim in iTerm

// open existing vim in iTerm
//
// experimenting with something like this
// as a modification to RunVim
//
// http://www.fastnlight.com/runvim/

tell application "iTerm"
        activate
        set _term_list to every terminal
        repeat with _term in _term_list
                tell _term
                        try
                                select session "Vim"
                                select _term
                                set _vim_tty to tty of session "Vim"
                                try --check to see if vim is even running
                                        do shell script "ps -t " & _vim_tty & " | grep vim"
                                        
                                        try --check to see if vim is foreground
                                                do shell script "ps -t " & _vim_tty & " -o command,state | grep vim.*+"
                                        on error --vim is suspended or something
                                                --check for vim as job %+
                                                tell i term application "System Events"
                                                        key code 6 using control down
                                                        delay 0.1
                                                        key code 14 using control down
                                                        key code 32 using control down
                                                end tell
                                                tell session "Vim"
                                                        write text "jobs | grep ]+.*vim"
                                                end tell
                                                delay 0.1
                                                set fulltext to text of session "Vim"
                                                set fulltext to quoted form of fulltext
                                                set lastline to do shell script "echo " & fulltext & " | tail -n 2 | head -n 1"
                                                set lastline to quoted form of lastline
                                                try --check %+
                                                        do shell script "echo " & lastline & " | grep -v jobs"
                                                        tell session "Vim"
                                                                write text "fg %+"
                                                        end tell
                                                on error --vim is not %+ job
                                                        
                                                        --check to see if vim is %- job
                                                        tell session "Vim"
                                                                write text "jobs | grep ]-.*vim"
                                                        end tell
                                                        delay 0.1
                                                        set fulltext to text of session "Vim"
                                                        set fulltext to quoted form of fulltext
                                                        set lastline to do shell script "echo " & fulltext & " | tail -n 2 | head -n 1"
                                                        set lastline to quoted form of lastline
                                                        try --check %-
                                                                do shell script "echo " & lastline & " | grep -v jobs"
                                                                tell session "Vim"
                                                                        write text "fg %-"
                                                                end tell
                                                        on error --vim is not %- job; just pick the first one in jobs list
                                                                
                                                                tell session "Vim"
                                                                        write text "jobs | grep -m 1 vim"
                                                                end tell
                                                                delay 0.1
                                                                set fulltext to text of session "Vim"
                                                                set fulltext to quoted form of fulltext
                                                                set lastline to do shell script "echo " & fulltext & " | tail -n 2 | head -n 1"
                                                                set lastline to quoted form of lastline
                                                                set jobnumber to do shell script "echo " & lastline & " | sed 's/[^0-9]//g'"
                                                                tell session "Vim"
                                                                        write text "fg " & jobnumber
                                                                end tell
                                                                
                                                        end try --%-
                                                end try --%+
                                                
                                        end try --vim foreground
                                        -- escape in case Vim was not in command mode
                                        tell i term application "System Events"
                                                key code 53
                                        end tell
                                        tell session "Vim"
                                                write text ":enew"
                                        end tell
                                on error -- vim not running
                                        tell i term application "System Events"
                                                key code 6 using control down
                                                delay 0.1
                                                key code 14 using control down
                                                key code 32 using control down
                                        end tell
                                        tell session "Vim"
                                                write text "vim; exit"
                                        end tell
                                end try
                                exit repeat
                        end try
                end tell
        end repeat
        tell _term
                try
                        select session "Vim"
                on error --no existing Vim session
                        launch session "Default Session"
                        set name of the last session to "Vim"
                        tell the last session
                                write text "vim; exit"
                        end tell
                end try
        end tell
end tell

How to count particular words in vim

From one of the comments in http://www.vim.org/tips/tip.php?tip_id=689

:%s/word/&/g


The above substitutes "word" for itself, and tells you the number of substitutions made.

VIM Tips

Quick Tips

* To quickly get out of command-line, press ctrl+c, it's faster than hitting ESC multiple times.
* ~ changes case of current letter.
* zb, zt, and zz scrolls the screen to make the current line at the top, bottom, or middle.
*
* to auto-indent a piece of code, highlight it in visual mode, and press =, to auto-indent the current line, press ==.
* Use gq to wrap the highlighted peice of text.
* Use :set wrap and :set wrap! to toggle long line wrapping.
* To visually check the difference between two files, use vimdiff file1 file2, it provides a nice color-highlighted view with code folding, much better than plain diff.
*
* Type :help to access Vim's help, and :help cmd for information on the command cmd.
* vimtutor is Vim newcomer's friend, it provides a course on Vim usage.


// insert code here..

shell/vim .rc's for Japanese support (UTF-8)

Get Japanese (and other multibyte, ascii-unfriendly languages) working in the Terminal.

.inputrc (bash)
set convert-meta off
set meta-flag on
set output-meta on


.cshrc (tcsh)
set dspmbyte=utf8


.vimrc
:set enc=utf-8
:set fenc=utf-8


And don't forget 'ls -w' or 'ls -v' to display files and directories.

Preferences: Uncheck 'Emulation > Escape non-ASCII characters';

More at Apple Support - Topic: Displaying foreign characters in the Terminal command line.
« Newer Snippets
Older Snippets »
5 total  XML / RSS feed