vim
link: https://www.keycdn.com/blog/vim-commands
There are two modes in vim command: command mode and insert mode. We will go through some frequent usages.
Basic commands
:help [keyword]
: this command will help you find out the helpful documentation of the [keyword] you type.:e [filename]
: this command will open a file. You need to provide the directory path of the file in the [filename].:w
: this command will save the current file.:wq
: this command will save the current file and quit vim.:q!
: this command will quit the vim without saving the file.
Movement
h
: move the cursor one charactor to the left.l
: move the cursor one charactor to the right.j
: move the cursor down one line.k
: move the cursor up one line.b
: move the cursor one word to the left.w
: move the cursor one word to the right.0
: move the cursor to the beginning of the line.$
: move the cursor to the end of the line.gg
: move the cursor to the beginning of the file.G
: move the cursor the end of the file.:#
: # is the number of a line. Move the cursor to that line.
You can add a count in front of one command to make Vim complete a command multiple times.
Editing
v
: highlight one charactor per timeV
: highlight one line per timey
: yank highlighted contentsp
: paste whatever has been copied after the current lineP
: paste whatever has been copied on the current lined
: delete highlighted contentsdd
: delete current lineu
: undo the last operationCtrl+r
: Redo the last undo.
: repeat the last action
Searching
/[keyword]
: searches for text in the document from the beginning to the end of the file.?[keyword]
: searches for text in the document from the end to the beginnign of the file.n
: Find next appearanceN
: Find previous appearance:%s/[pattern]/[replacement]/gc
: replaces all occurrences of a pattern and confirms each one.
grep
You can use grep
to search some pattern in a file.
Basic syntax
grep pattern filename
Useful arguments
-n
: show matching line numbers-c
: count the number of occurrence-r
: perform recursive search
grep -r "string" /home/
: search “string” in all files in the home directory and all its subdirctories.
Together with other commands
- find out process pid
ps -ef | grep someprocess
ls
ls
is used to list files or directories in Linux system. You can do more than what you expect by leveraging powerful arguments.
Useful arguments
ls -l
: list the contents of the directory in a table with columns including- content permissions
- number of links to the content
- owner of the content
- group owner of the content
- size of the content in bytes
- last modified date/time of the content
- file or directory name
ls -s
: list files or directories with their sizes.ls -S
: list files or directories and sort by size in descending order. You can also add a-r
to reverse the order.ls -lh
: list files or directories in the same table format as-l
, but with readable file sizesls -a
: list files or directories including hidden files or directories.ls -t
: list files or directories and sort by last modified date in descending order. You can also add a-r
flag to reverse the order.
touch
You can use touch
to create a new file, like touch filename
sed
You can use sed
to manipulte and edit text files without even opening them.
Basic syntax
sed [operation] filename
Useful usages
- substitute or replace a string
sed 's/pattern/replacement' filename
>s
in the command is the replacement indicator and/
in the command is the delimiter By default,sed
will only replace the first occurrence in a line. - substitute or replace a string(all occurrences)
sed 's/pattern/replacement/g' filename
- substitute or replace a string in a specific range of lines
sed 'line1,line2 s/pattern/replacement/g' filename
The range is from line1 to line2.
cat
cat
is short for concatenate
. You can use this command to show contents of a file or concatenate files together.
Basic syntax
cat filename
cat > filename
Useful usages
- display contents of a file
cat filename
- concatenate files together to a new file
cat filename1 filename2 > newfile
- copy one file
cat filename1 > filename2
>>
is printout redirection. It will redirect std out to file.<
is printin redirection. - append one file to another
cat filename1 >> filename2
ps
ps
will report a snapshot of the current processes.
Basic syntax
ps [options]
Useful arguments
ps -ef
: print out all processes. Use System V style.ps aux
: same asps -ef
. Use BSD sytle.
top
kill
df
df
is short for disk free, which is used to show the amount of free disk space available on Linux system and to understand the filesystems that have been mounted.
Useful arguments
df -h
: show all filesystems and their disk usage in human-readable form.df /path/to/directory_or_file
: show the filesystem and its disk usage containing the given file or directory
du
chmod
chmod
sets the permissions of files or directories. Permissions defines the permissions for owner of the file(user), members of the group who owns the file(group), and anyone else(others).
For example, if we want to set the permission like below:
- User can read, write and execute
- Group can read, write and execute
- Others can read and execute
You can use command:chmod u=rwx,g=rwx,o=rx filename
</br> Or you can use equivalent command: chmod 775 filename
where 4 stands for read
, 2 stands for write
, 1 stands for execute
.
ping
ping
can test the connectivity to a specific destination computer by sending one or more ICMP Echo Request packages.
Basic syntax
ping [options] URL
wget
wget
lets you download files form the web. It supports background running.
Basic syntax
wget [options] URL
curl
curl
can transfer data from or to a server without user interaction.
Basic syntax
curl [options] URL
Useful usages
- retrieve the homepage
curl example.com
- save the output to a file
curl -O url_address
>curl -o filename url_address
>-O
will resue its original filename. - get the headers of a url
curl -I url_address
xargs
xargs
allows you to build and execute commands from standard input.
Useful usages
echo "filename" | xargs touch
is equivalent to touch filename
Comments powered by Disqus.