Setup Linux developing environment for C++ on Windows

Posted by William Basics on Monday, October 9, 2023

Setup Linux developing environment for C++ on Windows

Using fonts for developers

As developers, we spend tons of hours reading code besides writing code. A beautiful, suitable, and eye-friendly font greatly impacts our efficiency.

From that, I recommend the “Nerd fonts” because they are not only targeted to developers but also support ‘iconic’ characters.

You can get the fonts from https://www.nerdfonts.com/#home

NerdFont

You can choose the font you like, but I prefer the “JetBrainsMono Nerd Font”. It comes from the JetBrains whose developing tools are quite widely used.

JetBrains Mono Nerd Font

Using Windows Terminal

When doing the development, we often need to run some commands on Windows, such as starting the WSL, doing SSH, or file-related operations.

On Windows, the first choice is the Windows Terminal. It is free and tab-style and supports PowerShell and WSL quite well. You can get it from the Microsoft Store.

Windows Terminal

Recommended settings are:

  • Set the font to “JetBrainsMono Nerd Font”: Settings → Profiles → Defaults → Appearance → Text → Font face

    Set Terminal Font

  • Copy on select: click “Open JSON file” → find “copyOnSelect” → set to “true” On Copy Select Then, we can skip “ctrl+c” to copy the text and use the mouse’s right key to paste.

Terminal on Linux

On Linux, the native terminal is far away from good enough. We can make it better with “Oh-My-Bash.”

Using Oh-My-Bash for a better shell

You can get the tutorial on its website https://ohmybash.nntoan.com/

In short, you can install it with the command

bash -c "$(curl -fsSL https://raw.githubusercontent.com/ohmybash/oh-my-bash/master/tools/install.sh)"

# or
bash -c "$(wget https://raw.githubusercontent.com/ohmybash/oh-my-bash/master/tools/install.sh -O -)"

A lot of themes already exist. You can choose your favorite one from this page https://github.com/ohmybash/oh-my-bash/wiki/Themes

Here are some examples:

oh-my-bash Theme Examples

You can change the theme in the ~/.bashrc file

OSH_THEME="agnoster"

# or just let the bot help you choose
OSH_THEME="random" # (...please let it be pie... please be some pie..)

Using Bash alias to save typing

We often type the command and its fixed arguments combination occasionally. We can convert them into minimal characters by using Bash alias.

For example, we repeatedly use “cd ..” to navigate to the above directory.

Now, we can add these aliases in the ~/.bashrc

alias .1='cd .. && ls'  # Remove the ls command if you don't want to do it with the directory changing
alias .2='cd ../.. && ls'
alias .3='cd ../../.. && ls'

Activate the config with

source ~/.bashrc

Then, we can use .1 and .2 to accelerate the navigation

Setting Vim

Vim is one of the most popular editors on Linux.

However, it is not properly configured for development initially, such as syntax highlighting, line numbers, encoding etc.

Create a ~/.vimrc file and copy the settings below to the ~/.vimrc file just created.

" Don't try to be vi compatible
set nocompatible

" Helps force plugins to load correctly when it is turned back on below
filetype off

" TODO: Load plugins here (pathogen or vundle)

" Turn on syntax highlighting
syntax on

" For plugins to load correctly
filetype plugin indent on

" TODO: Pick a leader key
" let mapleader = ","

" Security
set modelines=0

" Show line numbers
set number

" Show file stats
set ruler

" Blink cursor on error instead of beeping (grr)
set visualbell

" Encoding
set encoding=utf-8

" Whitespace
set wrap
set textwidth=79
set formatoptions=tcqrn1
set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab
set noshiftround

" Cursor motion
set scrolloff=3
set backspace=indent,eol,start
set matchpairs+=<:> " use % to jump between pairs
runtime! macros/matchit.vim

" Move up/down editor lines
nnoremap j gj
nnoremap k gk

" Allow hidden buffers
set hidden

" Rendering
set ttyfast

" Status bar
set laststatus=2

" Last line
set showmode
set showcmd

" Searching
nnoremap / /\v
vnoremap / /\v
set hlsearch
set incsearch
set ignorecase
set smartcase
set showmatch
map <leader><space> :let @/=''<cr> " clear search

" Remap help key.
inoremap <F1> <ESC>:set invfullscreen<CR>
inoremap jk <Esc>
nnoremap <F1> :set invfullscreen<CR>
vnoremap <F1> :set invfullscreen<CR>

" Textmate holdouts

" Formatting
map <leader>q gqip

" Visualize tabs and newlines
set listchars=tab:▸\ ,eol:¬
" Uncomment this to enable by default:
" set list " To enable by default
" Or use your leader key + l to toggle on/off
map <leader>l :set list!<CR> " Toggle tabs and EOL

" Color scheme (terminal)
set t_Co=256
set background=dark
let g:solarized_termcolors=256
let g:solarized_termtrans=1
" put https://raw.github.com/altercation/vim-colors-solarized/master/colors/solarized.vim
" in ~/.vim/colors/ and uncomment:
" colorscheme solarized

You can activate the setting immediately with this command inside Vim before quitting.

:so %

Visual Studio Code

VS Code has recently become the best tool for developing software on Linux. It is open-source and free to use but powerful, and it has tremendous plugins to enhance its functionalities.

  • Set the font with ‘JetBrainsMono Nerd Font’: Settings → Editor: Font Family
  • Set the terminal’s Copy On Selection: Settings → Features → Terminal → Copy On Selection
  • Set the Font Ligatures: Edit in settings.json → "editor.fontLigatures": true,
  • Set ‘Format On Save’ activated
  • Set ‘Files: Insert Final Newline’ activated
  • C/C++ - must install plugin when developing in C++ C/C++

  • C++ Class Creator - saving time to create a class C++ Class Creator

  • CS 128 Clang-Tidy - doing static analysis locally CS 128 Clang-Tidy

  • GitLens - Supercharge Git within VS Code GitLens

  • Git Graph - View a Git Graph of your repository and perform Git actions from the graph. Git Graph

  • Peacock - Quickly identify multiple VS Code instances Peacock

You can install them one by one in Extensions: Marketplace or with these commands:

code --install-extension amiralizadeh9480.cpp-helper
code --install-extension CS128.cs128-clang-tidy
code --install-extension eamodio.gitlens
code --install-extension FleeXo.cpp-class-creator
code --install-extension hbenl.vscode-test-explorer
code --install-extension matepek.vscode-catch2-test-adapter
code --install-extension mhutchie.git-graph
code --install-extension ms-python.python
code --install-extension ms-python.vscode-pylance
code --install-extension ms-vscode.cmake-tools
code --install-extension ms-vscode.cpptools
code --install-extension ms-vscode.cpptools-extension-pack
code --install-extension ms-vscode.cpptools-themes
code --install-extension ms-vscode.test-adapter-converter
code --install-extension twxs.cmake
code --install-extension johnpapa.vscode-peacock

Summary

I hope the information above gives you a good starting point. The tools and tips mentioned are useful to boost your efficiency and improve your developing experiences.

🔚