Forum: DSL Tips and Tricks
Topic: Vim tips
started by: jpeters
Posted by jpeters on Dec. 10 2007,08:11Using Vim has saved me endless hours recently (writing a testing program with lots of repetitous entering of data into arrays). One of the features that really came in handy was block visual mode (in 7.1) <control-v>. For example, highlight a column in specified rows (e.g., "45 <down arrow>", and change them (e.g., "r somekey").
Recording repetitious keystrokes was also a blessing. Start recording with "q somekey" and end with "q". Repeat with "n @ somekey", where 'n' is number of repetitions and 'somekey' is the buffer you initially chose. Other very helpful features:
1. Search and replace (example: :467,695 s/old/replace/ )
2. Set markers: (example: "mc") and return to marked line (example 'c )
3. Being able to specify repetition of any operation by putting a number in front (e.g. , "3yy" "45p" pastes 3 lines 45 times)
4. The ability to abandon the mouse completely. A mouse is great for a two minute task, but using the keyboard is much quicker. Vim has key assignments for everything.
Posted by lucky13 on Dec. 11 2007,00:21jpeters, I don't know if you saw my question in the Nov testing thread. I wanted to know if your extension is patched or the plain 7.1 sources. I'd already compiled 7.1-unpatched on my hard drive install with the intention of copying the patches from my FreeBSD ports directory and recompiling when I saw your extension had been submitted.
In addition to the things you mentioned, I like how easy it is to work on larger files with folding.
< http://www.linux.com/articles/114138 >
Here's one of my fave "cheatsheets":
< http://www.rayninfo.co.uk/vimtips.html >
One of the cool things about vim is that it can be extended and scripted to handle a variety of tasks. One of my favorites from vimscripts is potwiki, a personal plaintext wiki (which is easy to convert to whichever markup language):
< http://www.vim.org/scripts/script.php?script_id=1018 >
I've also used vimpress (WordPress blogging tool script) but I'm too addicted to using DeepestSender for my blogging.
< http://www.vim.org/scripts/script.php?script_id=1953 >
(and just for fun... try
Posted by mikshaw on Dec. 11 2007,02:28
One thing I find appealing about projects that are as ubiquitous as Vim is that they sometimes have their own official documentation domain:
< http://vimdoc.sourceforge.net/ >
Posted by lucky13 on Dec. 11 2007,03:55
I see one of the links is this:
< http://www.moolenaar.net/habits.html >
I came across his video presentation of that on Google/youtube a few months ago if anyone's interested. It's 80 minutes.
< http://youtube.com/watch?v=eX9m3g5J-XA >
Posted by jpeters on Dec. 11 2007,06:30
vim_7.1.unc was compiled from source. Sorry, I didn't see your question. I'm completely burned out right now completing a testing program in TCLTK, so will check out your links later.
'Insert' isn't used with visual mode, so you're just switching out of visual mode into regular command mode. You can do the same thing by just hitting "v" again. Visual mode is for blocking out text for cut & paste, replace functions, etc.
Posted by mikshaw on Dec. 11 2007,14:06
Highlight a vertical line
Type something (it will first appear on only one line)
After thinking about it, it seems the Shift would be necessary to distinguish between inserting in block mode and switching out.
Posted by jpeters on Dec. 11 2007,15:26
I guess I don't
<shift-i> takes you out of visual mode and into regular insert command mode. To get back to block visual mode, you'd have to <exc> out of insert and then <ctr v> again. (at least that's the way it works in 7.1)
Posted by mikshaw on Dec. 11 2007,21:25I don't know about 7.1; I'm still using 6.3
As it appears to me, the above series of commands does indeed take you out of visual block mode as you said, but not before inserting the typed text on every line that was marked with the visual block.
Posted by jpeters on Dec. 11 2007,23:38
Hey....that works! Although the highlighting goes off and and shifts out of visual mode, the esc key indeed writes to the previously highlighted column. That's very cool....Thanks for finding that little gem! Having the visual mode is much faster that using the automated key function, with the added advantage that you don't have to know the exact number of lines to repeat (because you can see and adjust the highlighted column before running the task).
(BTW....I'm not sure how you found that trick....you're not reading the manual, are you? )
Edit: That certainly was a timely tip; saved me at least an hour this evening!!
Posted by jpeters on Dec. 12 2007,06:23
I'm also going through it. First thing I spotted:
" :'a,'bg/fred/s/dick/joe/igc VERY USEFUL "
I like the 'fold' feature, which saves opening up several copies and getting a lot of .swp files, etc.. Thanks for the links!
Posted by mikshaw on Dec. 12 2007,14:14Some miscellaneous stuff that I've found useful on many occasions....
Splits the window and opens filename.
:r!<command> add the output of command to the cursor location. It's not always pretty.
Move cursor to the first non-blank character of the current line (is better than Home/0 in many cases)
Go to the first non-blank character of line <number>.
When cursor is over a number, Ctrl+a increases the number, Ctrl+x decreases.
I'm sure there were more that I use a lot, and some more unusual, but I haven't waken up yet =op
Posted by lucky13 on Dec. 12 2007,17:12
That's my favorite way for making and editing playlists and other lists.
:r! find [appropriate flags/paths] | grep [content I want]
So for all my Schubert MP3s (my mp3s are all lower case)
:r! find ~/ | grep schubert
Then edit anything that needs to be fixed and save.
Posted by jpeters on Dec. 12 2007,18:12Vim's power also has it's downside. For example, I've found it difficult to correct a bug in a function when there's another copy of it pasted somewhere else in my code
Posted by mikshaw on Dec. 14 2007,14:22
In any case, I don't understand why you would have the same function twice in the same code anyway. The whole reason behind using functions is to prevent the need for duplicate code.
Posted by jpeters on Dec. 14 2007,20:33
The point was that it's easy to inadvertantly push the "wrong" key during the process of coding, with unexpected results. That happened to me recently. After a half hour of trying to figure out why a simple function wouldn't work, I found a copy of it pasted way down in my code!
Posted by mikshaw on Dec. 14 2007,23:55Ah, yeah.
I think the thing I most often have trouble with as a result of using Vim is when using other editors. Many times a string of text gets started with "i", or ends with :wq =o)
Posted by WDef on Dec. 18 2007,15:26Clearly I need to work on my vim chops as I'm missing out on all this. All I ever use it for is a quick config file edit.
Posted by jpeters on Dec. 19 2007,18:12I was looking through lucky's cheatlink for a way to search & replace using wild characters (* or ?) Example:
search "F1 F2 F3 F4....
replace "F(1) F(2) F(3) F(4)
It's probably there somewhere, but couldn't find anything.
Posted by ^thehatsrule^ on Dec. 19 2007,19:27Could try something with regexp, such as 's/F\([0-9]\)/F(\1)/g'
Of course, if you needed it more than 1 digit, you could use an additional +
Posted by lucky13 on Dec. 19 2007,19:41
Grrr, off the top of my head I can figure out the search. The replace string needs work (e.g., it's changing F1 to F(d)). Off to google...
This can't be too difficult.
Edit: Especially if you do what hats suggested. The digit range (\d) will work for [0-9].
Posted by jpeters on Dec. 20 2007,00:51
I'll play around with this when I get some time, or else enquire on a vim forum.
That was about the first thing I tried, but didn't work.
Posted by ^thehatsrule^ on Dec. 20 2007,01:15What doesn't work? Perhaps the text you require replacing is different?
Posted by jpeters on Dec. 20 2007,02:35
I didn't know you expected it to work as is. As posted, it doesn't match. Eliminating the first set of (), it yields an illegal back ref error.
Edit: I'm checking through a few pages now on pattern matching in vim.
< http://www.vim.org/htmldoc/pattern.html >
EDIT: Hmm...Now it works! Thanks HATS....I have no idea why it didn't work before, since I pasted it in. (I guess I needed to go through the page I posted to see WHY it works before it would work.....)
note: perhaps I hadn't included the range previously....
EDIT: The '+' doesn't seem to work for getting additional digits, but a '*' does. Thus, '1,20s/F\([0-9]*\)/F(\1)/g' ,for replacing in lines 1-20.
EDIT: After trying it out further, I found that it's also putting () after every 'F' that doesn't have a number. e.g, F(100) F()r some F()cking reason.
Posted by jpeters on Dec. 21 2007,02:36Here's a work-around, although there must be a better way!
Posted by ^thehatsrule^ on Dec. 21 2007,02:46You have to escape the + with a \
The * wildcard stands for zero or more instances of a group.
EDIT: that link you posted explains that as well
Posted by jpeters on Dec. 21 2007,03:30
I just wanted to make sure you read it!
Well, here it is:
\1\@<=,\([a-z]\+\) ",abc" in "abc,abc"
Once again, you nailed it! I just created 5,346 array variables.
Thanks, Hats. (I'll dance at your wedding!)
Posted by jpeters on Feb. 12 2008,04:24I was just playing with GVIM; this sure makes exploring vim's power more accessible. Split with "explorer," convert to HTML, etc, etc. This surely isn't Notepad!!
Posted by lucky13 on May 28 2008,23:46This thread has been dormant too long. Here are a couple vim plugins that other DSL users might find useful. I don't know if anyone's tried them or remarked on them before in the forums.
lua-support: This adds a separate menu to turn vim into a lua IDE. Included in the menu are sections for comments, conditional statements, regex options, run, etc. The run menu includes syntax check.
vim script site:
< http://www.vim.org/scripts/script.php?script_id=1763 >
plugin homepage screenshots:
< http://lug.fh-swf.de/vim/vim-lua/ >
bash-support: This does the same as the above but for BASH.
< http://vim.sourceforge.net/scripts/script.php?script_id=365 >
shots: < http://lug.fh-swf.de/vim/vim-bash/screenshots-en.html >
There's also a perl version:
< http://vim.sourceforge.net/scripts/script.php?script_id=556 >
< http://vim.sourceforge.net/scripts/script.php?script_id=213 >
These aren't too terribly large, especially compared to the size of the full/normal install of vim 7.1-current.
Here's my own screenshot with part of the lua menu opened:
< http://lucky13linux.files.wordpress.com/2008/05/screen-lua-menu.png >