1

I am trying to sort the contents in my text file using the command

:1,$sort

This results in lines arranged in order based on the starting character - symbols, capital letters and then small letters

Now if i use

:1,$!sort

I get a different order which does not seem to make any sense. Everything looks mixed up.

So what is the role of the ! command here? Why does it mess up the entire sorting behaviour?According to https://stackoverflow.com/a/1220118/2105986, it is supposed to be a filter operator.

2 Answers 2

1

The ! character in the vi command mode causes vi to use the external application sort to sort the lines.

Your first command :1,$sort causes vi to use it's internal sort mechanism. In your second command :1,$!sort all lines are taken and given as standard input to the application sort.

Just try this vi command: :!ls -la. You will see the output of ls and after pressing Enter you get back to vi. That mechanism is usually (for example) to compile the code written in vi without closing and reopening the file.

So you see, using ! in this case causes complete different mechanisms. The ubuntu sort utility may sort different than the vi internal one.

1

According to that answer, 1,$!sort is the ex-mode version. You enter ex-mode by typing Q in normal mode. The normal mode version of this command is 1G!Gsort according to that SO answer. Otherwise, as chaos says, ! causes vim to open a shell and execute sort in it (thereby calling the sort program).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .