Skip to content

Instantly share code, notes, and snippets.

@jethrosun
Last active January 25, 2024 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jethrosun/efe7cf6c9e6f102dd563c474a6e09a69 to your computer and use it in GitHub Desktop.
Save jethrosun/efe7cf6c9e6f102dd563c474a6e09a69 to your computer and use it in GitHub Desktop.
Guide for code editoring and scripting

1. TODOs before the recitation

Install the following tools

# In side the VM: GUI terminal or ssh terminal
$ sudo apt install -y git

Set up ssh connection to your VM

# Inside the VM:
$ ip addr
# take the ip address of ens160, for example for me it is 192.168.133.133. 
# Run this from a terminal on your machine: Powershell of Windows, Terminal on Mac, Linux Terimnal
$ ssh cs3650@192.168.133.133

If you are instested in not typing your password every time, learn about adding ssh keys. This is shown in the recording, but leave out insturctions.

Use VS Code

Install VS code on your computer. Instruction here.

Follow the instruction here to set up ssh connection from your local VS code to you VM. Basically, you need

1.x VIM (optional).

I encounter some trouble with setting up C++ (clangd) language server protocol clients, so I would recommand you start with VS code anyway. What this means is that you can still have code completion etc, but it is not as powerful as VS code. If you want to try VIM/NVIM, that is all fine.

**For the purpose of the recitation, I will use neovim with NvChad, which should be just a simplified version of VS code. **

NOTE: before you move on, you need to know that it is a very popular question regarding How do I exit Vim/Neovim. The answer is you press Esc, type :q and hit Enter.

Neovim config distributions

NOTE: this is only for neovim, not for vim. You will need to install neovim and start differently

# In side the VM: GUI terminal or ssh terminal
$ sudo apt install neovim npm

# To install NvChad Nvim
$ rm -rf ~/.config/nvim
$ rm -rf ~/.local/share/nvim
$ git clone https://github.com/NvChad/NvChad ~/.config/nvim --depth 1 && nvim

# To start neovim
$ nvim

Vim config distributions

# In side the VM: GUI terminal or ssh terminal
$ sudo apt install -y vim clang

Code auto completion

To put it in a simple way, as you are typing function name, the code editor is able to make guesses as in which function you are thinking of (similar to Google search auto completion). This is largely done with some magic in the backgroud and you don't need to know the details. For today, the most popular framework is Language Server Protocol, which powers VS Code.

For example when you type fop, the editor should be able to guess fopen. The same with fclose, or execvp.

C coding standards

VS code is probably the most popular editor nowadays. Pretty much for any tool (Git, code snippet, search) there is a convienient GUI in VS Code. However, i think there is still some value to learn about VIM or Emacs

  • Using teriminal and Vim is the most fundemental way of using these tools, VS code works based on the tricks we use here.
  • In some cases, you might not be able to use a fancy code editor.
  • Some of the keybinding or philosophy of Vim or Emacs can make your code editoring much more efficient (you can use the Vim extention in VS Code).

Vim mode

Vim thinks that developers' time is spent reading, navigating, and making small edits. Therefore Vim has the following modes:

  • normal
  • visual
  • insert
  • cmd

Basics

  • insert text
  • cmd: quit, save, quit and save.
  • move:
  • word,
  • find

Scripting

Variable and printing

foo=bar
echo "$foo"
# prints bar
echo '$foo'
# prints $foo

arguments

$0 - Name of the script
$1 to $9 - Arguments to the script. $1 is the first argument and so on.
    $@ - All the arguments
    $# - Number of arguments
    $? - Return code of the previous command
    $$ - Process identification number (PID) for the current script
    !! - Entire last command, including arguments. A common pattern is to execute a command only for it to fail due to missing permissions; you can quickly re-execute the command with sudo by doing sudo !!
    $_ - Last argument from the last command. If you are in an interactive shell, you can also quickly get this value by typing Esc followed by . or Alt+.
false || echo "Oops, fail"
# Oops, fail

true || echo "Will not be printed"
#

true && echo "Things went well"
# Things went well

false && echo "Will not be printed"
#

true ; echo "This will always run"
# This will always run

false ; echo "This will always run"
# This will always run
#!/bin/bash
set -ex

echo "Starting program at $(date)" # Date will be substituted

echo "Running program $0 with $# arguments with pid $$"

for file in "$@"; do
    grep foobar "$file" > /dev/null 2> /dev/null
    # When pattern is not found, grep has exit status 1
    # We redirect STDOUT and STDERR to a null register since we do not care about them
    if [[ $? -ne 0 ]]; then
        echo "File $file does not have any foobar, adding one"
        echo "# foobar" >> "$file"
    fi
done

Additional resources

If you google any of the topic we discussed, there should be plenty of material online. Pick something that you find easier to understand would be good enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment