check if only whitespaces before cursor in current line

2 views
Skip to first unread message

TheLonelyStar

unread,
Jul 8, 2009, 8:39:53 AM7/8/09
to vim...@googlegroups.com

Hi.

I want to bind "tab" to autocompletion in python mode.
But when I am on a line, where there are only are whitespaces in front of
the cursor, autocompletion does not make sense.
Can I somehow detect this situation and insert an "tab" in that case?

Thanks!
Nathan
--
View this message in context: http://www.nabble.com/check-if-only-whitespaces-before-cursor-in-current-line-tp24390733p24390733.html
Sent from the Vim - General mailing list archive at Nabble.com.

TheLonelyStar

unread,
Jul 9, 2009, 9:39:41 AM7/9/09
to vim...@googlegroups.com

Can no pne help we with this?
I can get the current line:

let x=line(".")

But how do I check if there are only whitespaces before cursor?

Thanks!
Nathan


TheLonelyStar wrote:
>
> Hi.
>
> I want to bind "tab" to autocompletion in python mode.
> But when I am on a line, where there are only are whitespaces in front of
> the cursor, autocompletion does not make sense.
> Can I somehow detect this situation and insert an "tab" in that case?
>
> Thanks!
> Nathan
>

--
View this message in context: http://www.nabble.com/check-if-only-whitespaces-before-cursor-in-current-line-tp24390733p24410328.html

Christian Brabandt

unread,
Jul 9, 2009, 10:24:02 AM7/9/09
to vim...@googlegroups.com
On Thu, July 9, 2009 3:39 pm, TheLonelyStar wrote:
>
>
> Can no pne help we with this?
> I can get the current line:
>
> let x=line(".")
>
> But how do I check if there are only whitespaces before cursor?

something like this maybe?
let x=getline('.')
let y='\%>' . col('.') . 'v\s\+$'
let ws=match(x, y)


regards,
Christian

TheLonelyStar

unread,
Jul 9, 2009, 11:44:43 AM7/9/09
to vim...@googlegroups.com


Christian Brabandt-3 wrote:
>
>
> something like this maybe?
> let x=getline('.')
> let y='\%>' . col('.') . 'v\s\+$'
> let ws=match(x, y)
>
>

Hi,

Sorry, if this is getting a stupid question. I searched the documentation,
but I can not find another definition for "match" than higlighting in the
current buffer.

I mean, I need something like this:
if "only_whitespaces_before_cursor" then
do_something
else
do_something_else
endif

Thanks!
Nathan
--
View this message in context: http://www.nabble.com/check-if-only-whitespaces-before-cursor-in-current-line-tp24390733p24412419.html

Dennis Benzinger

unread,
Jul 9, 2009, 12:24:12 PM7/9/09
to vim...@googlegroups.com
Am 09.07.2009 17:44, TheLonelyStar schrieb:
>
>
>
> Christian Brabandt-3 wrote:
>>
>>
>> something like this maybe?
>> let x=getline('.')
>> let y='\%>' . col('.') . 'v\s\+$'
>> let ws=match(x, y)
>>
>>
>
> Hi,
>
> Sorry, if this is getting a stupid question. I searched the documentation,
> but I can not find another definition for "match" than higlighting in the
> current buffer.
> [...]

:help match()


Regards,
Dennis Benzinger

TheLonelyStar

unread,
Jul 9, 2009, 1:21:36 PM7/9/09
to vim...@googlegroups.com

Hi,

It just does not work, if I do

let x=getline('.')
let y='\%>' . col('.') . 'v\s\+$'
let ws=match(x, y)

echo ws

The result is always "-1", now matter of whitespaces before cursor or not.
So, the '\%>' . col('.') . 'v' matches everything after cursor and \s\+
should match the whitespaces.
I think I understand ... but it does not work :(
--
View this message in context: http://www.nabble.com/check-if-only-whitespaces-before-cursor-in-current-line-tp24390733p24414449.html

Christian Brabandt

unread,
Jul 9, 2009, 1:32:22 PM7/9/09
to vim...@googlegroups.com
Hi TheLonelyStar!

On Do, 09 Jul 2009, TheLonelyStar wrote:

> It just does not work, if I do
>
> let x=getline('.')
> let y='\%>' . col('.') . 'v\s\+$'
> let ws=match(x, y)
> echo ws
>

Let's take this line (| denotes the place where the cursor is, _
stands for Spaces):

Here is some whitespace|_________

If I do
:echo ws=match(x,y) it returns 24.


regards,
Christian
--
:wq!

TheLonelyStar

unread,
Jul 9, 2009, 1:58:48 PM7/9/09
to vim...@googlegroups.com

In my case it returns -1 ....

Here is my complete function from .vimrc:

map <tab> :call TestComplete()<cr>

function TestComplete()


let x=getline('.')
let y='\%>' . col('.') . 'v\s\+$'
let ws=match(x, y)
echo ws

endfunction

it echos -1 no matter what I do. And I also do not get it to work typing for
Example:
:echo ws=match(x,y)
--
View this message in context: http://www.nabble.com/check-if-only-whitespaces-before-cursor-in-current-line-tp24390733p24414975.html

Christian Brabandt

unread,
Jul 9, 2009, 2:07:31 PM7/9/09
to vim...@googlegroups.com
Hi TheLonelyStar!

On Do, 09 Jul 2009, TheLonelyStar wrote:

> In my case it returns -1 ....
>
>

> it echos -1 no matter what I do.

Please provide an exact example. Where is the cursor and what does the
line look like?

> And I also do not get it to work typing for
> Example:
> :echo ws=match(x,y)

:echo match(x,y)

regards,
Christian
--
:wq!

TheLonelyStar

unread,
Jul 9, 2009, 3:18:23 PM7/9/09
to vim...@googlegroups.com


>
> Please provide an exact example. Where is the cursor and what does the
> line look like?
>
>

Examples (| marsk the cursor):
let| x=getline('.')
|let ws=match(x, y)
| jde

All these return -1
--
View this message in context: http://www.nabble.com/check-if-only-whitespaces-before-cursor-in-current-line-tp24390733p24416087.html

Christian Brabandt

unread,
Jul 9, 2009, 4:17:36 PM7/9/09
to vim...@googlegroups.com
Hi TheLonelyStar!

On Do, 09 Jul 2009, TheLonelyStar wrote:

> > Please provide an exact example. Where is the cursor and what does
> > the line look like?
> >
> >
> Examples (| marsk the cursor):
> let| x=getline('.')
> |let ws=match(x, y)
> | jde
>
> All these return -1

Sure, because there is just not any amount of space followed by the
line end.

I guess I misread your question. If you want to check, that there is
no space between line start and the cursor use something like this:

echo match(getline('.'), '^\s\{' . col('.') . '}')


regards,
Christian
--
:wq!

Reply all
Reply to author
Forward
0 new messages