VIM functions to perform unix like sort and uniq commands.
-
The following defines two functions, implemented directly in VIM language. In
this example the unix like: sort and uniq commands are implemented,
using VIM build in language.
-
The need for sort uniq, on text files, has been discussed in this site:
remove file repetition
A simple example of verifying uniqueness in seed generating without leaving vim:
sort, uniq on a marked VIM text
VIM running a shell command on a block
sort, uniq using shell commands
-
The code can be either added to ~/.gvirc or written in a file.vim and
sourced, when needed:
:so file.vim
func Strcmp(i1, i2)
return a:i1 == a:i2 ? 0 : a:i1 > a:i2 ? 1 : -1
endfunction
func SortBuffer()
let lines = getline(1, '$')
call sort(lines, function("Strcmp"))
call setline(1, lines)
endfunction
func MyUniq()
" start from first line
call cursor(1, 1)
" while we're not on the last line
while line(".") < line("$")
" current line is the same as last line
if getline(".") == getline(line(".") - 1)
" so delete it
delete
" current line != last line
else
" just go to next line
normal j
endif
endwhile
" we are on the last line
while getline(".") == getline(line(".") - 1)
" remove it if it matches
delete
" the line before
endwhile
endfunction
-
To use it either source it or add it to ~/.gvimrc. The latter requires also
the following lines:
func VIMsu()
call SortBuffer()
call MyUniq()
endfunction
nmap vsu :call VIMsu()<CR>
-
Recently a friend told me that VIM has command to
do that. :sort u
|