Hidden features of Vim - vim

Over the years, I thought I'm a Vim master! Recently I visited a real Vim master! oops! My knowledge is awfully superficial!
For example I didn't know it's possible to add a \c to make search case insensitive. (instead of :set ignorecase)
I clearly remember when how I'm surprised when I found SuperTab or TagList plugins first time. Vim's official site says "Vim isn't an editor designed to hold its users' hands. It is a tool, the use of which must be learned.", so naturally it should have many undiscovered features.
So I'm asking, what are your favorite features of Vim? What are things you can do with it that you can't or are more difficult in the other editors?
Of course there's some same topics about other editors:
Hidden Features of Eclipse
Hidden Features of TextPad

About the "hidden" part... Try these:
:help 42
:help!
:o)

It took me a few years before I learned about text objects
:help text-object
:nmap cw ciw
:nmap cW ciW
Also nice are ci" and ci (when I want to change a string or function args).

This is not exactly a hidden feature but it's a little known feature.
If you type :X then you can encrypt your file with a password.

You can check the correctness of the text using line:
:set spell

Related

How to autocomplete file paths in Vim, just like in zsh?

In Zsh, I can use filename completion with slashes to target a file deep in my source tree. For instance if I type:
vim s/w/t/u/f >TAB<
zsh replaces the pattern with:
vim src/wp-contents/themes/us/functions.php
What I'd like is to be able to target files the same way at the Vim command line, so that typing
:vi s/w/t/u/f >TAB<
will autocomplete to:
:vi src/wp-contents/themes/us/functions.php
I'm trying to parse the Vim docs for wildmode, but I don't see what settings would give me this. It's doing autocompletion for individual filenames, but not file paths. Does Vim support this natively? Or how can I customize the autocomplete algorithm for files?
Thanks for any advice!
-mykle-
I couldn't find a plugin to do this, so I wrote one. It's called vim-zsh-path-completion. It does what you're looking for, although via <C-s> rather than <Tab>. You can use it with <Tab> for even more control over what matches, though.
It's got bugs, but for basic paths without spaces/special characters, it should work. I think it's useful enough in its current state to be helpful. I hope to iron out the bugs and clean up the code, but I figured I'd start soliciting feedback now.
Thanks for the idea!
Original (wrong) answer, but with some useful information about Vim's wildmode.
Put the following in your .vimrc:
set wildmenu
set wildmode=list:longest
That will complete to the longest unique match on <Tab>, including appending a / and descending into directories where appropriate. If there are multiple matches, it will show a list of matches for what you've entered so far. Then you can type more characters and <Tab> again to complete.
I prefer the following setting, which completes to the first unique match on <Tab>, and then pops up a menu if you hit <Tab> again, which you can navigate with the arrow keys and hit enter to select from:
set wildmode=list:longest,list:full
Check out :help wildmenu and :help wildmode. You might also want to set wildignore to a list of patterns to ignore when completing. I have mine as:
set wildignore=.git,*.swp,*/tmp/*
Vim doesn't have such a feature by default. The closest buil-in feature is the wildmenu/wildmode combo but it's still very different.
A quick look at the script section of vim.org didn't return anything but I didn't look too far: you should dig further. Maybe it's there, somewhere.
Did you try Command-T, LustyExplorer, FuzzyFinder, CtrlP or one of the many similar plugins?
I use CtrlP and fuzzy matching can be done on filepath or filename. When done on filepath, I can use the keysequence below to open src/wp-contents/themes/us/functions.php (assuming functions.php is the only file under us that starts with a f):
,f " my custom mapping for the :CtrlP command
swtuf<CR>
edit
In thinking about a possible solution I'm afraid I was a little myopic. I was focused on your exact requirements but Vim has cool tricks when it comes to opening files!
The :e[dit] command accepts two types of wildcards: * is like the * you would use in your shell and ** means "any subdirectory".
So it's entirely possible to do:
:e s*/w*/t*/u*/f*<Tab>
or something like:
:e **/us/f<Tab>
or even:
:e **/fun<Tab>
Combined with the wildmode settings in Jim's answer, I think you have got a pretty powerful file navigation tool, here.

Is there a way to move within a file in Vim similar to Emacs' Isearch?

