A combination of Vim and command-line tools is a handy text-processing toolkit for many tasks. This entry provides some examples. It makes coding and writing convenient and enjoyable.

Linux text tools

The magic of | allows the output of a function to be the input of another. This is analogy to one of cores of functional programming: with different combination of functions, one can achieve different results easily without rewriting the partial codes.

Some list of linux or python text tools are natually integrated with Vim:

  • awk: a powerful pattern scanning and processing language
  • column: columnate lists
    • (print "PERM LINKS OWNER GROUP SIZE MONTH DAY HH:MM NAME" && ls -l | sed 1d) | column -t
  • curl: access a webpage to vim
  • find: search for files in a directory hierarchy
  • gpg:used to encrypt/decrypt some text
  • grep: search for a pattern under directory
  • join: join lines of two files on a common field
  • perl: a powerful text processing language
  • python: provide all kinds of text processing tools
  • sed: stream editor for filtering and transforming text
  • sort: sort (in reverse) lines of text
  • tree: pretty print directory structure
  • uniq: remove duplicate lines or count the number of occurrences
  • base64: encode/decode text with base64

In Vim, just use :!{cmdtool} or :read {cmdtool} to pipe the output into current buffer.

Examples

Json format

  • :<,>!python -m json.tool: format json

Note use visual-select for the portion of JSON text, then :<,>!python -m json.tool

Sorting and removing duplicates

:<,>!uniq or sort

  • Some other way:
    !cat % | sort -t{DELIMITER_CHAR} -k2
    !cat % | sort -t\# -k2
    
    Go outside Vim, under shell:
    cat file | sort -t{DELIMITER_CHAR} -k{ColToSort}
    

Hashing

  • and base64 :'<,'>!base64, :'<,'>!base64 --decode

Columnating

  • column -t use: :<,>!column -t
  • Can be useful for formatting markdown table
# before:
a b   cc  d 
1  2  3 4
i   ii  iii   iv

# after:
a  b   cc   d
1  2   3    4
i  ii  iii  iv

# before:
|name|age|height|weight|
|john|17|187|78|
|tom|8|144|43|
|peter|18 |178|57|

# after:
name   age  height  weight
john   17   187     78
tom    8    144     43
peter  18   178     57

Vim redirect and put

It provides more granular control of shell output to register, under the cursor, or external file.

  • :vnew|put=execute('scriptnames')
  • :vnew|put=execute('ls')
  • :put=execute('ls')
  • :put=execute('echo expand(\"%:p\")')
  • :put=expand('%')
  • :redir
    :redir > {file}
    "do kinds of things, but the output are still printed in command window
    :redir END " only write to file until `:redir  END` called
    

Conclusion

Vim works well with many powerful cmd tools. The synergy makes Vim and other cmd tools great again!

Your thoughts...