• Vidhi

Working on emacs remotely

I often find myself working on remote servers, editing C++ codes and running / debugging them. It is always a struggle to find the right environment that helps one do all of this in seamless way that aids productivity. I believe the environment one uses to perform the tasks around the development of the code defines the quality of the code. Whenever I find myself switching windows and moving from one setting to another to edit, run and debug, I notice that the number of bugs that creep into the work increases exponentially. Alignment issues, structuring problems and even the smallest of the typos can be avoided with consistency of environment. Emacs is a very powerful ally to this end. While the learning curve can be steep, it literally pays to learn to use emacs for remote editing and debugging C++ codes with gdb. Add a screen session over it and you have an extra virtual machine that is always on (depending on how much your IT managers like coffee :)

In this post I will share my environment setup which has helped me with my work in debugging / editing C++ projects remotely. After ssh into a remote server, I make sure to start a screen session. This helps me to come back to the same environment that I have been working on for a while and saves the setup time. To do this, simply use the command screen (with option "-DR"). It can be immensely helpful to not lose focus and maintain the flow of thoughts from your previous session of work. Since I am an emacs fan, I always have a few windows of emacs (with option "-nw") open for quick editing. At the same time, I do not claim to be an emacs guru. I have learned this powerful beast enough to get through my day-to-day coding tasks. Emacs works with tabs called buffers. These are easily manageable micro-tabs with powerful settings to help you keep track of open files, current folder details, meta information about all open buffers and if you decide to use gdb inside emacs, it shows a debugger information buffer as well.

Here is a cheat sheet I refer to for my setup. While this information is available everywhere on the internet, not all of it at one place. Some of this is compiled on the fly - run into a problem, look up a solution on the internet, note it down here.


ctrl+a c - new screen

ctrl+a d -  screen detach 

ctrl+a Number - go to the "Number" screen

Editing in emacs:

emacs -nw - (here nw stands for no window)

C(ctrl) + f - find /load file - Tab completion available to guess name of files and let emacs show you the existing options in the folder that matches your partial filename

C + g - cancel

C + h will show help

C + s "string" - forward search for "string" in the buffer. Use C + r for backward search

C + x + b show buffers - tab will show all buffers 

C + space - selects a region. Use arrow keys after C+space

M + w - copy

C + w - cut (kill)

C + y - paste (yank)

M + x mark-whole-buffer - select entire file shown in buffer

M + x indent-region - indents the selected region inside buffer

M + % - query replace interactive mode. Replace all / individual selections

C + x o will switch cursor to other buffer

C + shift + minus to undo (until ** becomes — in the info bar of the buffer)


The command M-x gdb starts GDB in an IDE-like interface, with specialized buffers for controlling breakpoints, stack frames, and other aspects of the debugger state. I prefer the gud-gdb which essentially means run GDB, using a GUD interaction buffer for input and output to the GDB subprocess.

M(esc) + x gud-gdb will start gnu debugger

file filename - assigns the file filename to debugger

C + x space will set breakpoint

r - runs the file filename. If you need to send arguments, use r <args>

c - continue till next breakpoint

p variable_name will show the current value while debugging

p argv[0]@argc prints the value of pointer argv upto argc elements

set print pretty - dereference a pointer

p *inarray[0]->m_data@size prints array objects upto size

Damage control:

If you are like me, you will find yourself trying C + z sometimes on emacs. (Yes, I play online games). This essentially suspends the current emacs session. Just type "fg" and the suspended session is back to foreground.

If you want to permanently close the current emacs session, C+x C+c is the key.

Happy emacs-ing !

41 views0 comments