History Report a problem
Article Edit this page Discussion

The super star

From Vim Tips Wiki

(Redirected from VimTip1)
Jump to: navigation, search

Tip 1 Previous TipNext Tip

Created: February 24, 2001 Complexity: basic Author: Scott+Yegappan Minimum version: 5.7 Karma: 5333/1765 Imported from: Tip#1


In normal mode, move the cursor to any word. How do you search for the next occurrence of that word?

Press the * key to search forwards for the current word, or press # to search backwards.

Using * or # searches for the exact word at the cursor (searching for rain would not find rainbow).

Use g* or g# if you don't want to search for the exact word.

[edit] Using the mouse

With the proper settings, you can search for an exact word using the mouse.

Shift-LeftClick a word to search forwards, or Shift-RightClick to search backwards.

This needs a GUI version of Vim (gvim), or a console Vim that accepts a mouse. You may need the following line in your vimrc to enable mouse searches:

:set mousemodel=extend

[edit] More searching

After searching, press n (next) to search again, or press N to search in the reverse direction.

Vim maintains a search history – see Using command-line history. Type / or ? and use the arrow up/down keys to recall previous search patterns. You can edit a pattern, and press Enter to search for something different.

Suppose the cursor is on a word, and you want to search for a similar word.

Press / then <C-r><C-w> to copy the current word to the command line. You can now edit the search pattern and press Enter. Use <C-r><C-w> for a <cword>, or <C-r><C-a> for a <cWORD>.

<C-r><C-w> is Control-R then Control-W (hold down Ctrl and press r; release all keys; hold down Ctrl and press w).

After searching, use <C-o> to jump back to your previous position (then <C-i> will jump forwards).

After searching, an empty search pattern will repeat the last search. This works with /, :s and :g.

So, after searching for a word, use :%s//new/g to change all occurrences to 'new', or :g/ to list all lines containing the word. See Substitute last search.

[edit] See also

[edit] References

[edit] Comments

On UK keyboards, <Shift>-<3> works just like # for searching backwards. That's because <Shift>-<3> gives # on most other countries keyboards, although it gives the pound currency symbol on UK keyboards. Touch typists should appreciate this because it is the left-hand equivalent of how you type * with <Shift>-<8>, whereas the # on UK keyboards is way over next to the return key.

[edit] Highlight matches without moving

I find it very useful to highlight the word under the cursor like *, but without jumping directly to the next match.

This way, you can quickly see the highlights in the current page without scrolling anything. Then you can move to the first (ggn), last (GN), next (n) or previous (N) match as usual.

The basic command is:

:let @/="<C-r><C-w>"<CR>

And I have set it as follows in my vimrc:

map <F10> :set hls<CR>:let @/="<C-r><C-w>"<CR>
map <F11> :nohls<CR>

This way, F10 highlights and F11 unhighlights.

Here is another version that uses F10 to toggle search highlighting (F11 is not used).

map <F10> :set invhls<CR>:let @/="<C-r><C-w>"<CR>/<BS>

The 'inv' prefix on a boolean setting toggles it. The trailing '/<BS>' clears the command line.

Just position the cursor to an interesting word, press F10, and all occurrences of that word are highlighted. Use commands like n and N to search up and down. When you're done, press F10 again and the highlighting is off.


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pixar
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Flash Gordon
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"The super star"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation


.