I have found the following command in AWK useful in Vim
:'<,'>!awk '{ print $2 }'
Python may also be useful in Vim. However, I have not found an useful command in Python for Vim's visual mode.
Which Python commands do you use in Vim?
I have found the following command in AWK useful in Vim
:'<,'>!awk '{ print $2 }'
Python may also be useful in Vim. However, I have not found an useful command in Python for Vim's visual mode.
Which Python commands do you use in Vim?
It's hard to make useful one-liner filters in Python. You need to import sys
to get stdin
, and already you're starting to push it. This isn't to say anything bad about Python. My feeling is that Python is optimized for multi-line scripts, while the languages that do well at one-liners (awk, sed, bash, I could name others but would probably be flamed...) tend work less well (IMHO) when writing scripts of any significant complexity.
I do really like Python for writing multi-line scripts that I can invoke from Vim. For example, I've got one Python script that will, when given a signature for a Java constructor, like this:
Foo(String name, int size) {
will emit a lot of the boilerplate that goes into creating a value class:
private final String name;
private final int size;
public String getName() {
return name;
}
public int getSize() {
return size;
}
@Override
public boolean equals(Object that) {
return this == that
|| (that instanceof Foo && equals((Foo) that));
}
public boolean equals(Foo that) {
return Objects.equal(getName(), that.getName())
&& this.getSize() == that.getSize();
}
@Override
public int hashCode() {
return Objects.hashCode(
getName(),
getSize());
}
Foo(String name, int size) {
this.name = Preconditions.checkNotNull(name);
this.size = size;
I use this from Vim by highlighting the signature and then typing !jhelper.py
.
I also used to use Python scripts I'd written to reverse characters in lines and to reverse the lines of a file before I found out about rev
and tac
.
Python is most useful with vim when used to code vim "macros" (you need a vim compiled with +python
, but many pre-built ones come that way). Here is a nice presentation about some of the things you can do with (plenty of examples and snippets!), and here are vim's own reference docs about this feature.