How to execute a range expression for delete command in Pymol? - pymol

I am using PyMol and as of now I have to write a long command to delete useless files after splitting states:
split_states posesD01;
delete posesD01_0002;delete posesD01_0003;delete posesD01_0004;delete posesD01_0005;delete posesD01_0006;delete posesD01_0007;delete posesD01_0008;delete posesD01_0009;
I want to avoid deleting posesD01_0001
I have tried reducing it using regular expressions to:
split_states posesD01;
delete posesD01_000[2-9];
But it doesn't do anything. It does not throw any errors but it also does not do anything.
Any suggestions are welcome.
Thank You for reading!

The PyMOL prompt accepts wildcards (*). If that fits you, it would be the easiest approach.
delete posesD01_000*
For a more fine-tuned selection of numbers a pure python loop can be used.
for i in range(2, 10): cmd.delete(f"posesD01_000{i}")
To take care of leading zeros (in the case of a wider range), a explicit formatting must be set.
for i in range(0, 50): cmd.delete(f"posesD01_00{i:02d}")
By the way, you don't need to write a semicolon at the end of each command. This is python! :)

Related

Deleting specific patterns without deleting the whole lines

Say I want to remove all comment blocks in my source code without deleting the whole lines they are on.
It's possible to achieve this using the
:%s/\/\*.*\*\//
command. I was wondering, is there a specific delete command for this, or is replacing the matched pattern the best approach? The difference most likely wouldn't be much, I'm just curious.
Replacing with nothing really is the idiomatic 'delete this pattern' operation.
:%s/pattern//g
If you want to blank all lines that contain a pattern, like in your example, the obvious solution is to add wildcard matches around the pattern.
:%s/.*pattern.*//
An alternative is to use :global with a normal mode or Ex command. These two achieve the same thing:
:g/pattern/normal! S
:g/pattern/delete|put! _
By the way, while I don't recommend using abbreviated command names in scripts or in code that other people might see, I think it's fine to use them interactively. Thus I tend to abbreviate such commands as :g/pattern/norm! S and :g/pattern/d|pu!_.

Replace backward slash '\' with forward slash '/' in vim

I have some scripts(linux) which are generating the o/p with respect to Windows. I expect it to be /home/manty/... it generates \home\manty\... It is very annoying when I try to change one by one all the time. So just for this purpose, I am leaving vim and going to gedit to perform "find and replace". How can I do it in vim?
I tried with following :
:%s/\\/\/
It changed only in the two places. I tried several other combinations as well nothing is working out.
Ah. I missed the g thing.
:%s/\\/\//g
This command does the trick. g stands for global and will make the change globally (complete file).
Also, you can add an extra option of c if you want to monitor each change. If you want to use c option the command would be :%s/\\/\//gc and then select y or n for each change.
I will wait for better answers from vim geeks. I am sure there must be a better way.
You can use another delimiter like this:
:%s,\\,/,g

Complex replacement in gVim

I have been a terrible person as of late when it comes to Minecraft. I have over-modded it to the point that I need to completely re-write the IDs of them all.
The only problem is that... It'll take about a couple of hours jut to re-write them ONCE, not to mention if any of them collide with the original game. So, in order to save time, I figured I'd use Vim, but after reading through several of the helpful posts on here, I still only know a minimal amount about the replacement feature/command. Here's what I'm trying to do:
Replace this
I:exampleModnamePath.id=16389
I:exampleModnamePat2.id=19657
Etc.
With this
I:exampleModnamePath.id=20000
I:exampleModnamePath.id=20001
Etc.
This continues for a while, and to those who answer, could you please inform me of how it works, so I don't have to ask these questions all the time?
For your perusal:
:let g:num = 1
:g/\.id=\d\+$/exec 's!\.id=\d\+$!.id='.g:num.'! | let g:num=g:num+1'
This is slightly simplified version of my code for (re)numbering chapters in the ebooks.
Idea in a nutshell: use :g to run something over affected lines; use :exec to generate/run new substitution command AND increment the counter. Tried it once and was surprised to find that the trick worked. Was inspired by my previous toying with :g//s/// combo.
I'm not sure what is the rule you are using to choose which number to use for replacement but if all you need
is just a new number that doesn't collide with previous ones you could try just replacing the first digit
with something in a range not used. Something like replacing 16389 with 76389
To do that you could use this :s/Path.id=.\(.*\)/Path.id=7\1
That would search for the string Path.id= followed by a single character and then a group of more characters.
I will replace it with the string Path.id=7 and the group previously selected.
You could make it more selectiv adding letters before Path.id to match only certain types of paths.

How to extract text matching a regex using Vim?

I would like to extract some data from a piece of text with Vim. The input looks like so:
72" title="(168,72)" onmouseover="posizione('(168,72)');" onmouseout="posizione('(-,-)');">>
72" title="(180,72)" onmouseover="posizione('(180,72)');" onmouseout="posizione('(-,-)');">>
72" title="(192,72)" onmouseover="posizione('(192,72)');" onmouseout="posizione('(-,-)');">>
72" title="(204,72)" onmouseover="posizione('(204,72)');" onmouseout="posizione('(-,-)');">>
The data I need to extract is contained in the title="(168,72)" portions of the input. In particular, I am interested in extracting coordinate pairs in parentheses.
I thought about using Vim to first delete everything before title=", but I am not really a regex guru, so I am asking you. If anyone has any hint, please let me know! :)
This will replace each line with a tab-delimited list of coordinates per line:
:%s/.* title="(\(\d\+\),\(\d\+\))".*/\1\t\2
This task can be achieved with a much simpler solution and with few keystrokes using normal command:
:%normal df(f)D
This means:
% - Run normal command on all file lines;
normal - run the following commands in normal mode;
df( - delete everything until you find a parenthesis (parenthesis included);
f) - move the cursor to );
D - delete everything until the end of the line.
You can also set a range, for example, run this from line 5 to 10:
:5,10normal df(f)D
If you want an ad hoc solution for this one-off case, it might be quicker simply to select a visual block using CTRL-v. This will let you select an arbitrary column of text (in your case, the column containing title="(X,Y)"), which can then be copied as usual using y.
you can match everything inside title=() and discard everything else like this:
:%s,.*title="(\(.*\))".*,\1,

VIM: Replace all occurences of a word in the current C/C++ function

I've to replace all occurrences of a specific macro inside some(only some amongst dozens) C functions. Since the file is thousands of lines long, with several instances of the macro in all the functions, I'd like to replace all occurrences within the particular function the cursor is currently placed.
I know VIM provides navigation commands (like [[ to go to the beginning of the current function, and then % to find its matching closing brace) , but I can't figure out how to use them to come up with the required search-replace command.
Can anyone help ?
Place your cursor on the first opening brace. Then type v% and you will see the function body get highlighted. Then type the replacement command :s/find/replace/g and hit enter. This will replace within the selected function.
Note: You will see you command prompt change to: :'<,'>:s/find/replace/g.
Although I would also recommend dogbane's solution, I thought I'd also mention the NrrwRgn plug-in. It's quite useful for working on a continuous subset of a buffer.

Resources