I am a new user of vim (gvim in windows), and have found abbreviations a nice time saver - however they would be even better if i could stop the trailing whitespace at times.
I have some directories that i use a lot, and so i added some abbreviation/path pairs to my _vimrc:
:ab diR1 C:/dirA/dira/dir1/
:ab diR2 C:/dirA/dirb/dir2/
etc ...
Now when i type diR1 <space>
i get C:/dirA/dira/dir1/[]|
where the whitespace is represented by []
and the cursor is the |
character. I would like to get rid of the []
== whitespace.
This is a minor complaint: however you seem to be able to customise everthing else in Vim so i figured i'd ask -- is it possible to avoid the trailing whitespace when one uses abbreviations in vim?
An alternate tool used within Vim is a good answer - my objective is to save re-typing frequently used directory structures, but to have the cursor handy as i would almost always add something to the end, such as myFile.txt
.
The trailing white space (doubtless due to the fact that the space
triggered the abbreviation) which i backspace over before adding myFile.txt
to the end is less annoying than typing the whole thing over and over, but it would be ideal if i could avoid doing so ...
Every time the user issues a :w command, Vim will automatically remove all trailing whitespace before saving.
vim just to use this plugin and am happy with my life, all things considered. Show activity on this post. Actually this is the other way round, tab option is used to display a character when a tab character is inserted (\t) instead of spaces. And trail is use to show trailing spaces at the end of lines.
Automatically add abbreviations in a file vimrc file, and read the comments. Basically, you have to download the file located here and to copy it in the relevant directory. Then, select a word or an expression in visual mode, and push Shift+F8.
pb2q answer is exactly what you want in your current scenario, but does not fully answer the question presented in the title. This exact problem is addressed in the vim help file. See :helpgrep Eatchar
. The example it gives is this:
You can even do more complicated things. For example, to consume the space
typed after an abbreviation: >
func Eatchar(pat)
let c = nr2char(getchar(0))
return (c =~ a:pat) ? '' : c
endfunc
iabbr <silent> if if ()<Left><C-R>=Eatchar('\s')<CR>
You would put the Eatchar
function in your ~/.vimrc
file and then use like so in your abbreviations:
iabbr <silent> diR1 C:/dirA/dira/dir1/<c-r>=Eatchar('\m\s\<bar>/')<cr>
This would "eat" any trailing white space character or a slash. Note that I used iabbr
instead of just abbr
, because it is rare to actually want abbreviations to expand in command line mode. You must be careful with abbreviations in command line mode as they will expand in unexpected places such as searches and input() commands.
For more information see:
:h abbreviations
:helpgrep Eatchar
:h :helpgrep
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With