linux-guide
Keyboard shortcuts
CTRL + A– takes you to the beginning of the lineCTRL + E– takes you to the end of the lineCTRL + K– delete (yank) everything after the cursorCTRL + U– delete (yank) everything before the cursorCTRL + Y- "paste" everything you yanked (paste in quotes because it doesn't actually go into your system clipboard, only your bash clipboard).CTRL + L- clear the screenCTRL + R– reverse search through historyCTRL + D- exit bash shell
Editors
Nano
To open up a file in the nano code editor, just use the nano command:
nano <filename>
Here are some keyboard shortcuts that nano provides:
- ctrl + g : to see all possible shortcuts and get help
- alt : undo last action
Vim
To open vim, run the vim <textfile.txt> command to open up a specific command in vim.
To quit vim, first get into command mode by hitting esc twice, and then type and run :q. To quit no matter what, run :q! .
Vim has two modes: insert (typing text) and edit (for commands) mode. Here is how to get to them:
- insert mode: press
ito enter insert mode - edit mode: press
escto enter edit mode
Basic commands:
esc: toggles between typing mode and command mode. In typing mode you can make changes to your text file. In command mode you can type vim commands:q: quit vim:w: save file:wq: save and exit:qa!: quit no matter what.
Navigating a file
Arrow keys- move the cursor aroundj, k, h, l- move the cursor down, up, left and right (similar to the arrow keys)^ (caret)- move cursor to beginning of current line$- move cursor to end of the current linenG- move to thenth line (eg 5G moves to 5th line)G- move to the last linew- move to the beginning of the next wordnw- move forward n word (eg 2w moves two words forwards)b- move to the beginning of the previous wordnb- move back n word{- move backward one paragraph}- move forward one paragraph
Text editing
Here are commands that you have while in command mode that let you edit text quickly:
:d100: deletes the next 100 lines. Obviously, you can change the number like:d50to delete 50 lines.x- delete a single characternx- delete n characters (eg 5x deletes five characters)dd- delete the current line
Important commands
set nu: turns on line numbers for a fileu: undo last action
Bash History
You can use the history command to see a record of all your bash history.
Learning about commands
These are helper commands that help you learn about other commands:
help <command>: shows the MAN page for the specified command.which <command>: shows the file location for the specified command.type <command>: tells which type a specified command is.
For the type command, there are 4 types of things a command could be:
- executable “bin” commands
- built-in shell command
- shell function
- alias
Dealing with files
Getting file info
To get information about the what resource a filepath actually is, use the file command:
file <filepath>: describes whether the filepath is a symlink, file, or directory.
You also have these commands to get file size info
df -h: displays the disk usage information for the laptopdu -h <folder>: displays the disk usage info for the specified folder
To parse the basename of a file, you can use the basename command:
The basename <file> command gets back only the filename (including extension) from a filepath. You can add these options to further customize the command:
--suffix=SUFFIX: if specified, removes the suffix from the basename, like a file extension name like.sh.
Reading files
You can use the cat command to print out the contents of a file, or have a nice time reading the file using less.
cat <filename> # prints out file contents to stdout
less <filename> # shows file content in dedicated reader
cat
Here is basic usage of the cat commandL
cat <filename>: prints out the content of the file.cat FILES...: concatenates the contents in all the specified files and prints them all out at once.
You also have options on this command:
-n: prints out line numbers along with text
less
You have these navigation commands while in the less reader:
qto quitspace barto go down a pagebto go up a page/<pattern>to search for a pattern
You also have these options:
-N: displays line numbers along with text
head and tail
head <filename>: prints out the first 10 lines of a filetail <filename>: prints out the last 10 lines of a filehead -n NUMLINES FILENAME: prints out the first numlines lines of a file.tail -n NUMLINES FILENAME: prints out the last numlines lines of a file.