I've been using Vim for some time and I feel that I'm finally becoming somehow 'fluent' with it, but some of the feature listings, videos and other stuff I've seen (particularly Tim Visher's Vimgolf in Emacs series) really convinced me to give Emacs a try.
So for the past week, I've been using Emacs almost exclusively. I really miss some concepts I'm familiar with from Vim (mostly the action-movement style, things like ci" etc.), but one thing I really learned to love in Emacs is the way to move around a file with Isearch.
For example, if I wanted to move to a line where a function fn is called, and there were 2 other instances of 'fn' between the point and the position I want to move to, I'd do C-s fn C-s C-s. If I wanted to do the same in Vim, I'd have to do something like /fn <CR> n n :noh <CR>, which is nowhere as nice, so I'd probably just check the line number and do #G wwwww....
So my question is: is there a way to emulate the efficiency of Emacs' movement with search in Vim? It doesn't have to use search, I'm just looking for something other than the cumbersome go-to-line and forward several words I described above.
Edit: Item 4 in Effective Emacs describes nicely what I'm trying to accomplish.
What is described in your link is exactly my primary mean of movement in Vim: /foo<CR>nn for incremental forward search and ?bar<CR>nn for incremental backward search.
I don't see what is more efficient in <C-s>foo<C-s><C-s> and <C-r>foo<C-r><C-r>: that's even more keystrokes!
Anyway, I think that :set hlsearch is your problem. If all the matches are contained in the viewport that's OK but, as soon as some matches are hidden it becomes useless. I don't have set hlsearch so /foo<CR> is quite the opposite of inneficient for me.
Well, another problem is having (at least) three functions with the same name in the same file…
Vim has wonderful text-ojects: } or ]m that are very useful when looking at some code for the first time.
You can emulate Emacs with this keymap: cnoremap <C-s> <Cr>/<C-p>.
Though Vim has better ways for moving around than searching. Have you tried ctags?
I also recommend the EasyMotion plugin for quickly moving around in the visible area of the text.
I think you're looking for
set incsearch
you can couple it with
set hlsearch
for even better results.
Also check out "*", it's pretty awesome.

Configure tab to show list of variants instead of cycling through in VIM

When opening new buffer it VIM, I type:
new /path/to/fi
If I hit "tab" at this point it cycles through files. How to configure VIM to show list of variants instead of going for the first one?
set wildmenu
Is all you need to add to your .vimrc. Read :help wildmenu.
Set your wildmode setting to something different, for example
set wildmode=list:longest
If I misunderstood the question completely, yell ... :)
(This is not a direct answer to your question, but I think it's even better :)
You should check out the Command-T plugin, inspired by TextMate's 'Go To File'. It filters out possible combinations very intelligently, just type a few characters of each subdirectory enough to distinguish it and it 'gets' it, the characters don't have to be at the beginning and can don't have to be sequential. It also shows you a list of options left.
I realize this is a terrible explanation so check out this video to see how it works.
The downside is it requires Vim to be compiled with Ruby support.
Control-P (ctrlp.vim) is a replacement for Command-T written in VimScript, so it doesn't require Ruby.

How do I use VIM effectively for editing English text?

As a programmer by heart, if not by profession, I increasingly rely on, nay live in VIM for most editing-related tasks. What tips can you offer for using (almost) everyone's favorite editor for editing general-purpose text, say, an article? I mean plain text, with minimal markup using Markdown or RST; I'm not looking for support for LaTeX or for entering mathematical formulae.
I enable soft-wrapping when I'm editing most text files:
:set wrap
If you decide to do the same, then you'll want to know about gj and gk in normal mode, to move by screen lines instead of physical lines. I use them so often I remapped the up and down arrow keys to them instead of k and j.
Whether you're editing hard- or soft-wrapped files, you'll get a lot of mileage out of gqap (or its cousin gwap) to re-wrap a single paragraph with hard newlines, and vipJ to join all the lines of a hard-wrapped paragraph back into a single line.
You might also want to consider including a in formatoptions, to enable automatic paragraph formatting:
:set formatoptions+=a
When you're doing all this wrapping and unwrapping, it's nice to keep Vim from mangling numbered lists:
:set formatoptions+=n
In fact, I'd suggest reviewing all the formatoptions and adjusting them to your particular preferences:
:help fo-table
More info:
:help gj
:help gk
:help gqap
:help auto-format
:help formatoptions
Spell checking:
:setlocal spell spelllang=en_us
" ]s moves to the next mispelled word
" [s moves to the previous mispelled word
Set a thesuarus for Vim
Use par to format text
It's not very well maintained, but the Vim-Outliner project makes Vim into a killer outliner for plain text writing. You can download v0.34 here (there's a more recent version, I think, but I'm not sure where to get it):
http://www.vimoutliner.org/postnuke-phoenix-0.7.2.3/html/modules.php?op=modload&name=Downloads&file=index&req=viewdownload&cid=4
I really enjoyed this blogpost about writing better with latex. You could use vim-latex :) It's more about writing better, than just editing english text though.
http://matt.might.net/articles/shell-scripts-for-passive-voice-weasel-words-duplicates/
Use insert abbreviations:
iabbr teh the
iabbr i I
iabbr definately definitely
Edit, another tip:
set wrap nolist linebreak
This tells vim to wrap lines that are too long with "visual" newlines rather than adding an actual newline character to the file. The 'list' option must be off because it automatically disables the 'linebreak' option.

