Email: bknpk@hotmail.com Phone: +972-54-7649119


V

 

VIM functions to perform unix like sort and uniq commands.

  1. 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.

  2. 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

  3. 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

  4. 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>

  5. Recently a friend told me that VIM has command to do that. :sort u

  ...




Search This Site


Feedback This Site




new pages on this site