Aldebrn

Recent site activity

Sage in Research‎ > ‎

Background for coding

This document is meant to encourage those curious about programming in a command-line environment, or simply using it for general purpose PC usage. It will be evolving. I have no idea how to dump the knowledge of many years of this kind of development into a document so here goes.

Unix

Stand-alone command-line apps

  • ls: list files of current directory
    • ls -sh: the "s" gives the size of files in bytes, "h" makes it human-readable (kilobytes, megabytes, etc.)
    • ls -rt: "t" sorts by time modified, "r" reverses this, so "rt" puts the latest-modified files at the bottom
    • ls -Sr: "S" sorts by size, "r" reverses them to put biggest files at the bottom
  • du: prints all sub-directories with sizes
    • du -h: human-readable sizes
    • du --max-depth=1: don't descend more than this
  • wget: downloads files from the web. Easier for me to use than IE/Firefox's "Where do you want to save this file?" garbage.
    • wget --continue: continues a broken download
  • lynx or links or links2: command-line web browser, sometimes handy
  • seq  10: prints off numbers 1 to 10
    • seq -w 5 50: prints of 0-padded numbers
  • cat <filename>: prints out the file
  • head and tail <filename>: prints out the top or bottom lines of a file
    • head -n 13 <filename>: prints out the first 13 lines of a file
  • grep: search for text
    • grep fminsearch *m: does a line-by-line search of files ending in "m" for "fminsearch" and prints out the lines that passed
    • cat myfile.txt | grep fminsearch: filters the input of grep ("cat myfile.txt") for "fminsearch"

Shell scripting

  • for i in `seq -w 1 20`; do mv old_files_$i.txt new_files_$i.txt; done
    • This should give the complete novice a taste of the awesome power of shell scripting. I use the Bash shell's scriptability almost every day to do mundane tasks quickly.

Vim

Vim is a text editor that has evolved over many years, descended from vi which was born in the mid-70s. I use gvim, the GUI vim whenever I am at a GUI interface but often enough I am not and therefore use the command-line version of vim.

I used to use Emacs, which has a a learning-curve that's a tiny bit better than Vim's, for the sole reason that when you start Emacs, you can start typing right away and letters will appear on the screen. With Vim, we enter "insert" mode to begin typing. I switched because I worked at a shop where everyone used Vim and had all kinds of great time-saving code-editing tricks to share with me.

Vim (like Emacs) is infinitely configurable (I rely on good system administrators to set up a usable vim config file). This is not a Notepad-replacement. Vim has been around for many years and can do anything you can imagine doing with text. If you are doing something tedious, it can and should be automated in Vim. Using Vim really brings home the idea that a little bit of time and effort in the beginning can pay huge rewards over a lifetime of use.

Here's a tour of vim to get you started. I'm happy to give live/online guided tours.
  1. Start (g)vim
  2. "i" to enter insert mode and begin typing (just the one character, not the quotes)
    1. Modern shells and gvim allow you to use Control+arrows to jump words.
  3. Keep typing some stuff. Make lines that are longer than your window, type the same word several times, etc.
  4. Hit the Escape button (or Control-c) to exit the insert mode.
  5. Type ":w myfile.txt" and Enter to save the file
  6. "i" again to enter insert mode.
    1. Some people get angry when the up/down arrow keys jump to the next paragraph instead of screen-lines. You can google for the setting that enables screen-lines. Make sure you type it in the non-insert mode.
    2. Hit Escape button.
  7. Type ":s/something/something else/g" and Enter to replace every instance of "something" with "something else" in the current line (case-sensitive).
  8. Hit "v" to enable visual select (highlighting text) and select some text.
  9. Hit "d" to "cut" the text.
  10. Move somewhere else and hit "p" to paste the text.
  11. Hit "u" to undo the paste.
    1. Type in Control-r to redo (and restore the paste).
  12. Hit  "P" (capitalized) to paste the text in a slightly different way. One of these pastes it before the cursor, the other one on the cursor. I can never consciously remember which. With undo/redo, it's easy to figure it out if my muscle memory fades.
  13. Type "/something" and hit Enter to search the file for "something". 
    1. Hit "n" to go to the next place "YourName" is found.
    2. Hit "N" to go to the previous instance of "YouName."
  14. Type ":sp" to split the current file into two panes.
  15. Type ":vsp newfile.txt" to split the window vertically and start editing "newfile.txt". Using gvim when you start using panes is a lot easier.
  16. Use Control-W+arrow keys to move between these panes.
  17. Type ":wq" to save and quit one of the panes.
  18. Type ":wqa" to save and quit all files and exit vim.

Sage

These are detailed in the larger Sage in Research document.