Skip to main content

Command Palette

Search for a command to run...

Useful commands in bash and vi

Updated
7 min readView as Markdown
Useful commands in bash and vi
X

"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:

  1. :%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.

  2. 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 v to enter visual mode, make sure you are on the top/first line

    • if not press gg to go to the first line then press shift + G to go to the last line, highlighting the whole lines in the process.

    • press y to copy/yank

    • OR you can use ggVG, it does the same thing

      1. Enter the command mode by pressing the Esc key.

      2. Type ggVG and press Enter. This command moves the cursor to the first line, selects the entire file, and enters visual line mode.

      3. Press y to 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.

  1. :set paste

    When 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.

  2. How to Indent multiple lines at once in vi

    1. Open the file in vi or Vim.

    2. Move the cursor to the starting line of the block you want to indent.

    3. Press Ctrl + V to enter visual block mode or esc + shift + v to enter visual line mode or esc + v to enter visual mode. This mode allows you to select a rectangular block of text.

    4. 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.

    5. Press Shift+(dot) i.e "." to indent the selected block to the right. Each press of > will indent the block further.

    6. Once you've indented the block as desired, press Esc to exit visual block mode.

  3. 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.

  4. Shift + A to enter Insert mode at the end of the current line.

  5. Move the cursor to the end of the current line by pressing the $ key.

  6. Move the cursor to the beginning of the current line by pressing the 0 (zero) key.

  7. Shift + I to enter Insert mode at the beginning of the current line.

  8. In command mode(esc): u undo cntrl + r redo

  9. How to setup vi/vim to navigate to any line or position you want by moving the cursor directly without having to move one character at a time:

    1. Verify your vim version has mouse support by running vim --version

      If you see a +mouse in the output, you are good to go.

    2. vim ~/.vimrc

      and add this line set mouse=a

    3. Restart vim/vi: Open a file with vim/vi and test.

  10. 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.

  1. ctrl l: To clear your whole screen, both in linux and sql)

  2. 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
  1. find: Finds files and directories in the entire system

    1. find / -name <file>: searching a file by name. you can use wildcards to eg find / -name *.py. / means where to start searching for.

    2. 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).

    3. locate <file>: Used to quickly find filenames anywhere on the file system

    4. truncate --size 0 <file name> : Deletes contents of that file

  2. hostname -I : To show your remote computer ip address

Vscode:

  1. shift-tab: will un-indent nicely

  2. code file.py: Creates the file and opens it

  3. ctrl + a and ctrl + /: To comment multiple lines of code at once

GIT BASH:

  1. Shift + Insert: To paste in git bash on windows

  2. When 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/config and add this line ServerAliveInterval 300

TERMUX

  1. How to create a shared storage with Termux and your Android Storage:

    • Run this command on your Termux: termux-setup-storage

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.

  1. Download Simple Http Serverfrom your playstore

  2. Download Hackers Keyboard

    https://m.apkpure.com/hacker-s-keyboard/org.pocketworkstation.pckeyboard/amp

    1. Start the Local Web Server:

      By clicking START after launching the app

    2. Click on Root Folder to navigate to where your html and css files are stored.

    3. Copy the ip address to any browser of your choice and view your files.

Git

  1. 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:

      1. do Git Status

        This will show you the current status of your repository, including any deleted files.

      2. 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/file
        

        Replace /path/to/file with 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) or Cmd + 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.

More from this blog

PERSONAL BLOG

110 posts

Use the search button to search for a specific topic or keyword *all posts are updated on the go*