What Vim features are missing in Emacs with Viper and Vimpulse?

There are important features of Emacs which are missing in Vim, such as the comint mode, and there are no scripts/plugins which can replace them.
There are also benefits of Vim over Emacs, such as modal editing and generally better default shortcuts. However, Viper mode gets me both. Vimpulse also enables visual mode.
Unfortunately, no mode can make Emacs work as fast as Vim.
So I mostly learned Vim-in-Emacs. What Vim features am I missing?
I'm a regular vim user but not an emacs user and haven't used vimpulse to confirm this.
But browsing the vimpulse code below I see no mention of text objects.
http://www.emacswiki.org/cgi-bin/wiki/vimpulse.el
Vim introduced some very useful idioms for selection and movement that aren't in regular vi
cat 'cut around tag' for xml/html tags
diw 'delete in word'
di( di" delete text within a "" () etc.
in the vim help see :he text-objects for more.
Unfortunately, no mode can make Emacs work as fast as Vim.
If you mean that the emacs startup time is poor, you can cut it down dramatically by having an emacs session permanently open, and use gnuclient/emacsclient whenever you need to edit a file.
http://www.emacswiki.org/emacs/GnuClient
One vi feature that I miss in emacs is the . (dot) command that repeats the last edit. There is dot-mode.el which implements C-. to simulate this but it didn't always work for me (I am not proficient in elisp to figure out why).
Even viper doesn't implement this command exactly like vi does (last time I checked), and even if it does, I don't think that is enough for me to start using viper.
To get a complete list of what vimpulse may be missing type
:helpgrep not in Vi
Every feature in the help that is not vi compatible will be listed.
Theres about 700 matches.
Of course there will be a lot of duplicated functionality
Vi motion everywhere - in Vi you can always navigate using the same shortcuts, whether you are inside a directory listing, a help file, a write only file or something you're allowed to edit.
Correct visual and delete behaviour - very similar to emacs marking but idiomatically different, so difficult for a Vi user to retrain. If you're already used to the emacs marking you're not really missing anything.
Correct escape behaviour - should cancel all other modal activity and return you to command mode. Again, if you're comfortable in emacs you're probably used to having modal inputs and such anyway.
The obvious chestnuts about Vi being deployed on every nix system in the world (Gentoo purists correct me?) - not a very strong argument in my eyes anyway.
Those are the ones that sprang out during my use of emacs+viper.
Vim macros don't seem to work with viper/vimpulse:
Vim macros don't work when using viper + vimpulse in Emacs
(Btw, the killer emacs feature that makes me care about vim/vimpulse is that different frames can edit the same file. It's like having a split where the two pieces can be in different windows. If anyone knows how to accomplish that in vim, you'd be my hero.)
Incremental search using / and ? is missing.
Being a lover of emacs as an OS and vi as an input model, naturally I wanted to love viper. However within two seconds of using it I had to give it up: viper does not support C-[ as a substitute for the escape key–which is an essential shortcut for the most frequently used command in vi.

Resources