Useful commands in bash and vi

"I am currently a Software Engineering student at ALX. I'm passionate about technology and enjoy conducting research to find answers on my own. I have a natural inclination to ask 'WHY' more often than 'HOW'.
"While working on projects at ALX, I have acquired a wealth of interesting and diverse knowledge about software engineering and computer science in general. Therefore, I needed a place to store and save all this information, allowing me to refer back to it whenever I forget."
Vi:
:%s/\s+$//e
Sometimes Betty white space errors can be frustrating, especially when you haven't even figured out what the hell is wrong with your code.
The vi command above clears all unnecessary white space from your code.Copying and pasting in Vi can be frustrating too especially when you want to copy whole code and paste it elsewhere.
press esc to enter command mode
press
vto enter visual mode, make sure you are on the top/first lineif not press
ggto go to the first line then pressshift + Gto go to the last line, highlighting the whole lines in the process.press
yto copy/yankOR you can use
ggVG, it does the same thingEnter the command mode by pressing the Esc key.
Type
ggVGand press Enter. This command moves the cursor to the first line, selects the entire file, and enters visual line mode.Press
yto yank (copy) the selected code.
One issue here is that if you opened up one or more terminals, you can't copy from one terminal to the other only on that particular terminal. On that particular terminal, you can copy from one file and open another file to paste but you can't copy and paste on a different terminal.
:set pasteWhen you copy a code from another source/git hub or net, in most editors it appears to be scattered like in Termius for example or it might not preserve the format of the code. you can put your vi in paste mode, this mode helps preserve the formatting of the copied code.
How to Indent multiple lines at once in vi
Open the file in vi or Vim.
Move the cursor to the starting line of the block you want to indent.
Press
Ctrl + Vto enter visual block mode oresc + shift + vto enter visual line mode oresc + vto enter visual mode. This mode allows you to select a rectangular block of text.Move the cursor down to the last line of the block you want to indent. This will select the desired lines in a rectangular block.
Press
Shift+(dot)i.e"."to indent the selected block to the right. Each press of>will indent the block further.Once you've indented the block as desired, press
Escto exit visual block mode.
Betty 5 function and 40 lines max errors( c language)
There was one time I and my friend has the same code in a file, 6 functions, and some functions were more than 40 lines, I keep getting the 5 functions and 40 lines max per function errors. It was weird, so I started researching, betty doesn't have much information or community on it. Didn't get the answer in a day. Lastly, I figured it out, The secret was prototypes, he had placed all his prototypes in the file, while I had mine in the header file.
So if have six or more functions, place the 6 prototypes of the functions at the beginning of the file, and also place it in the header file.
This, for some weird reasons, suppress these errors.Shift + Ato enter Insert mode at the end of the current line.Move the cursor to the end of the current line by pressing the $ key.
Move the cursor to the beginning of the current line by pressing the 0 (zero) key.
Shift + Ito enter Insert mode at the beginning of the current line.In command mode(esc):
uundocntrl + rredoHow to setup
vi/vimto navigate to any line or position you want by moving the cursor directly without having to move one character at a time:Verify your vim version has mouse support by running
vim --versionIf you see a
+mousein the output, you are good to go.vim ~/.vimrcand add this line
set mouse=aRestart vim/vi: Open a file with vim/vi and test.
To enable auto indentation for html files in vi:
vim ~/.vimrc:And add this line
filetype plugin indent on
Bash:
When you want to copy the whole code from vi, it gets more frustrating, so many commands on the net claiming to be able to do just that, never still seen anyone that works.
What I normally do is use cat command
cat main.c
outputs the file content of main.c as plain text, then you can highlight and copy it all at once.
ctrl l: To clear your whole screen, both in linux and sql)du -sk <file>: Is used to estimate the disk usage (in kilobytes) of a file.du: The command to estimate file or directory disk usage.-s: Summarizes the total disk usage of the specified file or directory.-k: Displays the disk usage in kilobytes.
When you run du -sk file, the command will calculate the disk space used by the file file and display the result in kilobytes.
For example, if the file occupies 25,000 kilobytes (25 megabytes), the command will output:
25000 upgrade.img
find: Finds files and directories in the entire systemfind / -name <file>: searching a file by name. you can use wildcards to egfind / -name *.py./means where to start searching for.find / -size +10M -exec ls -ltr {} \;: Maybe you are running out of space, this command can help you track files taking up space. This command finds files with size >= 10 mb, exec makes sure the output is listed in long format, with time stamps and reversed so older files stays at the top(ie list them in long format, sorted by modification time in reverse order).locate <file>: Used to quickly find filenames anywhere on the file systemtruncate --size 0 <file name>: Deletes contents of that file
hostname -I: To show your remote computer ip address
Vscode:
shift-tab: will un-indent nicely
code file.py: Creates the file and opens it
ctrl + aandctrl + /: To comment multiple lines of code at once
GIT BASH:
Shift + Insert: To paste in git bash on windowsWhen you ssh connections timeouts more frequently than you had wanted:
This is useful for preventing the server from timing out the connection due to inactivity.
In your default Git Bash terminal, before initiating the SSH connection to your remote computer do
vim ~/.ssh/configand add this lineServerAliveInterval 300
TERMUX
How to create a shared storage with Termux and your Android Storage:
- Run this command on your Termux:
termux-setup-storage
- Run this command on your Termux:
Simple Htttp Server
How to setup a simple http server on your android to view and test your html and css files on a web browser on your phone.
Download
Simple Http Serverfrom your playstoreDownload Hackers Keyboard
https://m.apkpure.com/hacker-s-keyboard/org.pocketworkstation.pckeyboard/amp
Start the Local Web Server:
By clicking
STARTafter launching the appClick on
Root Folderto navigate to where your html and css files are stored.Copy the
ipaddress to any browser of your choice and view your files.
Git
If you mistakenly deleted a file you are working on from your terminal
If you haven't pushed the changes to your remote Git repository, but the file was previously added to the Git repository and committed locally, you can still recover it using the Git command line. Here's what you can do:
do Git Status
This will show you the current status of your repository, including any deleted files.
Recover the Deleted File: If you see the deleted file listed in the output of
git status, you can use the following command to restore it:git checkout HEAD -- /path/to/fileReplace
/path/to/filewith the path to the deleted file relative to the root of the repository.
Browser
- Hard Refresh (Force Reload): Try performing a hard refresh or force reload of the page in your browser. You can do this by pressing
Ctrl + F5(on Windows/Linux) orCmd + Shift + R(on macOS). This will force the browser to fetch all the resources again, including the logo.
If you've added a new logo to your project but your browser is still displaying the old logo, it's likely due to browser caching. Browsers often cache static assets like images to improve loading speed.



