1.1. Linux Commands and Vim

1.1. Linux Commands and Vim #

Common Commands #

CommandDescriptionExample
cdChanges the current directorycd dir1
catOutputs the contents of the filecat file.txt
echoOutputs the specified argument (to stdout)echo Hello
lsLists files in the specified directoryls dir1
rmRemoves (deletes) a file or directoryrm file.txt
manShows the manual page for the specified commandman ping
exitExits the current terminal sessionexit
mkdirCreates a new directorymkdir dir2
whichShows the full path of a command in the $PATHwhich google-chrome
pwdPrints the current working directorypwd
treeOutputs a list of files and directories in tree formtree dir1
lnCreates a new linkln dir1 linktodir
findHelps find data in file hierarchies (can be used for many advanced things)find ./ -name "*.txt"
psLists processes running on the systemps -ef
grepSearches a string for matches (basically a Find function)grep tofind file.txt
tailOutputs the last lines of a filetail log.txt
headOutputs the first lines of a filehead file.txt
touchUpdates the access times for a file, or can just be used to create a blank filetouch file.txt
mvMoves or renames a file or directorymv file.txt newname.txt
diffShows the differences between two filesdiff file-v1.txt file-v2.txt
whoamiOutputs your current usernamewhoami

Note: folders should be called directories in Linux/Unix.

Vim #

Vim is a highly-configurable text editor for Linux and Unix.

Command: vi or vim

  • Vim operates in 2 modes: Command and Insert
    • Command mode allows you to use your keyboard to enter commands
    • Insert mode allows you to enter text and edit a file
  • By default Vim opens in Command mode
    • You can change to Insert mode by hitting i (there are more keys to do this)
    • You can go back to Command mode by hitting esc
  • No one appears to know how to exit Vim
    • :wq: Save (write) and quit
    • :q!: Quit without saving

Common Vim Commands #

  • Cursor Movement
    • h j k l: up, down, up, right
    • Most versions allow arrow keys
  • Delete
    • dd: Delete line
    • x: Delete at cursor
    • dw: Delete word
    • r: Replace a character
    • D: Delete to end of line
  • Editing
    • yy: Copy (yank) a line
    • nyy: Copy n lines
    • p: Paste above current line
    • P: Paste below current line
    • ?string: Search backwards for string
    • /string: Search forwards for string
    • .: Repeat last command
    • u: Undo last command

For full commands see the amazing Vim cheat sheet.

© 2024 Ryan Bester & Collaborators