sponsor Vim development Vim logo Vim Book Ad

words_tools.vim : Alternatives to expand(

 script karma  Rating 15/6, Downloaded by 1949  Comments, bugs, improvements  Vim wiki

created by
Luc Hermitte
 
script type
utility
 
description
A tip would have been more accurate regarding what I am presenting here. But as some functions proposed are needed by another script of mine, here is a script file containing the definitions of three functions that can be used instead of expand("<cword>").

expand("<cword>") is a nice feature in VIM, but it is not very helpful when we have to deal with automatic completion -- you know : when you hit <tab> within VIM's command line or your favourite shell. Indeed, "<cword>" refers to the word under/after the cursor, while we need the word under/before the cursor.

So, here is three functions :
* GetPreviousWord() that returns the word preceding the cursor ; the word is either truncated at the cursor's position or completed by as many spaces as needed to reach this position. I call a word, a sequence of keyword characters ('\k', see :h isk).
  It is handy, in C programming, to test what precedes the cursor when we hit '(', and then expand C control-structures (for, if, else, elif, switch, etc.) to anything desired.

* GetCurrentWord() returns the word which is either stricly under the cursor or just before -- if the cursor is preceded by a space, an empty string is returned. Here, a word is a sequence of non white space characters : '\S' -- in order to expand regular expressions.
  Cf. http://vim.sourceforge.net/scripts/script.php?script_id=46 v2.2

* GetNearestWord() returns the word ('\S') either under, just before or just after the cursor.

You can take a look at a table showing the different returned values by this functions in equivalent contexts : http://hermitte.free.fr/vim/general.php3#expl_words_tools

As I said before, a tip would have been more appropriate. These functions are just examples of want could be done on this topic. They fulfill some specific needs I had.
 
install details
Drop it in your macros folder and source it when needed.

NB: this "library" is now distributed as part of lh-vim-lib: http://www.vim.org/scripts/script.php?script_id=214
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
words_tools.vim 1.0 2001-11-13 5.7 Luc Hermitte Initial upload
ip used for rating: 142.132.191.50

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github