I'm trying to figure out why the same key pattern viw
appears to behave differently in separate contexts
I have two buffers where the cursor is [ ]
In one a javascript file:
import {[f]oo, bang} from './utilities'
visual selection: foo
The other, :help
"inner word", select [count] words (see |word|).
White space between words is counted too.
When used in Visual linewise mode "iw" switches to
Visual characterwise [m]ode.
visual selection: mode.
I'm confused why in the first the inner-word excludes the punctuation character comma ,
and the second includes the punctuation .
What accounts for this inconsistency?
Yes (well not technically, the definition does not change, because it is per definition variable). Vim is (if configured to be) filetype sensitive. see :h filetype.
So EVERY setting can be different in a different filetype.
The one you are looking for is iskeyword. This controlls which characters are word boundaries. :h iskeyword states:
For a help file it is set to all non-blank printable characters except '*', '"' and '|' (so that CTRL-] on a command finds the help for that command).
So . belongs to the word, like a-z
Related
In vim insert mode, if there is a word insert, and if I typed ins, then type ctrl-p, vim will auto complete the word to insert.
I remember in my last PC if I type data.ins then type ctrl-p, vim will auto complete the word to data.insert, but since I bought a new PC, vim does not auto complete data.ins, how to fix this?
By the way if I type b on t of the word data.insert, vim will jump back to d, instead of i, I need to jump to i
I suspect that the dot character somehow got to be a part of the iskeyword option. To verify this, you can look at the output of :set iskeyword? (note the final questionmark).
To fix this temporarily, you can do :set iskeyword-=.. To fix this permanently, find out where iskeyword is changed in your .vimrc.
The option iskeyword determines what characters Vim considers to be part of a "word". With a dot inside it, string like data.insert will be considered a single word, meaning you jump over them with a single w/b/e/... motion. Ctrl-p autocompletion looks whether the substring typed until now is part of a "word" in the rest of the document. Since you hadn't typed data.insert before, it doesn't find anything starting with data.ins. When you remove the dot from iskeyword, it will only look if something started with ins (like insert).
After you removed the dot, if you find yourself occasionally wanting to jump over the entire dotted name, you can use Vim's concept of WORDs (see :h WORD). You jump over WORDs using W/B/E/...
The ab family of command (iab and cab) could be a huge time saver. However, it is yet unclear when will it be triggered. For example, with the following settings in _vimrc:
iab sj <c-r>=strftime("20%y-%m-%d %H:%M:%S")<cr>
cab cab drop C:\users\llinfeng\dropbox
For c and i being "mode-indicator", a complete time-tag will be inserted while in Insert mode if one presses <space> (or <ESC>, <C-[>) right after typing sj. For the second mapping, the full directory will be inserted in Command-line mode if one hits \ (or <space>) right after typing drop.
So, it seems that, for iab, the following characters will trigger the expansion of an abbreviation defined through iab-mapping:
<space>
<esc> (or escape in general)
Ideally, one may guess the <tab> key shall also trigger such expansion. However, for my case (set expandtab tabstop=4 shiftwidth=4, i.e. one "tab" key == 4 spaces"), no expansion would have been triggered.
Then, for cab, the following may work to trigger the expansion:
\
<space>
??
What is confusing to me is that: what is the thing "laying at the core" that triggers the expansion of an abbreviation defined through ab-mapping? And, does there exist a complete list of keys that could trigger such expansion, for both Insert mode and Command-line mode?
Thank you!
All the best,
-Linfeng
It's all exhaustively documented in the vimdocs:
http://vimdoc.sourceforge.net/htmldoc/map.html#abbreviations
Specifically:
An abbreviation is only recognized when you type a non-keyword character.
This can also be the <Esc> that ends insert mode or the <CR> that ends a
command. The non-keyword character which ends the abbreviation is inserted
after the expanded abbreviation. An exception to this is the character <C-]>,
which is used to expand an abbreviation without inserting any extra
characters.
But it goes on a good few paragraphs detailing precisely what conditions trigger different types of abbreviations. Specifically watch out for the difference between
full-id (foo, g3, -1)
end-id (#i, ..f, $/7)
non-id (def#, 4/7$)
abbreviations
Is there something like % (that represents the whole buffer in Vim command line) for the current selection, so that I can do something like: :# sort (Imagine # represents the selection).
EDIT:
Sorry, I missed to mention that I am requesting for a way to operate on block selections not on ordinary selections that can be operated using ranges '<,'>.
Yes. Example:
:'<,'>!sort
The range :'<,'> represents the visually selected lines.
:* is shorthand for :'<,'>
If you hit : while in visual mode it will start the command with '<,'>
For more help see:
:h '<
:h v_:
:h range
You are probably looking for the marks '< and '>:
:'<,'>sort
If you just select a few lines and hit : to enter the command line these marks should appear automatically.
As others have already remarked, the visual selection is represented by the '<,'> range (there's also the :* short form). However, as all ranges, this always covers entire lines, even if the selection is only characters or a block.
Most Ex commands (like :sort) only operate on full lines, for :substitute, you can limit the effects to the visual selection by including the special \%V atom in the search pattern, cp. :help /\%V.
The idea to make Ex commands handle blockwise selections is old, but unlikely to be tackled any time soon.
When I edit XML-files I sometimes want to jump to the next occurrence of text between quotes. For example, when my cursor is on my.attr in attr="my.attr" I want to jump to the next occurence of my.attr. I want to do it via some key combination (like Shift + * which is for words occurrences). Is is possible?
You can create a visual selection of the attribute value inside double quotes with vi". Then, there are several plugins that implement the * command for visual mode (usually by overloading the * command), i.e. they search for the next occurrence of the selected text. One such plugin is my SearchHighlighting plugin. (The plugin page has links to alternative plugins.)
Related plugins
If you want to change all attribute values (without constructing a :%s/ substitution), my ChangeGlobally plugin provides a gc{motion} and {Visual}gc command that does that.
I would yank the text inside the quotes with yi" (only works if the opening and closing quotes are on the same line) and then /<C-R>". (The <C-R> means CTRL-R, not 5 characters.)
This gives you a chance to modify the pattern before submitting it; as #Kalanidhi pointed out, you may have to escape some special characters. It uses the same i" text object as in #Ingo Karkat's answer.
If your text is short, then you can edit the command line with the arrow keys, but if it is long you may want to edit it in a command-line window with <C-F>. (Alternatively, if you are thinking ahead, use q/ instead of /.)
:help y
:help text-objects
:help c_CTRL-R
:help cmdline-editing
:help cmdline-window
You can use in command mode type /<exact pattern> if any special character then escape the special character like \
For example In command mode /"my\.attr"
So only search the exact pattern. N or n to move forward and backward .
When programming/writing I heavily use word-wise commands, for example "move to the left/right by one word", "delete next/last word" by pressing Ctrl (+left,backspace...).
The problem I have is, when the text I am editing contains symbols which will not be recognized as words, therefore ctrl + right will jump over a sequence of symbols AND a regular word after that.
Ideally I want to be able to set the delimiting characters for word-wise operations to space, tab, newline and opening and closing brackets - maybe also arithmetic operators (similar to how Eclipse handles it).
I am using Linux. Do you know any way how to change my settings system-wide or alternatively for xterm and (g)vim individually to achieve this?
Most likely, system-wide won't work. VIM is easy, you can set the characters that define the identifier using the iskeyword setting. In your case, there is too much in it, and you need to remove the ones you do need, or redefine it by adding the ones you do want. eg: :set isk=9,32,50-51
This will set keyword detection to spaces, tabs and parentheses.
However, in VIM you can jump based on word and WORDs, where the first is defined by the abovementioned iskeyword setting, while the latter will jump over all non-blank characters. Maybe, that's the motion you want. You can read more about this in the help (:help w).
Instead of holding down the control key and pressing the left/right cursor keys, why not use Vim's normal mode word motion commands?
w/W - move to start of next word/WORD
e/E - move to end of next word/WORD
b/B - move to beginning of previous word/WORD
ge/gE - move to end of previous word/WORD
You can read up on the difference between a word and a WORD by running :help word.