Is it possible to modify vimrc such that when I can search a variable inside a python function(excluding the same variable name in other function)
e.g I have a python file
def fun1():
a = 5
b = 0
c = a**b
def fun2()
a = 5
b = 0
c = a+b
What I want is when my cursor is in fun1() it only highlights the variable of a that I searched in fun1() and not in fun2()
(I think you could probably get better answers on vi & Vim. However I've been explained that the question is on-topic here. So +1.)
I don't think there's a straightforward, purely in-Vim solution, neither I think it's worth it that you spend some time for (trying) implementing it, and this is my answer.
I would suggest however, that you give a look at the Tagbar plugin. Why? Look at this: a Python file on the left, and its class/function structure on the right.
Another useful plugin is YCM. This plugin offers semantic completion for many languages, including Python, and that could help you a lot.
Related
I'm using the built-in function search() in Vim script to find certain words like "class".
I want to know whether the word is in Comment or not.
I'm using C++ language.
Is there any way to do it?
Thanks
Either move the cursor to search() result, or use searchpos(). Then, use the current/result coordinates synID + synIDattr against comment.
For instance, if you don't use n flag in search(),
:let is_in_a_comment = synIDattr(synID(line('.'),col('.')-1,1),'name') =~? 'comment'
Hence my lh#syntax#is_a_comment_at(lin,col) and lh#syntax#is_a_comment(mark) functions.
NB, In C++ you may need to match doxygen as well.
PS: On the subject lately I came up with an experiment: lh#syntax#getline_without(), it returns getline() result but stripped of the syntax elements we are not interested in. It can be useful to analyse a file, but not to jump to various positions within it.
I'm writing python code, and I'm facing to an annoying problem with the { and } moving keys : the vim definition for paragraphs is "blocks separated by an empty line".
However, when I code in python a class, I like to keep the indentation between methods, so is there an easy way to do paragraphs move to move like this :
class A:
def f(): #cursor here, when I type {, go between f and g
return 1
#the previous line is indented
def g():
return 2
Of course, it's always possible to remap } as a function doing
let a = #/
normal /\S\n\s*$/
normal j
let #/ = a
and { to a similar
but is there an easier way ?
The solution posted by #romainl is fine, but you might also want to look at Kana's textobj-user. It's a framework for defining text objects. Among other things, there is a plugin that uses this to define text objects for Python, which in turn have keys for moving across functions and classes. People have written many other similar plugins.
The default python ftplugin already redefines [m and ]m to jump to previous and next ^\s*\(def\|class\).
How can I use a snipmate variable in an expression?
snippet foo
${1:4} + 2 = `$1+2`
With the default value of 4 the above snippet produces:
4 + 2 = 2
Thanks.
At least in the original snipMate plugin, expressions are evaluated first, then tab stops. This allows you to define things like ${1:`v:version`}, but it makes your use case impossible to achieve.
Have a look at UltiSnips; it is modern and powerful, and might allow this. There are more alternatives, see this list on the Vim Tips Wiki.
A lot of times, I have a list of initializers in some of my code, like this:
class Foo(object):
def __init__(self, data):
self.foo = data.getFoo()
self.bar = data.getBar()
self.something = data.getSomething()
As you can see, I like my code aligned like a table. In a lot of cases, the above code can be generated by scripting Vim, coming from the output of some other program (DESCRIBE "foo"; in a database for example). Unfortunately, the scripted output usually looks like this, first:
class Foo(object):
def __init__(self, data):
self.foo = data.getFoo()
self.bar = data.getBar()
self.something = data.getSomething()
So after the automatic generation of th assignment statements, I'll have to manually align all statements for the desired look.
Now: Is there a way to get vim to align those "second halves"of the statements automatically?
The tabular plugin does exactly this. You can see it in action (and learn how to use it) here.
UPDATE: I'll give a brief explanation about the plugin usage, but no explanation will be better then Drew's video, so I strongly suggest everybody to watch it.
To use the plugin just call :Tab /= and it will align all the equal signs in the file. If you want to specify which line you want to align just give it a range :5,10Tab /= or use the visual mode (v or V) to select the desired lines, press : and insert the Tabularize command, your command line will look like this: :'<,'>Tab /=.
The argument in the Tabcommand is a Regular Expression, this means you can use this command to align many things. You'll be restricted only by your Regular Expression knowledge.
Sorry for any English mistake :D
An alternative to the already mentioned Tabular plugin is the venerable Align plugin.
One naive approach would be to first make enough space around the equal signs:
:s/=/ =/
Then, block-selecting (Ctrl-V) so that all the = characters and everything that follows is selected. Yank(y) that, paste it somewhere else.
Next, un-indent the pasted lines (10< is usually sufficient) until they're aligned to the leftmost position. Then, block-select again and paste to where they were cut off.
This feels like a lot of work though, for the desired effect.
Ruby:
file = File.new("some.txt", "r")
lines = file.readlines
Omni-completion tests
file.readl
---------
readline <- PASSED
readlines
---------
"hola".capital
---------
capitalize <- PASSED
capitalize!
---------
lines.
<-- FAILED (no suggestions)
lines[0].capital
<-- FAILED (no suggestions)
I tried Python as well, and it worked in similar way. So it looks like omni-completion can't be used for real development, as it fails on pretty simple cases?
Am I missing some thing? May be the intellisense can be improved some how for Ruby/Python?
The issue is that Vim does not know if line is a String, an Array or some other Class. There is no deep syntactical analysis in Vim. Vim has no idea of scope, if a variable or method has been defined, etc.
It is only suggesting similar words. So yes, Vim is more limited than an IDE in this aspect. This is also why Eclipse can suggest errors as you typed them, and Vim can't.
Vim is much more basic: in a way, everything is text, and not necessarily seen as "code".
So you are right this is one of Vim limitation.
There are some plugins to work around those limitations (omnicpp is using ctags to determine the scope of some methods) but they are often developed on a per-language basis and there is no silver bullet.